Loop through Table
ookami007
Member Posts: 581
Is there a way to specify the row or column in loop through table?
For instance, I want to loop through every row and in the behaviors I want to loop through every column in that row but there doesn't seem be a way to specify the row when cycling through columns.
Is that possible?
Comments
You using the change cell value function? In that you can pick the row and columns
Yes, but it looks like that's only for the starting row or the starting column, depending on what you picked to loop through.
So, if I pick columns... it will loop through the columns... I'm assuming on row 1, from the start column to the end column. At least that's what it appears.
It doesn't seem like there's anyway to actually specify a row if I choose columns or a column if I choose rows.
If you tell the "Loop over Table" behavior to loop through the columns, it does not actually care about any rows. All it does is return you an "index" of the column it is at during looping.
In the body of the loop, you then specify which row to deal with.
The same goes for the rows. If you tell it to loop over the rows, it returns a row index as it loops through, you then need to decide which columns of the current row you want to act on.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
Why not try tablesearch in combination with a function that searches columns instead of loop? It has the additional bonus of being instant in it's execution.
Yes, but I am actually looping through the entire table to build the level.
I tried putting a loop through table with columns inside a loop through table rows but the columns loop through table never actually fires.
Is that expected behavior?
I see. I'm not sure about it being expected behavior. Perhaps somebody well versed in loops can chime in.
Obviously, this can be done multiple ways but I thought a loop inside of a loop would be the fastest. ... you know... like a dream within a dream within a dream... (cue Inception music)
In the creator you cannot put a loop inside a loop, see the notes in the behaviour selector. @ookami007
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
@ookami007, there are many, many right AND wrong ways of doing this.
Also, probably in a more optimised and faster fashion than using the Loop-over-table behavior. It all depends on what your logic needs to do, how many rows you go through, how many columns, etc.
So we can't really give you pointers without knowing the purpose of the code.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
@Hopscotch, I think the idea is to loop through every table cell, which would require a loop inside of a loop. Essentially this:
For every row R
For every column C
Do something with tableCellValue(table,R,C)
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
@tatiang, yup, and as I mentioned two posts up, you cannot put a loop inside a loop, as he tried. As there are many other ways to do it, it would be best for @ookami007 to explain what he needs it for and how many rows and tables are involves.
I.e. it may be best and fastest to just loop over the intended rows and process the columns individually, instead of loop over them as well.
As loops take one code cycle (+-0.02seconds), looping over 10 rows and 10 columns will take 2 seconds, only looping over the rows and processing all columns directly would only take 0.2 seconds.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
Here's one way to do this. As was mentioned, the best method depends on various factors but at least you can see one method. This spawns a new actor for each table cell that then logs the table cell value.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
@tatiang I am generating a random level in a table and then drawing using loops.
I haven't looked at your project yet, but I suspect you're doing it the manual way I am doing it... which is:
Set col = 1
Set row = 1
Loop until done = true
Get tablecellvalue row,col
Spawn actor based on value
increment col, if col is greater than maxcols, then
set col to 1
increment row
if row greater than maxrows, then done = true
It draws it just fine, but I was looking for a faster way to render it, since there will be a good deal of cells to go through and I'm sure the players aren't going to want to wait 20 seconds to start the level.
You will find a hard limit on the number of actors you can spawn in a given time. So no matter how fast you loop through the table, it's going to take a few seconds to spawn 100+ actors. Using @Hopscoth's method I had it checking all 400 cells in my table in about 0.6s, but adding 400 spawns will add a lot of time to that.
Contact me for custom work - Expert GS developer with 15 years of GS experience - Skype: armelline.support
@ookami007 Yes, that's the general method I'm using. To read through 1000 cells (20 columns, 50 rows) and spawn an actor using the data from each cell takes several seconds, though. Spawning recursive actors is always a bit dicey when the numbers get that large.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
My method is working... I'll just have to see if I can figure a way to speed it up. Obviously, I can display some sort of loading screen - maybe with some random tips but the faster the better.
I have it generating quasi-random dungeons at the moment. I have the dungeon split into 6 sections and it randomly generates one of x templates for each section. The more variations I have, the more random it will seem.