How to randomly select a row from a table and then grab the values of column 1 and column 2
revoltandreverb
Member Posts: 159
This works, but it's always the 1st row
random(tableCellValue( game.drop_table ,1,1),tableCellValue( game.drop_table ,1,2))
Below I've tried to randomise the row, but I guess using tableRowCount twice means the rows will never match.
random(tableCellValue( game.drop_table ,tableRowCount( game.drop_table ),1),tableCellValue( game.drop_table ,tableRowCount( game.drop_table ),2))
Thanks!
Comments
Let me make you a quick demo. Back in a moment...
My GameSalad Academy Courses! ◦ Check out my quality templates! ◦ Add me on Skype: braydon_sfx
See Braydon's response. It's better.
My Latest GS Game - Tiny Spirit
My First GS Game - Dashing Ralph
Here you go.
My GameSalad Academy Courses! ◦ Check out my quality templates! ◦ Add me on Skype: braydon_sfx
I can't open your file (maybe because I am on windows), but would this work?
Create an integer as a self.random row. Then you have this code:
Change attribute self.random row to random(1, tableRowCount(game.table1)
Change attribute (whatever you'd like) to tableCellValue(game.table1, self.random row, 1)
Will it work with the random(1, tableRowCount(game.table1) or would I need another pair of brackets around the tableRowCount?
random(1, tableRowCount(game.table1)
that's missing a end ')'
it should be
random(1, tableRowCount(game.table1))
Send and Receive Data using your own Server Tutorial! | Vote for A Long Way Home on Steam Greenlight! | Ten Years Left
Thanks for hijacking the thread @ImNiklas
@Braydon_SFX that worked a charm, but I would like to learn and understand. I ended up using the 2 column variables with self.Position.X - random( self.Col1Value,self.Col2Value ) for dropping an object from the top of the screen.
Looking at your template. You created a variable called {WhichRow} then created a variable called {Col1Value} that used {WhichRow} and ditto for {Col2Value}. I created these on the actor that drops from the top of the screen, as the first 3 behaviours, is that the correct way, or should it be another actor specially for holding those variables?
Is there a specific reason that the 3 variables were actor attributes and not game attributes. My logic tells me they need to be actor attributes, so that when the actor dies and is spawned again, the {WhichRow} attributes is" renewed"? But at first I made them game attributes...
The last thing I would like to do is not have the same row selected twice in a row. I'm assuming that I would need to created a new attribute {check_row_number} and when a random row is genereated with {WhichRow}, save {whichrow} into a key called {check_row_number} and the next time {whichrow} is generated, first check if it's equal to {check_row_number} if it is, generate another {whichrow}, if it's not equal, jus continue, but I'm not sure how to go about it.
Thanks!
If each actor will be dropping, then yes, you want those attributes to be self attributes.
Okay, so you want it to pick a random row, but not the previous random row..Okay, back in a mo (maybe more), I'll try and figure that out...
My GameSalad Academy Courses! ◦ Check out my quality templates! ◦ Add me on Skype: braydon_sfx
Thanks! No rush.
What's the reason they should be self attributes and not game attributes?
Okay! Here you go. This was a little more difficult when using tables, but... Hey, there are always workaround, even if you don't like the work arounds.
Okay, this does exactly the same thing except it grabs a random row, but will not pick the row it picked previously. Click on the white actor on screen to pick a new row. Hopefully you can tweak this to fit your game.
Best,
Braydon
My GameSalad Academy Courses! ◦ Check out my quality templates! ◦ Add me on Skype: braydon_sfx
This is poetry. It's very intense, I'll have to wrap my head around it first and get back to you
Thanks again!