Game Loop¶
As every game functions generally in a loop, the game loop is the core of your Scorbit integration. This is where you'll update game state and send data to the Scorbit cloud service. The game loop should run continuously updating scores, modes, and player states, and committing changes at the end of each frame.
Call commit() unconditionally
IMPORTANT: call commit() unconditionally at the end of each cycle
Basic Structure¶
// Initial setup before entering to game loop
sb_config_t config = sb_config_create();
sb_config_set_provider(config, "your_provider");
sb_config_set_machine_id(config, 1234);
sb_config_set_game_code_version(config, "1.0.0");
sb_config_set_hostname(config, "staging");
sb_config_set_encrypted_key(config, "your_encrypted_key_here");
// ... set other config options as needed
// Create game state object from config
sb_game_handle_t state = sb_create_game_state(config);
sb_config_destroy(config);
// Main game loop
while (1) {
if (game_finished()) {
sb_set_game_finished(state);
}
if (game_started()) {
sb_set_game_started(state, SB_GAME_STARTED_BY_BUTTON);
}
// Update scores, modes, and player states using sb_set_score, sb_add_mode, etc.
update_game_state(state);
...
// IMPORTANT: call commit unconditionally at the end of each cycle
sb_commit(state);
}
// Cleanup when done. However, this is usually not reached in a game loop.
sb_destroy_game_state(state);
void game_loop() {
// Initialize game state with Config builder
scorbit::Config config;
config.setProvider("your_provider")
.setMachineId(1234)
.setGameCodeVersion("1.0.0")
.setHostname("staging")
.setEncryptedKey("your_encrypted_key_here");
// ... set other config options as needed
// Create game state object from config
auto gs = scorbit::createGameState(config);
// Main game loop
while (1) {
if (game_finished()) {
gs.setGameFinished();
}
if (game_started()) {
gs.setGameStarted(scorbit::GameStartOrigin::StartButton);
}
// Update scores, modes, and player states using gs.setScore, gs.addMode, etc.
update_game_state(gs);
// IMPORTANT: call commit unconditionally at the end of each cycle
gs.commit();
}
}
def game_loop():
# Initialize game state with Config builder
config = scorbit.Config()
config.set_provider("your_provider")
config.set_machine_id(1234)
config.set_game_code_version("1.0.0")
config.set_hostname("staging")
config.set_encrypted_key("your_encrypted_key_here")
# ... set other config options as needed
# Create game state object from config
gs = scorbit.create_game_state(config)
# Main game loop
while True:
if game_finished():
gs.set_game_finished()
if game_started():
gs.set_game_started(scorbit.GameStartOrigin.StartButton)
# Update scores, modes, and player states using gs.set_score, gs.add_mode, etc.
update_game_state(gs)
# IMPORTANT: call commit unconditionally at the end of each cycle
gs.commit()
Key Concepts¶
1. Initialization (before entering main loop)¶
- Configure a
Configobject with provider name, machine ID, and encrypted key - Create a game state object at startup from the config
- Set up any necessary callbacks (e.g., logging, key persistence, events)
2. State Updates¶
- Update player scores
- Manage active modes
- Track current ball and player
- Handle game start/end conditions
3. Commit Frequency¶
- Call
commit()at the end of each frame / cycle. Changes are only sent if state has been modified
4. Status Monitoring¶
- Check connection status periodically (see Pairing & Status)
- Update UI indicators based on connection state
Example Integration¶
Here's a more detailed example showing common game state updates, including scores, players, modes and ball tracking:
void update_game_state(sb_game_handle_t state) {
if (is_game_ending()) {
sb_set_game_finished(state);
}
// Handle game session state
if (is_game_starting()) {
sb_set_game_started(state, SB_GAME_STARTED_BY_BUTTON);
}
if (is_game_active()) {
// Update current player and ball
sb_set_active_player(state, current_player);
sb_set_current_ball(state, current_ball);
// Update scores
sb_set_score(state, 1, player1_score, 0);
// Manage modes
if (multiball_active) {
sb_add_mode(state, "MB:Multiball");
}
}
}
void update_game_state(scorbit::GameState& gs) {
if (is_game_ending()) {
gs.setGameFinished();
}
// Handle game session state
if (is_game_starting()) {
gs.setGameStarted(scorbit::GameStartOrigin::StartButton);
}
if (is_game_active()) {
// Update current player and ball
gs.setActivePlayer(current_player);
gs.setCurrentBall(current_ball);
// Update scores
gs.setScore(1, player1_score);
// Manage modes
if (multiball_active) {
gs.addMode("MB:Multiball");
}
}
}
def update_game_state(gs):
if is_game_ending():
gs.set_game_finished()
# Handle game session state
if is_game_starting():
gs.set_game_started(scorbit.GameStartOrigin.StartButton)
if is_game_active():
# Update current player and ball
gs.set_active_player(current_player)
gs.set_current_ball(current_ball)
# Update scores
gs.set_score(1, player1_score)
# Manage modes
if multiball_active:
gs.add_mode("MB:Multiball")
For complete examples, refer to the Scorbit SDK examples which demonstrate a full game loop implementation in C, C++, and Python.