Local variable reseting on function call

0 favourites
  • 7 posts
From the Asset Store
Color Variable
$4.50 USD
10% off
Easily store, modify, read and manipulate colors with Color Variables!
  • Link to .capx file (required!):

    docs.google.com/file/d/0B3gyd8mDsXNlQVA1YnJpb1JPblE/edit

    Steps to reproduce:

    1. create a new event group

    2. add a local variable inside the group

    3. create a new function that sets the value of the local variable

    4. call the function in the "on start of layout" to set the variable value to 100

    5. display the value of the variable in a text

    Observed result:

    the value is zero

    Expected result:

    the value should be 100

    the local variable shouldn't be reset since we're not leaving its scope; this can be solved by marking the variable "static", but that is not the point.

    Browsers affected:

    Chrome: yes

    Firefox: yes

    Internet Explorer: yes

    Operating system & service pack:

    windows 7 SP1

    Construct 2 version:

    r139

  • The variables scope is the on start event, and a function by definition leaves that scope.

    The only bug you've found is the fact that you can reference that variable.

  • newt, sandulas There is no error.

    The variable in the scope of the function. That the variable is not reset should do it as static. (local variable set static)

  • The variable is declared in the "Main" group in my example (see the .capx). So this group should be the scope of the variable, not the on start event. Quote from the manual:

    "For example, if an event variable is in a group of events, it becomes a local variable. Then, it will only appear as an option for a variable in events inside that group. In other groups or in other event sheets it does not appear at all and cannot be accessed. This makes the variable local to the scope in which it is placed."

    Also from the manual:

    "By default, local variables reset to their initial value whenever entering their scope (usually every tick), like local variables in programming languages"

    So the variable should be reset only when entering the group.

  • I'm afraid you've taken that a little to literally.

    Its a grouping, or collection of events, not the organization control.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • This is actually by design. Within the scope of a local variable, triggers act like function calls; for each depth of the function call, local variables hold unique state (like with recursive functions in traditional programming languages).

    So when 'On start of layout' fires, 'Health' has the value 0. Then you call the "SetHealth" function. Because a new trigger is firing within another trigger, it actually becomes a new unique value. Think of it as 'Health2' at this point. You set 'Health2' to 100, then the function returns. 'Health2' scopes out since the function call has ended, then it returns to continue execution in 'Start of layout'. This returns to using 'Health', which was not actually modified, so is still 0.

    Workaround: make it a static variable.

  • Thanks everyone!

    Ashley, I understand what you mean, so everything makes sense (although it works differently from most programming languages, where the variable is holding a unique state for each function call only if it is declared inside the function).

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)