Broken local variables?

Discussion and feedback on Construct 2

Post » Wed Feb 04, 2015 8:28 pm

Pardon me my great ignorance if I simply didn't fully understand the way local variables work but, is this normal? (see capx)
You do not have the required permissions to view the files attached to this post.
B
11
S
3
G
3
Posts: 206
Reputation: 2,379

Post » Wed Feb 04, 2015 9:02 pm

There is nothing wrong, the reason it appears to not work, is because it gets reset to 0 all the time. You can see this if you run it in debug mode and pause it, and just keep pressing step.

You have to move the "Text Set Text.." below the "set variable to round..". And optimal move the local variable to the top of the event where you need it.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Wed Feb 04, 2015 9:05 pm

Or you can make the local variable "static" so it saves it's value.
B
91
S
31
G
102
Posts: 5,232
Reputation: 67,250

Post » Wed Feb 04, 2015 9:47 pm

@nimos100 : I just tried putting everything into the same group, but it keeps not working (see capx). Why does the variable get reset to 0 anyway?

( @r0j0hound : Yes I know, and then it would show in the debugger. But I need the variable to change over time.)
You do not have the required permissions to view the files attached to this post.
B
11
S
3
G
3
Posts: 206
Reputation: 2,379

Post » Wed Feb 04, 2015 10:06 pm

Ashley and Tom work hard on not only Construct 2 but something just as magical...
https://www.scirra.com/manual/1/construct-2

...and if you type "Local Variable" to search in the Manual you get ....
https://www.scirra.com/manual/83/variables

...and if you scroll down to where it says "Static and constant variables" you get...
"By default, local variables reset to their initial value whenever entering their scope (usually every tick), like local variables in programming languages. If the variable is marked static in the Event Variable dialog it will persist its value permanently, like a global variable."

Good luck. :-)
B
20
S
5
G
1
Posts: 102
Reputation: 1,706

Post » Wed Feb 04, 2015 11:54 pm

I had read that page. More than once on top of that. Last time was today, and I had purposely avoided reading the paragraph about "static and constant variables" because it was of no interest in my case and I wanted to keep working on resolving my problem. Thanks for pointing it out though.

Another thing you can read, in the "edit profile" page, is :
"Don't forget, let's all be nice to each other in the forum!"
Not quite sure if "cynical" and "mocking" is compatible with "nice", but let's say that none of us can remember every single thing that's written on this website.

Thanks everyone.
B
11
S
3
G
3
Posts: 206
Reputation: 2,379

Post » Thu Feb 05, 2015 12:02 am

I just tried putting everything into the same group, but it keeps not working (see capx). Why does the variable get reset to 0 anyway?


Not really sure how to explain why it resets. But the way you can look at it, is that a local variable only keep the assigned value during a single cycle (tick) if its in an event. So at the end of the event the variable is "destroyed".

For a function it only live during the function call. So if you call a function several times within the same cycle, the variable will still be reset to it initial value each time you call it.

So its a "Use and throw away variable" where global is "Keep and reuse variable" that stays the same until you change it. So it will never reset to its initial value unless you do it manually.

Not sure if that makes it easier to understand. But not sure how to explain it otherwise :D
Last edited by nimos100 on Thu Feb 05, 2015 12:48 am, edited 1 time in total.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Thu Feb 05, 2015 12:42 am

Ok now I see where I got lost. I confused "static" and "constant". I don't need constant variables right now, but I could use some static variables.

Now it works! Thanks @nimos100 ! :)
B
11
S
3
G
3
Posts: 206
Reputation: 2,379

Post » Thu Feb 05, 2015 12:59 am

However I meant to post a screenshot to show you how to do it, but my dropbox had a melt down. But got it working.

But what I meant you could do was like this.
Image

It doesn't really help you to put the "Set text.." in an every tick, if it only updates once every 1 sec (60 ticks).

A good idea I think is to use Variable objects instead of local and global variables. Because when you get a lot of them you will end up with a huge list, that you have to scroll through, which gets annoying.

So instead you can just make a small sprite (16x16) and place it outside the layout and call it "Game_settings" or whatever. And then you add all the variables you need to this one instead. That way you can easily find them when you need them and you can use Booleans as well. Personally I rarely use anything other than those for global and local variables. And if you need them in other layouts, you can just turn the object global in the properties settings.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,828

Post » Thu Feb 05, 2015 3:41 am

Yeah, sometimes I use objects too, but when you use a lot of variables into a single expression, the expression starts getting really long. Example : Variable1 + (Variable2*Variable3) / Variable4 feels better for the eyes and the mind than variable_object.Variable1 + (variable_object.Variable2*variable_object.Variable3) / variable_object.Variable4 ;)
B
11
S
3
G
3
Posts: 206
Reputation: 2,379


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 1 guest