Using a specific set of numbers and rotating though them one by one.

GoatTeaBlendGoatTeaBlend Member, PRO Posts: 22

So I have a REAL attribute that represents 8 different colors, 1-8. I have 2 buttons that cycle though these colors (numbers) one at a time. Cycling up is working..1..2..3..4...ect, but going down takes the number to 0 and then is will not loop back to the number 8. Does anyone have an idea of resolving this issue. Thanks for your ideas.

Comments

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    Assuming the attribute name is "myNum," then to cycle up, the expression would be:
    Change Attribute:[ myNum ] To:[ (myNum + 1)%9 ]

    To cycle down, the expression would be:
    Change Attribute:[ myNum ] To:[ (myNum - 1)%9 ]

  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408

    @RThurman said:
    Assuming the attribute name is "myNum," then to cycle up, the expression would be:
    Change Attribute:[ myNum ] To:[ (myNum + 1)%9 ]

    To cycle down, the expression would be:
    Change Attribute:[ myNum ] To:[ (myNum - 1)%9 ]

    I've always heard that the % sign would be depreciated and you should use the MOD function instead. Been hearing that for years though so...
    but anyways, the same functions in the Mod format would look like:

    Change Attribute:[ myNum ] To:[ mod(myNum + 1,9) ]
    Change Attribute:[ myNum ] To:[ mod(myNum - 1,9) ]

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited March 2015

    @jonmulcahy -- Thats right!
    I have a bad habit of using the "%" operator instead of using the mod() function. Thanks for turning the thread toward a more appropriate solution.

  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408

    @RThurman said:
    jonmulcahy -- Thats right!
    I have a bad habit of using the "%" operator instead of using the mod() function. Thanks for turning the thread toward a more appropriate solution.

    I only started using mod() a few weeks ago :) I was a die hard % supporter until i forced myself to switch.

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    I've tried to quit several times. Honest! But each time I just fall back into the same bad pattern.

    I think its time for me to start attending some 7-step recovery program to get rid of this bad, bad habit.

    Anyone know when the local ModAnon meeting starts?
    :)

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

    +1> @RThurman said:

    Change Attribute:[ myNum ] To:[ (myNum + 1)%9 ]

    @jonmulcahy said:
    Change Attribute:[ myNum ] To:[ mod(myNum + 1,9) ]

    Both these will cycle around 0-8 rather than 1-8

    mod( myNum ,8)+1 will work.

  • SocksSocks London, UK.Member Posts: 12,822

    @jonmulcahy said:
    oops, you are right, I blindly followed RThurman !

    @RThurman said:
    I've tried to quit several times. Honest! But each time I just fall back into the same bad pattern.

    There are others, it might% not be obvious who they are%, but you% are not alone, many% of us suffer.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    As a mod, I only use % about 20% of the time.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    Let this be a lesson to all you young'uns out there.
    See what bad habits will get you.
    A totally derailed thread -- thats what it'l get you!

    Now repeat after me:
    Use mod() not %
    Use mod() not %
    Use mod() not %
    ...

Sign In or Register to comment.