Record and replay gameplay by saving inputs instead of video. Determinism rules (fixed time step, seeded random numbers, no wall clock), a tested recorder that stores only input changes, playback with pause, speed and fast seeking through keyframes, state hashes to detect desync, server-side replay verification to catch forged scores, compact share codes (gzip and base64url) for links, ghost ra...
Download skill (.zip) Files on GitHubGame replay system
Save inputs per frame, not pictures. Re-running the same inputs through the same deterministic game gives the same result: a 3-minute run is a few KB, can be shared in a link, and can be checked by a server.
Make the game deterministic
- Fixed steps:
update(1/60)from an accumulator (the scaffold loop does this). Never step with the real frame time. - Seeded randomness:
const rng = seeded(seed)and userng()everywhere in game logic.Math.random()is fine for visual-only effects (particles), never for anything that changes the outcome. - No clocks or devices in logic: no
Date.now(),performance.now(), screen size or frame rate in rules. - Order: iterate arrays, not objects with changing keys; don’t depend on
Setorder after deletes. - Same code version: store
meta.versionand refuse replays from other versions (or keep old rules). - Math functions (
Math.sinetc.) can differ in the last digit between browsers. Fine for replays on the same browser; for cross-browser verification round state (hashStaterounds to 1e-6) or use integer and fixed-point positions.
assets/replay.js
import { seeded, createRecorder, createPlayback, verifyReplay, hashState, encodeReplay, decodeReplay } from "./replay.js";
const rec = createRecorder({ seed, meta: { game: "my-game", version: "1.2.0" } });
each fixed step: rec.record(input); state = step(state, input, rng);
const replay = rec.finish({ score: state.score, hash: hashState(state) });
const code = await encodeReplay(replay); // share: https://my.game/?replay=CODE
const pb = createPlayback(replay, { init: (seed) => ({ state: newGame(), rng: seeded(seed) }), step, keyEvery: 300 });
each frame: pb.update(realDt); draw(pb.state); pb.paused = true; pb.speed = 4; pb.seek(frame); pb.progress
Inputs are actions ({ left: true, jump: false }), recorded only when they change, so holding a key costs nothing.
Uses
- Ghost racer: play the best replay alongside the player, drawn see-through.
- Instant replay or kill cam: keep the last 10 s of inputs and a keyframe; play from it at 0.5x.
- Score verification: the server runs
verifyReplay(replay, { init, step })and only accepts the score when it matches. Run the same rules file in Node (shared/). - Bug reports: attach the replay code; developers replay the exact run. Pair with game-bug-diagnostics.
- Multiplayer: lockstep games and server-recorded matches replay the same way, with every player’s inputs per frame.
Tested
Node tests: 1,000 frames stored as about 16 input changes, verification passing, a forged score caught, playback ending in the same state hash, seeking backwards and forwards giving identical states, 2x speed, encode/decode round trip with URL-safe characters.
Pitfalls
Variable time steps, unseeded randomness, recording positions instead of inputs, replays from older game versions, and
reading Date.now() in game logic.