Page 1 of 1

How do I add value to variable?

PostPosted: Wed Oct 28, 2015 12:44 am
by Knovolt
How would you add different values to the same global variable?
Eg:
I click "button", and it adds 1 to "gVariable".
I click "button" a second time, it adds 2 to "gVariable".
I click "button" a third time, it adds 3 to "gVariable".
I click "button" a fourth time, it adds 1 to "gVariable".

Then it continues to loop by adding 1, then 2, then 3, then 1, then 2, etc...

Is this possible to do in as few events as possible? Rather than have many, many different events/functions and calling them?

Thanks in advance.

Re: How do I add value to variable?

PostPosted: Wed Oct 28, 2015 1:25 am
by ultrafop
My thinking is that you would have two variables here, an "add variable" and the "gVariable" you mentioned.

Set up something like,

1)
When button is clicked
sub event - if "add variable" = 3 set to 1
else - add 1 to "add variable"

2)
when button is clicked add "add variable" to "gvariable"

Re: How do I add value to variable?

PostPosted: Wed Oct 28, 2015 3:21 am
by chrisinfinger
So based on your example by the 4th click the gVariable would equal 7?

If instead it should be 1,2,3,1,2,3 and not 1,3,6,7 then you would use a on click compare event starting with 0.
On-click/compare gVar=0/set gVar 1, gVar=1 set 2, gVar=2 set 3, else gVar>2 set 1.

Re: How do I add value to variable?

PostPosted: Wed Oct 28, 2015 5:38 pm
by Knovolt
@ultrafop
@chrisinfinger

Thanks for the input and ideas. But, with your help and a little changing around, I got this working.

https://gyazo.com/8c7b830a981693ee8cafc55bd43412a2

Re: How do I add value to variable?

PostPosted: Wed Oct 28, 2015 6:21 pm
by Paradox
I took that as a challenge to make the shortest solution possible.
Can anyone go shorter? ;-)

here's mine: (button sprite has an instance variable, IVariable)

Image


Oops, just noticed, I included 4 instead of resetting on 4, just leave the 4 out of the Tokenat.