using local variables?

Get help using Construct 2

Post » Tue Jan 22, 2013 10:58 pm

@Kyatric - So I checked out the example you posted (many moons ago).
I have a group set up to control the horizontal movement of my character. I then create a local variable (we'll call it GroupVar). GroupVar is set up to control the speed of the character --> Set X to PlayerBox.X+GroupVar*dt (PlayerBox being the invisible platform object).
All the Events following GroupVar's declaration inside that group remain on the same level as GroupVar.

At the push of a button, I add or subject a value from GroupVar, ultimately adjusting the speed of the character as it moves.

That's the idea, anyway. This works when I set GroupVar as a global variable. But (using an on-screen text test) I noticed that when I declare GroupVar locally at an initial value of 120, no matter how many times I push the proper button, it remains at 120. Again, as a global variable, it worked perfectly, and I saw the text display the proper value and the character moved accordingly (even to the point of moonwalking at negative values! lol)

What I don't quite understand (though I believe I have an inkling as to the reason) is why a global variable seems to not be reset every tick while a local variable is. IS THIS because every tick the local variable is essentially re-declared within the group/event it's associated with, whereas the global variable remains open to any and all events/actions that may call on it?

As always, thank you!
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
112
S
20
G
8
Posts: 741
Reputation: 13,636

Post » Wed Jan 23, 2013 12:46 am

@Rhindon:
[QUOTE=Rhindon]IS THIS because every tick the local variable is essentially re-declared within the group/event it's associated with, whereas the global variable remains open to any and all events/actions that may call on it?[/QUOTE]

Yes.
Global variables are even global to the project, and their value is "fixed" over time.

Local variable has a smaller scope (a group for example) and is reset every tick.

Since this post though features were added to variables: static and constant.

A static local variable won't be reset and will hold a fixed value over time, but its scope remains unchanged.

A constant is a variable which value will never change during the execution of the game.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Wed Jan 23, 2013 4:34 am

@Kyatric - AH! Okay, so if I want to isolate the GroupVar to its group, I just need to set the variable to static so it doesn't get reset every time?
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
112
S
20
G
8
Posts: 741
Reputation: 13,636

Post » Wed Jan 23, 2013 5:45 pm

@Rhindon: Well the static will prevent it resets every tick.
Otherwise, making a local variable in the group "isolates"/sets the scope to only this group.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Wed Jan 23, 2013 5:59 pm

@Kyatric - I think I got it. Thanks. I tested it and everything seems to be working.
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
112
S
20
G
8
Posts: 741
Reputation: 13,636

Post » Sat Feb 07, 2015 3:20 pm

How can i reset local variables?
You do not have the required permissions to view the files attached to this post.
B
6
Posts: 24
Reputation: 300

Previous

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 24 guests