Local / Global variable inconstancy. Please explain..?

Discussion and feedback on Construct 2

Post » Thu Nov 13, 2014 12:35 pm

Can someone please explain to me in a simple way, why I get different results when I use a local from a global variable? This is on two events that should in essence be the same!

Have a look at the attached capx.
LocalVsGlobalVariables.capx

When the starting value for both variables is 0, the local variable doesn't advance beyond 1 while the global variable advances as expected. Even more strangely, while the supposed X value for the Local Sprite advances by 32 on each click, the actual position remains the same, just like the local variable. It's hard for me to explain it in words but if you check the reported values on the Text's columns you'll see what I mean.

If the starting value is 1, the Local Sprite does move but this is just due to the fact that the local variable gets stuck to a value of 2 instead of 1.

This is a mess on something that should be really straight forward. What I'm I doing wrong, or what are my false expectations?
You do not have the required permissions to view the files attached to this post.
composer - multimedia artist
www.eli0s.com/en/
B
69
S
26
G
5
Posts: 1,146
Reputation: 9,829

Post » Thu Nov 13, 2014 1:59 pm

LocPoz set"Static"to"√"
B
37
S
11
G
1
Posts: 80
Reputation: 3,325

Post » Thu Nov 13, 2014 2:00 pm

It seems you didn't understand the static variable concept yet, so you might want to read on that part. The global variable work as they seem because they're static by default. Difference between local and global is global is accessible anywhere, while local is only accessible by event at same level under single parent and all it's child. It's the static that keep the number unchanged, while non-static will reset every tick.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Thu Nov 13, 2014 2:01 pm

<ninja'd>
Local variables only last for the length of the event - that's what makes them local. Otherwise they would be global. Locals are only used for temporary calculations. You could use an instance variable on the "Local" sprite, if you needed data to persist per object.

https://www.scirra.com/manual/83/variables
ImageImageImage
B
71
S
21
G
230
Posts: 3,687
Reputation: 128,845

Post » Fri Nov 14, 2014 1:55 am

@winsonzhong ,
@DuckfaceNinja ,
@blackhornet ,

Thank you! I understand now!

I had made the wrong assumption that local variables act as global but only to a smaller scope. I was mislead to believe that I could add to them in a way that my example was setup, because in the past I used local variables in a loop to advance incrementally some values. But the difference is that the loop runs in a single tick and then it resets. I hadn't realized that.

Again, thank you all for your time and explanations ;)
composer - multimedia artist
www.eli0s.com/en/
B
69
S
26
G
5
Posts: 1,146
Reputation: 9,829


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 8 guests