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.[attachment=0:27ffonrh][/attachment:27ffonrh]
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?
Develop games in your browser. Powerful, performant & highly capable.
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.
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.
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