using local variables?

Get help using Construct 2

Post » Wed Nov 09, 2011 7:36 pm

if i change global variable into local, i can't find where i can change it, i went to system > under global en local, but it isn't in the dropdownmenu? did it miss something?
ImageImage
B
61
S
19
G
6
Posts: 809
Reputation: 9,028

Post » Wed Nov 09, 2011 8:05 pm

If you put it in a sub-event and not in the first 'branch' it should automatically turn into a local variable for that 'branch'.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Wed Nov 09, 2011 8:13 pm

yeah but that i know, but where can you use it or change it, or is it suppose to be only a static number

if you do a set var... where can i find that local numbervtrix2011-11-09 20:14:42
ImageImage
B
61
S
19
G
6
Posts: 809
Reputation: 9,028

Post » Wed Nov 09, 2011 8:30 pm

A local variable is only reachable in the event you set it. (that's the point of local variable).
For example, if you have a local variable in a group, you can only reach it in the events and actions of that group.

That's why it is called "local" in opposition to "global" variable that you can access from anywhere in your event sheet.
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,999
Reputation: 57,793

Post » Wed Nov 09, 2011 8:46 pm

i realise that, i put it inline like a subevent under a condition, then i expect that i can change it somewhere in that condition group, have you been able to use it?
ImageImage
B
61
S
19
G
6
Posts: 809
Reputation: 9,028

Post » Wed Nov 09, 2011 9:16 pm

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,999
Reputation: 57,793

Post » Wed Nov 09, 2011 9:26 pm

ahh thx for the example, i needed to put everything in a subevent and then place the local above the subevent, i was placing it on the same level as my condition

thankyou
ImageImage
B
61
S
19
G
6
Posts: 809
Reputation: 9,028

Post » Fri Sep 14, 2012 5:08 pm

I have a question about local variables:

Can anyone give some practical examples, how to use local vars effectively?

Why is it not possible to assign a default value to some parameter like Sprite.X?

For most of the cases I have to use global variables, because local vars change they value every tick to the default. May be I don't get something.
B
42
S
11
G
4
Posts: 505
Reputation: 7,267

Post » Fri Sep 14, 2012 5:52 pm

[quote]Can anyone give some practical examples, how to use local vars effectively?[/quote]
They are handy for temporarily storing values from one event to another. The key word being "temporary".

[quote]Why is it not possible to assign a default value to some parameter like Sprite.X?[/quote]The values of the first instance of a sprite is where the initial values come from.
B
79
S
24
G
54
Posts: 4,743
Reputation: 40,749

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

Next

Return to How do I....?

Who is online

Users browsing this forum: mercuryus, Naightwolf, Skebab and 22 guests