Can I infinitely toggle attribute without a timer?
I have an attribute named "toggle" that is boolean set to TRUE.
I have a rule saying that if toggle is TRUE, then change attribute to FALSE.
I have another rule saying that if toggle is FALSE, then change attribute to TRUE.
I then have a displaytext of "toggle". I was expecting it to be constantly changing between TRUE and FALSE, but it is just stuck on TRUE. How do I get this infinite switch to happen??
I have a rule saying that if toggle is TRUE, then change attribute to FALSE.
I have another rule saying that if toggle is FALSE, then change attribute to TRUE.
I then have a displaytext of "toggle". I was expecting it to be constantly changing between TRUE and FALSE, but it is just stuck on TRUE. How do I get this infinite switch to happen??
Comments
Make a rule: when touch is pressed (or whatever action you want):
- change attribute 'toggle' to (toggle+1)%2.
This will keep flipping 'toggle' between the values 0 and 1.
On your display text actor, create rule: when toggle = 1, display text toggle
Is this what you're after?
1) (It only executes rules once -- unless it is made to do otherwise)
Analysis: It did what you told it to do. It checked each rule. The first time it changed the Boolean to false. Then (1/60 of a second later) it changed the Boolean to true.
Now its waiting for the next behaviors to do. (Since there are no more behaviors to execute its just sitting there with nothing to do.)
or
2) (Its executing the rules over and over so fast that all you see is the end result of each pass)
Analysis: Its doing what you are telling it to do. Its checking each rule. The first rule changes the Boolean to false. Then (1/60 of a second later) the second rule changes the Boolean to true.
Now its displaying the true condition as it goes back to the top to check things again. (Thus the false condition happens so fast that its never seen.)
Which interpretation is true? Do rules only execute once? Or do they execute constantly? Or is there another explanation?
@eric.ojha
Hi, useful way from @Chobbiface using modulus but your way is just as valid when it works right...
Anyhow, to your question of why your method of toggling isn't working...
My take on it: for a toggle true/false to work successfully for what your after, you need to put the second rule into the Otherwise section.... if you don't, it'll happen so fast over and again without a pause as @RThurman wrote.
So your rules should look something like this:
When toggle is false
---your behaviour
----your stuff
----your other stuff
Change attribute to true
Otherwise
---your behaviour
----your stuff
----your other stuff
Change attribute to false
Hope that sorts it for you.
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
I tried the method that you suggested, but it doesn't work for me.
I tried adding an integer attribute as well, x=x+1 each time the boolean changes from true to false, and I displayed that attribute as well, and it only made it to 1. So it looks like the rule is only being executed once. Any thoughts/suggestions?
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
Or did I mess something up in my initial example? (see first post)
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
Okay, I've added a counter (index) that increases by 1 each time the toggle switches from TRUE to FALSE and vice versa. But, each time I run the program, the counter stops at "2" meaning the rules are only executing once and then stopping... right?
Guru Video Channel | Lost Oasis Games | FRYING BACON STUDIOS
any luck with the test loop?
@simo103
but that's just it, I wanted to be able to do this without using a timer as per my original q
Hi Eric, in my rush a week or so ago, I put a change attribute without saying which one in my example, although if you guessed that - which I'm sure you did - then I'm not certain why it's not working for you....
Anyway, here is the updated example:
When toggle is false
----your stuff
Change attribute toggle to true
Otherwise
----your stuff
Change attribute toggle to false
As a switch going on forever, that can't not work.....
PS If you wanted a counter, so that it toggles only a certain amount of times, then th following example should work out (make an integer attribute x and set it's value to the max. amount of times you want):
When toggle is false and x >0
----your stuff
Change attribute x to x-1
Change attribute toggle to true
Otherwise
When toggle is true and x >0
----your stuff
Change attribute x to x-1
Change attribute toggle to false
Hope that sorts it for you.
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps