Press button - add 1 to local var

Get help using Construct 2

Post » Fri Apr 06, 2012 10:52 pm

I feel like this might actually be a bug - but I'm not sure. It's very simple. I'm just trying to add 1 to a local variable when I click on a button. Here's the entirety of the code:


And here's a capx file: http://dl.dropbox.com/u/28484936/add_to_local_var.capx

For some reason it won't add to the "number" variable.

Anyone have a clue? Thanks/
B
20
S
5
G
3
Posts: 65
Reputation: 3,764

Post » Fri Apr 06, 2012 11:17 pm

Locals only apply to that event. Since clicking a button is a trigger once type of event, nothing will happen.
Try using a global, or adding a variable to the button object itself.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Fri Apr 06, 2012 11:18 pm

I think this has to do with the 'repasting' of triggered events. On export, as far as I understand it, having a triggered condition in a subevent causes the event to get moved to the end of the event sheet with all the preceeding conditions of the parent events pasted in. As such, I think the local variable isn't getting pasted with it.

@Ashley - what do you think?
Moderator
B
88
S
32
G
33
Posts: 3,005
Reputation: 27,432

Post » Fri Apr 06, 2012 11:21 pm

move local number before event 1 and it will work, also you dont need event 2 and 3 as subevents of event 1 to achieve what you wontpodpathos2012-04-07 00:03:12
B
12
S
2
G
2
Posts: 96
Reputation: 1,583

Post » Fri Apr 06, 2012 11:55 pm

if you arrange events like this it will be triggered only once


seems that local number is here a condition - if it is 0 then add 1
B
12
S
2
G
2
Posts: 96
Reputation: 1,583

Post » Sat Apr 07, 2012 12:27 am

I know that if I make it a global variable (which is what happens if I move it above event 1)it will work. But, this is just a simple example to show what I'm trying to achieve. In my game I want to avoid making global variables when possible.

It seems like what I'm trying to do (have an triggered event that changes a local variable) should be possible. Maybe it's just not implemented yet?

Thanks for your help. I guess for now I'll switch it to a global variable.


B
20
S
5
G
3
Posts: 65
Reputation: 3,764

Post » Sat Apr 07, 2012 12:43 am

Local variables get reset every tick. Quote from the manual:

------
1. Local variables reset their value to the initial value every tick. This is by design, and intended to match how local variables work in real programming languages.
------

If you need to store the variable more than 1 tick, it's got to be global.

One trick I'm using (because I don't like to have a lot of global variables either) is this:

1. Create a sprite on the layout where you need the "local" variable and give it some "meaningful" name. For example I need some local variables that would store their value across ticks on my "Play" layout, I create a sprite called "varsPlay" and add it to the Play layout, somewhere out of view (-100, -100 coordinates or something)

2. On this sprite, add all the local variables you need, as Instance variables. Like in your example, you'd add a "number" variable to the sprite varsPlay, and in your event you'd have varsPlay -> Add 1 to instance variable "number", and Text -> set text to varsPlay.number.

I hope this helps you keep your design tidy, it does help me do that. One more advantage you get if you do this is that you can use "boolean" variables too, which you can't if you use proper variables.

Cheers!
B
14
S
5
G
7
Posts: 235
Reputation: 5,175

Post » Sat Apr 07, 2012 12:45 am

@Geo

Thank you! That explains it very nicely actually. Using a "local variables" sprite seems like a good way to go. I appreciate you taking to time to explain your method.
B
20
S
5
G
3
Posts: 65
Reputation: 3,764

Post » Sat Apr 07, 2012 1:01 am

Sure, no problem, glad it helps
B
14
S
5
G
7
Posts: 235
Reputation: 5,175


Return to How do I....?

Who is online

Users browsing this forum: DiabloOvermind, emoticon, odiusfly and 16 guests