Question about getDoubleKeyCollectionFromDb()

Game development with Board Game Arena Studio
Post Reply
User avatar
IndianaScones
Posts: 19
Joined: 07 June 2012, 07:04

Question about getDoubleKeyCollectionFromDb()

Post by IndianaScones »

I'm going through the Reversi tutorial and stuck on the following:

Code: Select all

// Get the complete board with a double associative array
    function getBoard()
    {
        return self::getDoubleKeyCollectionFromDB( "SELECT board_x x, board_y y, board_player player
                                                       FROM board", true );
    }
Since bSingleValue is true, the result should only contain the third column. So in the above, it's just a single column db with player?

The code is then used as follows:

Code: Select all

if( $board[ $x ][ $y ] === null ) // If there is already a disc on this place, this can't be a valid move
How is [ $x ][ $y ] being used to find the player value if the result of getBoard() only contains the third column? It all makes sense to me except for the second arg for getDoubleKeyCollectionFromDb().
User avatar
tchobello
Posts: 526
Joined: 18 March 2012, 13:19

Re: Question about getDoubleKeyCollectionFromDb()

Post by tchobello »

hi, you should have a look at getCollectionFromDb().

It works quite the same way and there are more explanations on the wiki.
User avatar
robinzig
Posts: 415
Joined: 11 February 2021, 18:23

Re: Question about getDoubleKeyCollectionFromDb()

Post by robinzig »

So this method returns an array whose keys are the `x` values, and whose values are themselves arrays. Those "second level" arrays have keys as the `y` values, and values which are the arrays of player values. Hence using `$board[$x][$y]` to access those values.

Had the `bSingleValue` argument been `false`, that array of player values would in fact have been an array of further associative arrays, each containing just a single key `player` with the player value in question. It does this so you can use the same method with queries for more than 3 columns, and get the complete rest of the data in this "third level" array. But usually when there's just a single third column you're interested in, you don't want extra "noise" of extracting the single value from this array, so that's what that argument is there for.

Hope this makes sense. If it doesn't from a text explanation, try just `var_dump`-ing the value returned by this call (and perhaps also try doing that when changing the `true` argument to `false` and compare the two) and it should make sense.
Post Reply

Return to “Developers”