I need a briefing on Loop Over Table

MoikMoik Member, PRO Posts: 257

What does the Store Index In Attribute do/how do I format one? All I could find in the cookbook about it simply says what rules you can't use it with, not how to use it. I'm guessing Store Index In Attribute is how the behaviour decides which row to run the loop over? Do I just make an Integer attribute with a 1 in it or something?

Use Case is simply that I want to have the value of each cell in a column go down by 1 across 15~20 rows. I may also want to run a max(stat,0) across all the cells to make sure they don't go negative.

I've read elsewhere that Loop Over Table can be slow. This will be occuring on a core loop function/running once maybe every 3 seconds off a player tap/click. Not sure if I should look at other options, or what those options would be.


  • HopscotchHopscotch Member, PRO Posts: 2,782
    edited November 2015

    @Moik how many rows do you have? The loop behavior does one step every draw cycle. This means that cycling through a table with 50 rows will take about 1 second, 0.02s for each row.

    In stead of looping over the table to check for max(stat,0), make sure your values are stored correctly at the time of writing to the table.

    The Index is just the current row the loop is pointing at. This allows you to then make changes to, or read from, the current row.

    If your table has 5 rows, then the code inside the loop will be executed 5 times. Each time, the index will contain the value of the current row (1, 2, 3, 4 and then 5).

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

    The index attribute should be an Integer type with the value 0 (the starting value doesn't matter because the Loop Over Table behavior will automatically determine the necessary values during the loop, as @Hopscotch explained above).

    Personally, I wouldn't use a loop for either of your descriptions. To subtract one from every cell, consider using a Change Table Value behavior (although this would require that your values are in columns, not rows). To set minimum values for table cells, do this wherever you change their values. For example, if you currently are changing a table cell to tableCellValue(tableName,row,col)-25, then you would instead change it to max(tableCellValue(tableName,row,col)-25,0).

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

  • MoikMoik Member, PRO Posts: 257
    edited November 2015

    Thanks guys. A lot of good calls there. Will definitely switch to using max before the calc for clean-up as needed. The loop is a lot slower than I was expecting it to be. Wasn't expecting a "back-end" black box function to be locked to framerate.

    @tatiang Did you have an idea for something procedural using Change Table Value, or are you suggesting simply adding Column cells into one Change Table Value until I have one action per cell? Would it be bad if I simply added as many separate Change Table Value rules as I needed to do them one by one? (The data is 15 rows in a single column, if I change it now I'll have to go back and re-do existing UI and calcs which are already mapped to reading up and down a column).

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

    I was suggesting a bunch of Column changes in a single Change Table Value behavior. I haven't tested the speed of that so you'd need to see how it work for you. There are MUCH faster looping methods (search for Hyper Loop or Fast For Loop) if you need them. And yes, if you prefer you keep everything in rows, you could have that many Change Table Value behaviors each with a single row change. I honestly don't think 15 of those would be a problem. 500 might. ;)

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

Sign In or Register to comment.