Page 1 of 1

[SOLVED] Store array as global variable

Posted: 03 May 2016, 12:51
by Woodruff
Hi folks!

Today I'm facing a problem about how to deal with a multiplayer phase. I'm in a multiplayer state where all players must choose a card to play first. Once all players make their decision, then all cards are meld in player board. It is very similar to the game mecanism in 6nimmt.

So I want to keep track of who will play which card using an array as a global variable. First I want that array empty at the setup (let's call it $chosen_cards). Then when a player make is decision and his move is valid on server side, I want to update that array like this:

Code: Select all

$chosen_cards[$player_id] = $card
(with $card an array representing the information about the chosen card). Then, when all players have made their choice, I jump to a game state where I read the contents of this array in order to make the play.

I've tried two things to do so so far:

#1: add 'chosen_card' as a global variable in self::initGameStateLabels and use the getter and setter: impossible because this mecanism can handle only scalar variables, not arrays :? .

#2: initialise the variable as $this->chosen_card in material.inc.php: seems awkward to me because this file is there to stoke game material information, not current situation. Anyway, this solution fails because the array seems re-initialized each time I want to update it to resolve player move.

I think I could design a specific table in database to handle this but I feel that solution very overwhelming.
What solution do I get left before I have to do that?

Re: Store array as global variable

Posted: 03 May 2016, 17:31
by pikiou
On BGA, all the data which persists during a game is kept into the database. Don't be shy to use it !
initGameStateLabels is just a shortcut to write scalars into the database.
Php variables are destroyed between http requests so $this->chosen_card is unfortunately not a solution.
Creating tables that will never have more than 10 rows is indeed a bit extreme, but it has to have benefits only admins know of ^^

I used to have only one MySQL table in which I loaded and stored serialized php variables, but admins told me it was bad practice.
Actually, my state functions were something like function stNextTurn(&$game, &$playerList, &$activePlayerIds) so game data and player data were automatically loaded and saved ^^
This solution took way too much work to create for its benefits, and admins have good reasons to ask us not to do this.

[SOLVED] Store array as global variable

Posted: 10 May 2016, 14:28
by Woodruff
Thanks for your reply Pikiou.

Understood I'll do that :)