Using a table in a loop

seveksevek Member, PRO Posts: 30
edited June 2014 in Working with GS (PC)

Hi guys.

So, I have this table.

...which contains 9 different Y-axis coordination.

Now, the first actor is spawned based on row 1, (90 pixel on Y-axis),
the second actor is spawned based on row 2, (110 pixel on Y-axis),
the third actor is spawned based on row 3, (130pixel on Y-axis).

... and so on. You get the point.

Now, I can individually spawn them, but that seems really inefficient. Especially since I want them to loop back to row 1 when the last row is used.

Any suggestion on how to write a rule so that y-axis coordination on row is used from the top to bottom and loop back to the first one?

Thanks!

Comments

  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited June 2014

    Hi @sevek Try the following:

    Make an integer attribute, call it Count, its value at the default zero. I think you've got the spawning in a Timer behaviour? If not, just don't use that and add your own triggering method you're using.

    Timer: Every ? seconds
    Rule: When Count <9 and >0
    Change Attribute Count to Count+1
    Spawn Behaviour Position y coord: TableCellValue(YourTable,Count,1)
    Otherwise
    Change Attribute Count to 1
    Spawn Behaviour Position y coord: TableCellValue(YourTable,1,1)
    

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • seveksevek Member, PRO Posts: 30

    Thank you Gyroscope once again! =)

    I think I am understanding your logic.

    Thanks! =)

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited July 2014

    @sevek‌

    In addition to the excellent approach that @gyroscope‌ gave, there are a couple other ways to do it.

    1)
    Make an index attribute.
    Timer every ? seconds Change Attribute: [ self.index ] To: [ (self.index %9)+1 ] Spawn actor -- position↑: [ tableCellValue(game.Table 1, self.index ,1) ]

    2)
    You don't need a table when the values go up by 20 every time. You could --

    Make a 'yValue' attribute.
    Timer every ? seconds Change Attribute: [ self.yValue ] To: [ max(90,(self.yValue %250)+20) ] Spawn actor -- position↑: [ self.yValue ]

  • seveksevek Member, PRO Posts: 30

    Thank you! =) @RThurman‌ .

    I will try that method as well! =)

    Appreciate it!

  • seveksevek Member, PRO Posts: 30

    @RThurman‌
    Hi.
    So I got it working using your method.
    Just quick question.

    What does "%250" do? what is the logic behind this?

    Thanks bunch!

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

    @sevek‌

    The " % " sign indicates a modulo operation. (You might want to look up what modulo is/does.)

    Perhaps a better way to write the expression is:
    max(90,mod(self.yValue+20,270))

    This form of the expression uses modulo the way GameSalad documentation wants it done.

  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited July 2014

    And to add, @sevek , to add to @RThurman's excellent advice, GameSalad is going to depreciate the % eventually and I think it was @CodeWizard recently who recommends the mod expression be always used instead now. :smile:

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • seveksevek Member, PRO Posts: 30

    Thanks guys! =)

    That really helped me to understand the concept!
    I have progressed so much further with you guys help! =)

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

    You are welcome!

Sign In or Register to comment.