game-state-sync
Keeps a multiplayer game's world consistent between an authoritative server and its clients: choosing a sync strategy (dumb client, lockstep, snapshot…
by fcsouza★ 29 GitHub stars
Install game-state-sync in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
Game State Sync
The server owns the game state. Clients send inputs, never results. Every technique below hides latency on top of that rule; none of them replaces it.
Prerequisites: a server tick loop and authority model (game-backend-architecture). For fanning deltas out across several server processes, see the fan-out section of game-backend-architecture.
Pick a sync strategy
Decide this first. Most games combine rows: prediction for the local player, interpolation for everyone else, lag compensation for hits.
| Game situation | Strategy | Cost and limits | Boilerplate |
|---|---|---|---|
| Turn-based or slow (strategy, cards, board games) | Dumb client: send input, wait for the server's new state, render it | Input lag equals round-trip time. Fine when turns are slow | Not needed; skip prediction |
| Your own avatar in a real-time game | Client prediction plus server reconciliation: apply input locally, tag it with a sequence number, replay unacknowledged inputs on each server state | Mispredictions cause corrections; smooth small ones, snap large ones | StateSyncEngine.reconcile, ClientGameLoop.handleServerUpdate |
| Other players whose speed and direction change instantly (shooters) | Entity interpolation: render them in the past, between two received snapshots | They appear about one interpolation period behind. Source defaults to 100 ms (cl_interp 0.1) |
RollbackBuffer.getBracket over received snapshots |
| Entities with predictable motion (vehicles, ships) | Dead reckoning: extrapolate from last speed and heading, correct on the next update | Wrong when motion changes. Cap how long you extrapolate | Pattern only, see references/client-architecture.md |
| Hit detection against entities shown in the past | Lag compensation: the server rewinds the world to the shooter's view time and tests the hit there | A target can be hit just after reaching cover | Not implemented; use RollbackBuffer for the history |
| 2-4 players, simulation you can make deterministic | Deterministic lockstep: send inputs only | Bandwidth independent of object count. Everyone waits for the slowest player's input; float differences break it | Not implemented |
| Physics-heavy co-op with no competitive stakes | Distributed authority with state synchronization: each player owns the objects it touches | Less secure; only for non-competitive play | Not implemented |
The preset values in templates/sync-strategies.ts (tick rates, byte budgets, buffer sizes) are starting points to measure against, not benchmarks.
Procedure
1. Define the state shape
Keep entities in Record<id, entity>, not arrays. An insert into an array shifts every later index and the whole tail diffs as changed.
interface MyGameState extends Record<string, unknown> {
tick: number;
players: Record<string, { x: number; y: number; vx: number; vy: number }>;
}
2. Run the authoritative server loop
import { StateSyncEngine, type PlayerInput } from './state-sync';
const engine = new StateSyncEngine<MyGameState>(initialState, {
tickRate: 20,
snapshotInterval: 1,
historyLength: 64,
interpolationDelay: 2,
});
// Each server tick. With snapshotInterval: 1, tick() already stores this
// tick in the history; snapshot() replaces that entry (the buffer keeps one
// entry per tick) and returns { tick, state, timestamp } to send.
engine.tick((state, tick) => simulate(applyInputs(state, pendingInputs), tick));
const snap = engine.snapshot();
Send each client the new state as a delta (step 3), with the sequence number of the last input processed for that client.
3. Encode deltas against what the client has acknowledged
Over an unreliable transport such as UDP, encode each client's delta against the last snapshot that client acknowledged, not against the previous tick. A client that lost the previous packet cannot apply a delta built on it. Keep a per-client acknowledged tick and a history of sent snapshots (RollbackBuffer). Send a full snapshot on join and after sustained packet loss.
Over WebSocket, deltas against the previous tick are safe: WebSocket runs on TCP, which delivers every message in order, so the client always holds the previous state. This is what game-backend-architecture does. Resend a full snapshot when a client reconnects.
import { encodeDelta, decodeDelta } from './delta-encoder';
const delta = encodeDelta(ackedBaseline, currentState); // server, per client
const next = decodeDelta(ackedBaseline, delta); // client, same baseline
toBinaryDelta only frames the JSON delta in a byte buffer. It does not compress. Quantize floats yourself if bandwidth matters.
4. Predict and reconcile on the client
clientTick is your own counter, advanced once per fixed step (step 7). applyLocalInput does not advance the engine's tick.
let sequence = 0;
const input: PlayerInput = { tick: clientTick, playerId, actions, sequence
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
- boilerplate/delta-encoder.ts
- boilerplate/game-loop-client.ts
- boilerplate/rollback-buffer.ts
- boilerplate/state-sync.ts
- references/client-architecture.md
- references/sources.md
- templates/sync-strategies.ts
FAQ
What does the game-state-sync skill do?
Keeps a multiplayer game's world consistent between an authoritative server and its clients: choosing a sync strategy (dumb client, lockstep, snapshot interpolation, client prediction with server reconciliation, lag compensation), plus TypeScript boilerplate for delta encoding, a rollback history buffer and a fixed-timestep client loop. Use when a multiplayer game feels laggy on input, shows remote players stuttering or teleporting, drifts out of sync between clients, or sends too much state per tick.
How do I install the game-state-sync skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the game-state-sync skill run scripts?
Yes, this skill bundles executable scripts. Review the source before installing.
Related skills
mcp-builder
AnthropicGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
claude-api
AnthropicReference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
algorithmic-art
AnthropicCreating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
mcp-integration
AnthropicThis skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…