Opening a Game Session¶
At the start of a game, we must create a new session and initializes the game state. This is typically done when a new game begins or after the previous game ends.
Basic Usage¶
Key Concepts¶
1. Session Initialization¶
- Marks the beginning of a new game session
- Automatically resets all game state values
- Sets Player 1 as the active player by default
- Sets ball 1 as the current ball
2. Initial State Configuration¶
- Can modify initial state before first commit
- Set custom starting player if needed
- Configure initial ball number if different from default
- Add any starting modes or conditions
3. Session Activation¶
- Must call
commit()
to activate the session - Session isn't active until committed
- Only one active session allowed at a time
Example with Multiple Players¶
For simplicity, all players are added at once in a single cycle. Normally, they are added individually across multiple cycles.
Here's a more detailed example showing how to start a game with multiple players:
void start_multiplayer_game(sb_game_handle_t state, int num_players) {
// Start the game session
sb_set_game_started(state);
// Initialize all players with zero scores
for (int player = 1; player <= num_players; player++) {
sb_set_score(state, player, 0, 0);
}
// Set initial game state
sb_set_current_ball(state, 1);
sb_set_active_player(state, 1);
// Commit to activate the session
sb_commit(state);
}
void start_multiplayer_game(scorbit::GameState& gs, int num_players) {
// Start the game session
gs.setGameStarted();
// Initialize all players with zero scores
for (int player = 1; player <= num_players; player++) {
gs.setScore(player, 0);
}
// Set initial game state
gs.setCurrentBall(1);
gs.setActivePlayer(1);
// Commit to activate the session
gs.commit();
}
def start_multiplayer_game(gs, num_players):
# Start the game session
gs.set_game_started()
# Initialize all players with zero scores
for player in range(1, num_players + 1):
gs.set_score(player, 0)
# Set initial game state
gs.set_current_ball(1)
gs.set_active_player(1)
# Commit to activate the session
gs.commit()
Note
Starting a new game while another game is in progress will automatically end the current session and start a new one.
Warning
Make sure to call commit()
after setGameStarted()
. The session won't be active until committed.
Tip
You can set initial scores, modes, or other state variables before the first commit. These will be included in the initial session state.