Ghost replays for racing and speedrun games
ghostreplayspeedrunserializationinterpolation
A ghost is a translucent replay of a previous run (your PB or someone else's) shown while you play. There are two ways to record one, and the right choice depends on whether your physics is deterministic.
State recording vs input recording
- Input recording (inputs + timestamps): tiny files, but only works if identical inputs produce bit-identical results everywhere. Dart cannot guarantee float determinism across VM, JS, and WASM, and any physics patch invalidates old ghosts.
- State recording (positions sampled at a fixed rate): simple, survives physics patches, a few KB per run. Run chose this. Input recording only becomes possible with a fixed-step simulation (Fixed-timestep game loop for browser games) and a physics version stamp.
Run's format (built v0.8.14)
- Sample at 10 Hz while the timer runs. Frame = time f32 + x f32 + y f32 + state u8 + flags u8 = 14 bytes; about 4 KB for 30 s, so no compression needed.
- Header: version, level id, username, total time, frame count. Format v2 appended a checkpoint section (count u16 + per checkpoint: id string + time f32); v1 still decodes.
- Playback: 35% opacity component behind the player, Catmull-Rom spline interpolation between samples, a running frame index for O(1) lookup instead of binary search per frame.
- Storage: base64 in local prefs for PBs; server stores it as a MEDIUMBLOB on the score row,
with
has_ghostin leaderboard rows and aGET /scores/{id}/ghostendpoint. - Further size reductions if needed: delta encoding, int16 fixed-point positions, keyframes for seeking.
Input recording when the sim is deterministic (seed + input log)
If a game is built deterministic from the start (fixed 60 Hz step, all randomness from one
seeded PRNG such as mulberry32, no wall-clock reads in the sim), input recording becomes
the cheap option and unlocks more than ghosts. Design worked out for the ~/dev/game
wireframe flight game ("VECTOR" pitch, July 2026):
- Replay = seed + input log. Delta-encode inputs per tick; a whole sortie fits in a
~2 KB string that can be pasted or shared.
- Daily seed: everyone gets the same spawn sequence that day, so scores are directly
comparable, a retention loop for free.
- Campaign missions = named seeds + scripted spawn tables, the same code path as the
endless mode.
- Build this early, while the sim is small. Every later system must stay deterministic
or old replays desync, so stamp replays with a sim version.
Checkpoint splits
- Store each checkpoint's time by a stable checkpoint id. When racing a ghost, show the delta under the timer for ~3 s: green ahead, red behind, fade the last 0.5 s.
- Bugs found: register checkpoints outside the "better than best" guard; count checkpoints across all dimensions from level data, not from currently loaded children.
Bugs worth remembering
- Start the recorder at the timer's current elapsed time, not zero, or partial-run ghosts get a corrupted timeline (hide the ghost until its segment starts).
- Key ghost and PB storage by a stable level identity. Run's legacy levels clobbered new levels' ghost slots because both were keyed by list index (fixed with a separate prefix and deliberately no fallback). See Game content data models (stable ids, schemas, serialization).
Ideas not yet built
Race-this-ghost buttons on the leaderboard, a pre-level ghost picker (your PB / #1 / browse), spectate mode, multi-ghost racing, and live ghost racing (Live ghost-racing multiplayer (design research)).
See also
Linked from
- Arcade game design toolkit (loops, enemies, scoring, structure)
- Fixed-timestep game loop for browser games
- Game content data models (stable ids, schemas, serialization)
- Live ghost-racing multiplayer (design research)
- Run (runrunrunrun.run), Justin's speedrun platformer
- Speedrun timers, medals, leaderboards, and anti-cheat