Expand description
§GGRS
GGRS (good game rollback system) is a reimagination of the GGPO network SDK written in Rust.
Instead of the callback-style API from the original library, GGRS returns a list of GgrsRequests
for you to fulfill each frame.
§How It Works
Rollback networking lets your game run at full speed using only local input, predicting what remote players are doing. When real remote inputs arrive, GGRS detects any misprediction, rolls the game back to the last correct state, and re-simulates forward. To support this, your game needs to be able to:
- Save its state to a
GameStateCellon request. - Load a previously saved state from a
GameStateCellon request. - Advance by one frame given a set of player inputs.
GGRS handles everything else: input exchange, prediction, rollback scheduling, time synchronization, and desync detection.
§Quick Start
§1. Implement Config
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct Input { pub left: bool, pub right: bool, pub jump: bool }
pub struct GameState { /* ... */ }
pub struct GgrsConfig;
impl ggrs::Config for GgrsConfig {
type Input = Input;
type InputPredictor = ggrs::PredictRepeatLast;
type State = GameState;
type Address = std::net::SocketAddr;
}§2. Build a Session
let socket = UdpNonBlockingSocket::bind_to_port(7000).unwrap();
let mut session = SessionBuilder::<GgrsConfig>::new()
.with_num_players(2).unwrap()
.with_input_delay(2)
.add_player(PlayerType::Local, 0).unwrap()
.add_player(PlayerType::Remote("127.0.0.1:7001".parse().unwrap()), 1).unwrap()
.start_p2p_session(socket).unwrap();§3. Drive the Game Loop
Each tick, poll the network, drain events, then advance the frame:
session.poll_remote_clients();
for event in session.events() { /* handle GgrsEvents */ }
session.add_local_input(local_handle, my_input).unwrap();
match session.advance_frame() {
Ok(requests) => {
for request in requests {
// handle SaveGameState, LoadGameState, AdvanceFrame — in order
}
}
Err(GgrsError::PredictionThreshold) => { /* skip frame */ }
Err(e) => panic!("{e}"),
}§Session Types
| Type | Use case |
|---|---|
P2PSession | Main multiplayer session; connects peers directly. |
SpectatorSession | Watch a game without contributing input. |
SyncTestSession | Local determinism testing; no network required. |
All session types are constructed with SessionBuilder.
§Feature Flags
sync-send: AddsSend + Syncbounds toConfig,NonBlockingSocket, and session types. Enable this if you need to share sessions across threads.wasm-bindgen: Enables WASM support. Required when targetingwasm32with browser APIs.
§Further Reading
See the docs/ folder for guides on sessions,
the main loop, requests and events, time synchronization, and more.
Structs§
- Game
State Accessor - A read-only accessor for the
Tthat the user previously saved into aGameStateCell. - Game
State Cell - An
Arc<Mutex>that you cansave()/load()aTto/from. These will be handed to the user as part of aGgrsRequest. - Message
- A messages that
NonBlockingSocketsends and receives. When implementingNonBlockingSocket, you should deserialize received messages into thisMessagetype and pass them. - Network
Stats - Statistics about the quality of the network connection to a remote peer.
- P2PSession
- A
P2PSessionprovides all functionality to connect to remote clients in a peer-to-peer fashion, exchange inputs and handle the gamestate by saving, loading and advancing. - Predict
Default - An input predictor that always predicts that the next input for any given player will be the Default input, regardless of what the previous input was.
- Predict
Repeat Last - An InputPredictor that predicts that the next input for any player will be identical to the last received input for that player.
- Session
Builder - The
SessionBuilderbuilds all GGRS Sessions. After setting all appropriate values, useSessionBuilder::start_yxz_session(...)to consume the builder and create a Session of desired type. - Spectator
Session - Connects to a remote host in a peer-to-peer fashion without contributing input.
- Sync
Test Session - A session for verifying that your game logic is deterministic, without any network involvement.
- UdpNon
Blocking Socket - A simple non-blocking UDP socket tu use with GGRS Sessions. Listens to 0.0.0.0 on a given port.
Enums§
- Desync
Detection - Desync detection by comparing checksums between peers.
- Ggrs
Error - This enum contains all error messages this library can return. Most API functions will generally return a
Result<(), GgrsError>. - Ggrs
Event - Notifications that you can receive from the session. Handling them is up to the user.
- Ggrs
Request - Requests that you can receive from the session. Handling them is mandatory.
- Input
Status InputStatuswill always be given together with player inputs when requested to advance the frame.- Player
Type - Defines the three types of players that GGRS considers:
- Session
State - A session is always in one of these states. You can query the current state of a session via
current_state.
Constants§
- NULL_
FRAME - Internally, -1 represents no frame / invalid frame.
Traits§
- Config
- Compile time parameterization for sessions.
- Input
Predictor - An InputPredictor allows GGRS to predict the next input for a player based on previous input received.
- NonBlocking
Socket - Custom transport layer for GGRS sessions.
Type Aliases§
- Frame
- A frame is a single step of execution.
- Player
Handle - Each player is identified by a player handle.