Skip to content

Save/Load Demo

App: apps/save_load_demo/

Demonstrates game-authored, versioned save/load (engine PLM-022 / ADR 0036). On every launch it reads a save slot, increments a persisted launch counter, and writes it back — so quitting and re-running the app makes the counter climb, proving the state survives across runs. A small GUI window shows the current count and lets you re-save on demand.

Terminal window
% ./plume3d save_load_demo

Re-run it a few times and watch the launch count go up. The save lands at saves/demo.sav under the app’s own directory (the write dir).

  • On init, reads the slot with Save.read("saves/demo.sav"). If it returns null (no save yet, or a save from a newer build), it starts fresh at launch 1; otherwise it reads the previous launches value with loaded.getInt("launches", 0) and adds one.
  • Rebuilds a fresh SaveState from the current game state — an int launches, a string player, a bool tutorialDone, and a number volume — and persists it with Save.write(_slot, s), latching a status message on success or failure. This is the game-authored model: the app writes exactly the keys it cares about, not an automatic scene dump.
  • Persists immediately on launch, and again whenever you press S, so you can re-save without waiting for the next run.
  • Draws a small Gui window showing the persisted launch count, the last status message, and the hint to quit and re-run.
import "engine" for Save, SaveState
// Read (or start fresh), increment, write back — the persist-across-runs loop.
var loaded = Save.read("saves/demo.sav")
var launches = (loaded == null) ? 1 : loaded.getInt("launches", 0) + 1
var s = SaveState.new()
s.setInt("launches", launches)
s.setString("player", "Ada")
s.setBool("tutorialDone", launches > 1)
s.setNumber("volume", 0.8)
Save.write("saves/demo.sav", s) // -> true on success

Save.read returning null is the same signal for “no save yet” and “save is from a newer engine build” — a newer-format file is rejected, never mis-parsed — so guarding the read against null is all the version-safety the game needs.

  • Save / LoadSaveState.new, setInt/setString/setBool/setNumber, getInt, count; Save.write, Save.read.
  • Gui — the status window (beginWindow, label, layoutRowDynamic).
  • InputkeyJustPressed for the re-save key.
  • Loggerinfo.