Skip to content

Development source guide ยท workspace 0.1.0

Saves, snapshots and replays

Decide what kind of persistence you need before choosing a mechanism. A player's saved progress, a debugging snapshot and a repeated input script solve different problems. The engine does not make them equivalent automatically.

Choose the right kind of state

Need Suitable mechanism Important limit
Player progress across sessions Game-integrated save storage and explicit slot/UI policy The game must serialize the state it needs and decide when to write it.
Restore a debugging situation A game-defined state/load hook Completeness depends on the hook; the generic fallback is deliberately narrow.
Repeat a short interaction A harness command script, ideally starting from a known state No general recording, seek, rewind or full replay-history service is supplied.
Player preferences or profile-like settings The game's explicit settings/player storage A stored preference still needs to be applied when loading.

The generic harness has state and load, but no universal save, quicksave, slot-selection or autosave command. Those are game features if the game implements them.

Understand the default debugging snapshot

If the game does not override the state hook, the standard application snapshot includes a title-derived game identifier, tick, entity count and rows of existing transform positions. Positions are rounded to three decimal places.

The default load restores tick and positions of existing objects. It does not reconstruct the scene or restore every component. In particular, do not rely on it to restore rotations, scales, physics bodies, random-generator state, held input, camera or interface state.

Warning. The fallback is not a safe arbitrary-state interchange format. It does not enforce the same game-identity check as the dedicated save API, and a failed load can occur after some earlier changes have already happened. Use a known snapshot for the intended game instance; restart a disposable test if a restore leaves it uncertain.

To inspect a snapshot, enable the harness as described in inspection and request:

AIGameEngine ctl ./first-game state

Read the response before treating it as valid JSON or a complete save. A game-specific state hook can return a different, richer representation.

Restore through the harness carefully

The protocol form is load followed by the inline JSON itself. A filename is not accepted as an instruction to read a file. If you keep snapshots in files, your own tooling must read the file and send its accepted JSON as the command payload.

Pause first, use the game's expected schema and inspect the reply and resulting state. The load command does not automatically resume a paused game. Send resume explicitly if you intend to continue.

Do not build a shell command by evaluating snapshot contents. Treat saved JSON as data. Also avoid the CLI's semicolon-chain shorthand for JSON containing semicolons: that splitter does not understand JSON strings.

A custom game can provide validation, more complete restoration and input cleanup. Those are properties of its implementation, not promises made by the default snapshot.

Integrate player saves

The save library offers binary slots, JSON snapshots and player JSON through Rust APIs. A game must choose its game identifier, slot policy, component codecs and save/load actions.

Native storage normally uses a per-game folder under the appropriate user data location. AIGAME_SAVE_DIR can select a different save root. Changing the root changes where the game looks; it does not migrate earlier files automatically.

Slot stems must be nonempty, at most 64 bytes, and use ASCII letters, digits, underscores or hyphens. Player storage adds player- before validating that stem, leaving at most 57 bytes for a supplied player ID. Apply a nonempty-player-ID rule in your game UI as well; storage's prefix alone does not enforce it.

The mechanisms use different files:

Mechanism Native filename pattern Detail
Binary slot slot.sav Includes format/game/checksum checks; registered component codecs determine contents
JSON snapshot slot.json Snapshot reads/writes reject another game's identity through the save API
Player data player-id.json A separate game-managed record

JSON snapshots and player records share the same filename namespace. Reserve the player- prefix for player records: snapshot slot player-alice would collide with player ID alice, and the listing APIs classify that prefix as player data.

Deleting a binary slot through the binary delete API does not delete all related snapshots or player files. Present deletion accurately in the game UI.

Native writes create directories and use a temporary-file-and-rename flow. That is not a multiwriter conflict-resolution system. Avoid several game processes writing the same slot at the same time.

Test save compatibility

A useful save test changes a visible game value, writes it, closes the game, reopens it and checks the restored value and its consequences. If position matters, also check collision/physics and camera behavior rather than only the saved numbers.

For custom components, verify registration and version behavior. The binary loader can skip unknown component blocks; that does not guarantee that removing or changing a component preserves your gameplay meaning.

Keep backups before changing a save format. A new feature with a default is different from silently replacing an old player's progress with a fresh state.

Browser persistence is different

The current browser storage backend uses localStorage with game/file-scoped keys. It is not account-based cloud synchronization. Data belongs to that browser/origin and can disappear when site data is cleared or access is denied.

Storage denial or quota failure can surface as a missing-storage error. Do not assume every missing save means the player intentionally deleted it. Browser capacity is limited; large-save storage is not solved by assuming an unfinished alternate backend exists.

Native binary saves can use compression. Web binary compression is disabled, and compressed native binary data is rejected by that decoder path. Do not promise that any .sav can be exchanged between native and web builds. JSON snapshots are a separate format, not an automatic guarantee that the game's schema matches.

Repeat a scenario, not an imaginary recorder

For a reproducibility script:

  1. Begin from a fresh, known game state or a validated game-defined snapshot.
  2. Pause and use explicit update counts.
  3. Press and release every held control deliberately.
  4. Request state at important points.
  5. Capture the needed rendered result separately.
  6. Record which game build and environment the result came from.

There is no generic command that records all future inputs, seeks to any earlier tick or guarantees identical rendered images. Cutscene timeline APIs are another feature again; their timing tests do not prove a complete game's replay determinism.