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_device_info_t device_info = {
.provider = "your_provider", // Required: your provider name
.machine_id = 1234, // Required: your machine ID
.game_code_version = "1.0.0", // Required: version tracking
.hostname = "staging", // Optional: staging, production, or custom URL
.uuid = NULL, // Optional: will be auto-generated if NULL
.serial_number = 0 // Optional: device serial number
...
};
// Use encrypt_tool to generate your encrypted key from your private key
const char* encrypted_key = "your_encrypted_key_here";
// Create game state object with encrypted key
sb_game_handle_t state = sb_create_game_state2(encrypted_key, &device_info);
// Main game loop
while (1) {
if (game_finished()) {
sb_set_game_finished(state);
}
if (game_started()) {
sb_set_game_started(state);
}
// 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 your device info
scorbit::DeviceInfo info;
info.provider = "your_provider"; // Required: your provider name
info.machineId = 1234; // Required: your machine ID
info.gameCodeVersion = "1.0.0"; // Required: version tracking
info.hostname = "staging"; // Optional: staging, production, or custom URL
info.uuid = ""; // Optional: will be auto-generated if empty
info.serialNumber = 0; // Optional: device serial number
...
// Use encrypt_tool to generate your encrypted key from your private key
std::string encrypted_key = "your_encrypted_key_here";
// Create game state object with encrypted key
auto gs = scorbit::createGameState(encrypted_key, std::move(info));
// Main game loop
while (1) {
if (game_finished()) {
gs.setGameFinished();
}
if (game_started()) {
gs.setGameStarted();
}
// 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 your device info
info = scorbit.DeviceInfo()
info.provider = "your_provider" # Required: your provider name
info.machine_id = 1234 # Required: your machine ID
info.game_code_version = "1.0.0" # Required: version tracking
info.hostname = "staging" # Optional: staging, production, or custom URL
info.uuid = "" # Optional: will be auto-generated if empty
info.serial_number = 0 # Optional: device serial number
...
# Use encrypt_tool to generate your encrypted key from your private key
encrypted_key = "your_encrypted_key_here"
# Create game state object with encrypted key
gs = scorbit.create_game_state(encrypted_key, info)
# Main game loop
while True:
if game_finished():
gs.set_game_finished()
if game_started():
gs.set_game_started()
# 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 your device info with provider name and machine ID
- Create a game state object at startup with device info and encrypted key
- Set up any necessary callbacks (e.g., logging)
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);
}
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();
}
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()
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.