Local variable reseting on function call

Bugs will be moved here once resolved.

Post » Tue Sep 17, 2013 9:29 am

Link to .capx file (required!):
https://docs.google.com/file/d/0B3gyd8mDsXNlQVA1YnJpb1JPblE/edit?usp=sharing

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
B
4
Posts: 5
Reputation: 310

Post » Tue Sep 17, 2013 9:44 am

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.
Image ImageImage
B
169
S
50
G
169
Posts: 8,286
Reputation: 108,216

Post » Tue Sep 17, 2013 9:55 am

@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)wizsg2013-09-17 09:57:39
B
9
S
3
G
1
Posts: 29
Reputation: 2,207

Post » Tue Sep 17, 2013 3:17 pm

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.sandulas2013-09-17 15:17:58
B
4
Posts: 5
Reputation: 310

Post » Tue Sep 17, 2013 3:34 pm

I'm afraid you've taken that a little to literally.
Its a grouping, or collection of events, not the organization control.
Image ImageImage
B
169
S
50
G
169
Posts: 8,286
Reputation: 108,216

Post » Tue Sep 17, 2013 3:57 pm

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.
Scirra Founder
B
395
S
232
G
88
Posts: 24,371
Reputation: 193,762

Post » Wed Sep 18, 2013 8:52 am

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).
B
4
Posts: 5
Reputation: 310


Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 2 guests