Cycle through integers

imjustmikeimjustmike Member Posts: 450
edited February 2015 in Working with GS (Mac)

Hey guys, I'm trying to have a button that triggers 4 different states (in this case, up, down, left, right) and I'd like to use an integer attribute to do that. To do that I'm looking to create a button that increases an integer attribute by one each time it is clicked until it reaches 3, then, on the next click bring it back to 0.

The current rules I'm using are as follows:

IF touch is released AND integer < 3 THEN change integer.attribute to integer.attribute+1
IF touch is released AND integer = 3 THEN change integer.attribute to 0

The issue with this is that on the third click the second rule is triggered, so it doesn't cycle through properly. I've tried adding timers, but that doesn't seem to help.

Anyone have any thoughts? I would have thought this is pretty simple but I can't seem to crack it. I'm sure I'm missing something obvious!

Comments

  • imjustmikeimjustmike Member Posts: 450

    Worked it out. Changed the first rule to be:
    IF touch is released AND integer < 3 AND integer.attribute =/= 3 THEN change integer.attribute to integer.attribute+1

  • MentalDonkeyGamesMentalDonkeyGames Member Posts: 1,276
    edited February 2015

    Try this:

    If touch is released and integer = 4 change integer.attribute to 0
    or
    If touch is released and integer > 3 change integer.attribute to 0

    Hope this helps.

    edit: this might be better

    When touch is released, change attribute "integer.attribute" to mod(integer.attribute,3)+1

    this will cycle the integer from 1 to 3, and then back to 1 (1,2,3,1,2,3...)

    Mental Donkey Games
    Website - Facebook - Twitter

  • imjustmikeimjustmike Member Posts: 450

    @NipaDidIt that edit is insanely useful, didn't know gamesalad had that functionality. Thanks!

  • MentalDonkeyGamesMentalDonkeyGames Member Posts: 1,276

    That´s the nice thing about Gamesalad. You learn something new every day :smile:

    Mental Donkey Games
    Website - Facebook - Twitter

  • SocksSocks London, UK.Member Posts: 12,822
    edited February 2015

    @imjustmike said:
    Hey guys, I'm trying to have a button that triggers 4 different states (in this case, up, down, left, right) and I'd like to use an integer attribute to do that. To do that I'm looking to create a button that increases an integer attribute by one each time it is clicked until it reaches 3, then, on the next click bring it back to 0.

    Change X to mod(X+1,4)

  • imjustmikeimjustmike Member Posts: 450

    @Socks thanks buddy. Think I'm going to do some reading on mod() as it looks like a very useful function.

  • SocksSocks London, UK.Member Posts: 12,822
    edited February 2015

    @imjustmike said:
    Socks thanks buddy. Think I'm going to do some reading on mod() as it looks like a very useful function.

    Just so you know, it used to look like this:

    X%Y

    And now it looks like this:

    mod(X,Y)

    So don't get confused by the old way of doing it when you see it in examples, in fact the old way still works but is not recommend as it has been replaced with the new way which is said to be more future-proofed.

    So round(game.time)%7 will give you

    0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 . . .

    Which is the same as mod(round(game.time),7)

    . . . . . . . . . .

    And round(game.time)%4+1 will give you

    1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 . . .

    Which is the same as mod(round(game.time),4)+1

    . . . . etc

  • imjustmikeimjustmike Member Posts: 450

    @Socks thanks! A bastion of knowledge, as always

Sign In or Register to comment.