How can I have an attribute loop between 3 numbers?

BenMakesGamesBenMakesGames Member Posts: 85
edited March 2014 in Working with GS (Mac)

If I have a change attribute function that reads, for example, as follows:

Change Attribute "TrafficLight.Attribute" to ("TrafficLight.Attribute+1")#3

it will Loop the values 0 1 2(because of the applied #3)

Does anyone know how to have it loop the values 1 2 3 instead of 0 1 2?

Comments

  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078

    this mite work, try it out.
    set your trafficlight attribute to 1
    then if you want the attribute to loop like youve mentioned do.

    timer, every 0.1
    change attribute "trafficlight.attribute" to ("trafficlight.attribute"+1)%3

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

    You're correct in noting that the mod(,3) aka %3 function results in values of 0, 1, or 2. To return values of 1, 2, 3, you just add one to the mod result: attribute%3+1 or mod(attribute,3)+1.

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

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

    If you look at the output of any equation as nothing more magical than a number, a question like how can I have 1,2,3 instead of 0,1,2 should be self-explanatory (as others have pointed out if you want 0,1,2 to become 1,2,3 simply add 1 to the value).

  • HopscotchHopscotch Member, PRO Posts: 2,782
    edited March 2014

    So you got me thinking, because here in europe the traffic lights go yellow before turning green, i.e. green, yellow, red, yellow, green, yellow, ... (I think you notice a pattern?)

    so I had to try and flipflop through a number series of 1,2,3,2,1,2,3

    might also be usefull for a state machine

    ("trafficlight.attribute" mod 4)+floor((("trafficlight.attribute"-1) mod 4) / 3) * 2

    Just for fun, can someone make this kind of cycle generic for any cycle length using a triangular waveform function?

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

    @Hopscotch nice! That could also be used for a waypoint actor that starts at position 1 (table x/y values), then moves to position 2, then to position 3, back to 2, back to 1, rinse and repeat.

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

  • BenMakesGamesBenMakesGames Member Posts: 85

    @tatiang said:
    Hopscotch nice! That could also be used for a waypoint actor that starts at position 1 (table x/y values), then moves to position 2, then to position 3, back to 2, back to 1, rinse and repeat.

    exactly what im working on. Thanks for the answer everybody. Ill get onto adding it in shortly.

  • HopscotchHopscotch Member, PRO Posts: 2,782
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited March 2014

    @Hopscotch said: Just for fun, can someone make this kind of cycle generic for any cycle length using a triangular waveform function?

    For 1,2,3,2,1,2,3,2,1 you can try:
    abs(mod( self.Time ,4)-2)+1

    For 1,2,3,4,3,2,1,2,3,4,3,2,1 try:
    abs(mod( self.Time ,6)-3)+1

    For 1,2,3,4,5,4,3,2,1,2,3,4,5 try:
    abs(mod( self.Time ,8)-4)+1

    For 1,2,3,4,5,6,5,4,3,2,1,2,3,4,5,6 try:
    abs(mod( self.Time ,10)-5)+1

    See the pattern?

    By the way, you don't necessarily need to use self.Time. Its just a useful built-in counter. You can use any means you want to create an incrementing counter.

Sign In or Register to comment.