Need help with this table loop
MattButlerStudios
Member Posts: 486
I am trying to find an efficient way to loop over a table and avoid repeating cell values. The below code is what I have come up with, but I am not really sure why it isn't working.
The table has 10 rows and 2 columns. The second column is used for true false values in which I will detect whether the cell has been used yet or not.
values.TBL = my table of values
game.randomNumber = value of the random row to be used
game.currentValue = the current table cell that is selected
-when touch (button) is pressed change attribute game.randomNumber to random(1,10).
-Loop until tableCellValue(values.TBL,game.randomNumber,2) = false
(inside the loop) change game.randomNumber to random(1,10)
-(after loop) change game.currentValue to tableCellValue(values.TBL,game.randomNumber,1)
-change table value (table) values.TBL, (row) game.randowRow, (column) 2, (value) false
Comments
Is there a reason you don't want to use the tried and true method of copying the table to a backup table (if needed) and then deleting each row as it's chosen?
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Here's a demo.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
@tatiang I wasn't aware of the copy table rule. I thought I was going to have to put a bunch of code together to achieve that. Stupid mistake. Thanks!
www.mbstudios.co | Free Stuff
No problem! Just thought I'd save you the trouble.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
@tatiang just one more question. If I am using a table with these rules but only want to use rows 1-30 what could I do to make that happen?
www.mbstudios.co | Free Stuff
The one table feature I wish was implemented is Copy Partial Table or just a section in the existing Copy Table behavior that allowed for a selection of rows.
But for rows 1-30, you'd just change attribute self.row to random(1,30). You can still copy the whole table unless you prefer not to.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
@tatiang Would there be a problem though if the row is getting deleted and then I still include it in the random value? My table has lots of data but only some needs to be utilized.
www.mbstudios.co | Free Stuff
Oh, sorry, yes it would be a problem. The solution is a bit more complex than that, then. And so maybe you DO need a separate column for keeping track of used rows. Or a Loop Over Table to copy values from part of one table to another. This is what gets tricky about having partial descriptions of issues. You had said your table had 10 rows, now more than 30 but with a subset of 30... so each new piece of information changes how the problem gets resolved.
Can you explain the purpose of having a larger table of which you only want to use a subset? Could you instead use multiple tables, each with 30 rows?
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
@tatiang The reason that I am using 1 table for all of the values is because I already have about 12 other tables in use. Each table represents a category and depending on a variable a certain range is selected inside of the category (table). If I could get the boolean table column to work that would be really great!
www.mbstudios.co | Free Stuff
So I decided to take a stab at creating a method for copying a partial table. It's not the boolean method you first described but it could help with this situation.
http://forums.gamesalad.com/discussion/comment/518535/#Comment_518535
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
@tatiang Thanks a lot for the template. I will try and incorporate it tomorrow.
www.mbstudios.co | Free Stuff
@tatiang Just wanted to let you know that I implemented the code you showed me and it works great! Thanks again!
www.mbstudios.co | Free Stuff
@MattButlerStudios Glad to hear it!
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User