Beginner’s Guide to Boardgame.io (Part 5): Stages
Now that we have phases down, which allow boardgame.io to have different behaviors and available moves at different points of the game, we can also introduce stages, which are similar to phases, but instead allow for different behaviors and available moves at different points of a player’s turn.
Default Behavior and what Stages are used for
By default, each player can only make moves during their own turn — while they are the currentPlayer.
However, in board games, there are situations where decisions and moves have to be made by players during other players turns.
For example, imagine a card game where a player is able to require all player to discard a card from their hand. Since these discards are not linear and the decision to discard can happen at different points, they cannot be considered separate turns.
Using stages, we can set the game to a stage where each player (other than the currentPlayer
) can use the discardCard move. Then, when everyone has discarded, the stage can return to the currentPlayer
or move to the next turn as needed.
Defining Stages
First, here is the basics of how to declare stages. You set a stage within the ‘turn’ key in the game object. There, you can set a stage object that has stage key-value pairs, where the key is the name of the stage the and values are objects with a { moves: {moveA, moveB}} setup.
Setting the Stage
You can transition stages in a similar way to transition phases. There are two a main ways to transition the stage. The first way is to simply setStage:
setStage('discard');
The above code will set the stage to the stage with the name of ‘discard’.
Alternatively, you can set a stage order with the ‘next’ property name:
stages: {
A: { next: 'B' },
B: { next: 'C' },
C: { next: 'A' },
}endStage();
Then, with the next stage defined for each stage, you can call endStage() in the board game code to move to the next stage.
setActivePlayers
In the example I spoke above in the beginning of the article, you want to move multiple players to another stage. To do so, you can use the setActivePlayers event.
Using this event, you can set the current player to a specific stage, with all other players to another stage. You can also set all players to a specific stage instead or each player (by player number) to a specific stage.
You can set move limits for the active players that will disallow moves after a specific number. The revert property name is then used to revert players back to the previous stage after moveLimits are exhausted or all players have exited the stage using endStage. You can also manually set the nextStage instead of reverting to the previous stage using the ‘next’ property name.
Conclusion
This is definitely the tip of the iceberg for stages. Stages are very flexible and have a lot of use, because they allow for much more intricate turns for board games with a lot of different decision points. For example, using stages, you can implement a reaction system in a board game, where players can respond to moves with other moves. Stages are definitely a valuable building block for board games of all types.