Can I infinitely toggle attribute without a timer?

eric.ojhaeric.ojha PRO Posts: 23
edited August 2012 in Working with GS (Mac)
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??

Comments

  • ChobbifaceChobbiface PRO Posts: 491
    Make an integer attribute 'toggle'. Leave the value as 0

    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?
  • eric.ojhaeric.ojha PRO Posts: 23
    It's also about understanding why the method of changing the toggle value doesn't work
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    Two interpretations come to mind:

    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?
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598

    @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

  • eric.ojhaeric.ojha PRO Posts: 23
    @gyroscope
    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?
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    Rules need external forces to change them. Make two rules one that changes it to false and another to true. Now those two rules will be triggered off each other. You can't make a loop inside a function which is what a rule is.
  • eric.ojhaeric.ojha PRO Posts: 23
    That's what I did in my initial example, but it didn't seem to do anything?
    Or did I mess something up in my initial example? (see first post)
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    edited August 2012
    You have to remember the state will change 60 times a second so a display text might not show the state change.
  • eric.ojhaeric.ojha PRO Posts: 23
    @FryingBaconStudios

    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?
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    Sounds like it let me do a test today and make a loop.
  • simo103simo103 Member, PRO Posts: 1,331
    @eric.ojha .... I would put a timer in the rules just to slow things down for testing. So for example I would put if true then timer After 1sec change to False and the same in the False to True rule ... I find I often need to slow things down a bit to make sure my logic is right. Once you know its working then you can reduce the timer to 0.2 secs or something like that to see if the action is so fast without a timer that it won't work. Hope that helps ... Keep the counter out of the timer though.
  • eric.ojhaeric.ojha PRO Posts: 23
    @FryingBaconStudios
    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 =)
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited September 2012

    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

Sign In or Register to comment.