Changing Values in $game_options

Game development with Board Game Arena Studio
Post Reply
User avatar
MikeIsHere
Posts: 137
Joined: 30 April 2020, 22:52

Changing Values in $game_options

Post by MikeIsHere »

What is the best practice for changing the values of $game_options

I would like to change this

Code: Select all

    100 => array(
            'name' => totranslate('Game length'),
            'values' => array(                    
                    1 => array( 'name' => totranslate( 'Standard game (121 points)' ) ),
                    2 => array( 'name' => totranslate( 'Quick game (61 points)' ) ),
                    3 => array( 'name' => totranslate( 'Debug game (31 points)' ) ),
            ),
            'default' => 1
        ),
Change the 1->121, 2->61, 3->31 It would take away some gnarly if statements. My game is in Alpha, and I know that it doesn't matter much, but I am also trying to learn what would happen if I was in production and how to handle this change with grace.
User avatar
RicardoRix
Posts: 2548
Joined: 29 April 2012, 23:43

Re: Changing Values in $game_options

Post by RicardoRix »

how about a lookup array. 1=> 121, 2 => 61, 3 => 31.
or more simply (121,121,61,31);
User avatar
Brainchild
Posts: 71
Joined: 31 January 2014, 19:42

Re: Changing Values in $game_options

Post by Brainchild »

You can also replace the hard-coded values in gameoptions.inc with PHP defines.

Code: Select all

// Variant IDs
if (!defined("OPTION_GAME_LENGTH")) // ensure this block is only invoked once, since it is included multiple times
{
    define("OPTION_GAME_LENGTH", 100);
        define("GAME_LENGTH_STANDARD", 1);
        define("GAME_LENGTH_QUICK", 2);
        define("GAME_LENGTH_DEBUG", 3);
}
And wrap it in a method in your.game.php.

Code: Select all

    function getNumPointsToWinGame()
    {
        $num_points_to_win_game = 121;

        switch ($this->gamestate->table_globals[OPTION_GAME_LENGTH])
        {
            case GAME_LENGTH_QUICK:
                $num_points_to_win_game = 61;
                break;

            case GAME_LENGTH_DEBUG:
                $num_points_to_win_game = 31;
                break;
        }
        
        return $num_points_to_win_game;
    }
No gnarly if-statements!
User avatar
Tisaac
Posts: 2743
Joined: 26 August 2014, 21:28

Re: Changing Values in $game_options

Post by Tisaac »

I can only emphasize the advice from Brainchild : you should use a lot of defines, that makes the code much easier to read.
Use defiines for gameoption, for states, for other enums, ...
User avatar
LaszloK
Posts: 36
Joined: 05 January 2018, 19:00

Re: Changing Values in $game_options

Post by LaszloK »

Like others said, leverage associative arrays and set the key to be the value you want. In Tarokk I used an option like this:

Code: Select all

        'name' => totranslate( 'Game length' ),
        'values' => [
            4 => [ 'name' => totranslate( 'Very short - 4 hands' ) ],
            8 => [ 'name' => totranslate( 'Short - 8 hands' ) ],
            12 => [ 'name' => totranslate( 'Medium - 12 hands' ) ],
            20 => [ 'name' => totranslate( 'Long - 20 hands' ) ],
            32 => [ 'name' => totranslate( 'Very long - 32 hands' ) ]
        ],
Then get the value of the option and use it directly as the number of hands to play (or in your case, points to win).
User avatar
MikeIsHere
Posts: 137
Joined: 30 April 2020, 22:52

Re: Changing Values in $game_options

Post by MikeIsHere »

Then get the value of the option and use it directly as the number of hands to play (or in your case, points to win).
This is what I want to move towards but since I already implemented as 1 2 3. So my question is how can I change my array and still support backwards compatibility
User avatar
Tisaac
Posts: 2743
Joined: 26 August 2014, 21:28

Re: Changing Values in $game_options

Post by Tisaac »

The easiest would probably be to define a custom method with a lookup array inside :

Code: Select all

public function getGameLength(){
  $option = self::getGameStateValue('gameLength');
  $tab = [1 => 121, 2 => 61, 3 => 31];
   return $tab[$option]; // Maybe add some check here
 }
Post Reply

Return to “Developers”