How to randomly select a row from a table and then grab the values of column 1 and column 2

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

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273

    Let me make you a quick demo. Back in a moment...

  • stueynetstueynet TorontoMember Posts: 166
    edited March 2014

    See Braydon's response. It's better.

    My Latest GS Game - Tiny Spirit
    My First GS Game - Dashing Ralph

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273

    Here you go.

  • ImNiklasImNiklas Member Posts: 103

    @Braydon_SFX said:
    Here you go.

    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?

  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408

    random(1, tableRowCount(game.table1)

    that's missing a end ')'

    it should be

    random(1, tableRowCount(game.table1))

  • revoltandreverbrevoltandreverb Member Posts: 159

    Thanks for hijacking the thread @ImNiklas :wink:

    @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!

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273

    @revoltandreverb‌ said: "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..."

    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...

  • revoltandreverbrevoltandreverb Member Posts: 159
    edited March 2014

    Thanks! No rush.

    @Braydon_SFX said:
    If each actor will be dropping, then yes, you want those attributes to be self attributes.

    What's the reason they should be self attributes and not game attributes?

  • Braydon_SFXBraydon_SFX Member, Sous Chef, Bowlboy Sidekick Posts: 9,273

    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

  • revoltandreverbrevoltandreverb Member Posts: 159

    This is poetry. It's very intense, I'll have to wrap my head around it first and get back to you :smile:

    Thanks again!

Sign In or Register to comment.