How do you do, every X...
gamesfua
Member Posts: 723
Quick question. How do you do a line of code that says every x do this.
Example: lets says i want my integer attribute to say "every interval of 5 do xyz." Its not the xyz portion thats the problem, its the interval that i'm stuck on.
In case i'm still confusing anyone, heres another example:
Lets say i want to pop an ad up every 4 times a player hits the play game button. I would obviously need a line of code that says every interval of 4 show ad.
Thanks a million!
P.S. I realize i could just make a counter for that example that counts to 4 and then resets to 0, but thats not what i'm looking for. I want something to keep counting every instance of a number (like every 4).
Example: lets says i want my integer attribute to say "every interval of 5 do xyz." Its not the xyz portion thats the problem, its the interval that i'm stuck on.
In case i'm still confusing anyone, heres another example:
Lets say i want to pop an ad up every 4 times a player hits the play game button. I would obviously need a line of code that says every interval of 4 show ad.
Thanks a million!
P.S. I realize i could just make a counter for that example that counts to 4 and then resets to 0, but thats not what i'm looking for. I want something to keep counting every instance of a number (like every 4).
Comments
1. Number of touches.
2. 4th touch activated.
When touch is pressed changed attribute 1 to attribute 1+1
When touch = 4 change attribute 2 to 1 or true (depending on attribute used)
Perform action now wanted.
Then have a rule that then resets both attributes back to 0 so it can loop again.
i.e. self counter = 4
game 4thTouch = true
actor 1
when
game 4thTouch = true
Do .....
actor 2
when
game 4thTouch = true
Do .....
It all depends on whether you want just 1 actor to do something or multiples.
1. Create an integer attribute (it can also be a self attribute if all your rules will be on a single actor)
2. In your actor of choice add this:
- Rule: When touch is pressed
-- Change attribute: game.attribute to (game.attribute+1)%4
3. Then add this:
- Rule: When game.attribute = 0
-- Do whatever you want
With this method you don't have to reset your game attribute every time, it will be done automagically. To change the number of touches you need for your something to happen just change the number after the "%".
If its only that actor that needs to know then using the % is def the way to go as it saves on the resetting rule
By using the "%" system the attribute will automatically reset itself to 0 once it reaches the number after the "%". So you just have to check when that attribute reaches 0 and you're good to go, regardless of what actor the "%" rule is placed.
Use a game attribute, it will start on 0, it then counts 1, 2, 3 and 4 and resets back to 0. Run your rule once the value = 4.
4 is the value at which a %4 module reset:
%2 -> 0, 1, 0, 1, 0, 1, ...
%3 -> 0, 1, 2, 0, 1, 2, ...
%4 -> 0, 1, 2, 3, 0, 1, ...
%5 -> 0, 1, 2, 3, 4, 0, ...
and so on.
2,3,0,1 - So every 4th press will be on 1. If you keep your rule to trigger on 0 it will incorrectly trigger as it will be after 3 presses when it needs to be 4. If you keep it at 0, it will trigger straight away the 1st time without any presses, after this it would be correct but 1st time only it would trigger when not needed. Again, you could build a work around to this.
Basically the percentage used in the way @MarkOnTheIron suggested means the change attribute will cycle between a pre-defined number of options.
So when Mark said - Create a rule that when touched - Change attribute: game.attribute to (game.attribute+1)%4
This makes a game attribute you created every time you press it, go up like this;
0,1,2,3 and upon next touch, will start back at 0 ... so, another cycle of 0,1,2,3, it will continuously loop as 0,1,2,3,0,1,2,3 each time it is pressed (in increments of +1 to each touch).
So the number after the % symbol is determining how many changes the attribute has;
%2 -> 0, 1, 0, 1, 0, 1, ...
%3 -> 0, 1, 2, 0, 1, 2, ...
%4 -> 0, 1, 2, 3, 0, 1, ...
%5 -> 0, 1, 2, 3, 4, 0, ...
See how they loop when they reach their maximum number of options you specify after the % symbol, make sense now?
It would start as -1, on 1st touch it would change to 0, 2nd to 1, 3rd to 2, 4th to 3 and then repeat. So if you changed the main rule to activate when the game attribute equalled 3 that would work
Yep, like this:
The counting rule: Change self.count to (self.count+1)%4
And start the attribute on -1
http://www.mediafire.com/?l6c62cjn7jvoc5h
; P