parse
SourceChecks the structure of untrusted snapshot data. Names and values are checked against the schema by the runtime.
Runtime
Save and load the world as plain data.
runtime.snapshot() captures entities (with their ids), component values, relation edges, resources, and committed state-machine values, keyed by descriptor, relation, and machine names. A bound schema guarantees those names are unique, so they are stable save keys. The snapshot is plain data: store it with JSON.stringify or structuredClone.
runtime.restore(data) accepts unknown, because saved data comes from outside the program, and validates it before touching the world: shape, unknown names, every component and resource value (through its descriptor's constructor), relation edges, and machine states. On failure the world is unchanged.
Both methods exist only when every component and resource in the schema is either constructed with a constructor that accepts untrusted input (Descriptor.ConstructedComponent(...) given a decode(raw: unknown), as @typeonce/bevy-ts-math modules export, or a result(raw: unknown)) or transient (Descriptor.TransientComponent<T>(), which is not saved). Otherwise calling them is a compile error naming the descriptors without a validator, so no unvalidated value can enter the world through a save file. Descriptor.fromStandardSchema(...) turns any Standard Schema validator into a constructor.
Restoring despawns every current entity and spawns the saved ones with their original ids, so change detection and renderer sync see a restore as normal despawns and spawns. Pending commands and events are dropped. Transient components are absent from restored entities; transient resources keep their current value.
localStorage.setItem("save", JSON.stringify(runtime.snapshot()))
const restored = runtime.restore(JSON.parse(localStorage.getItem("save") ?? "null"))
if (!restored.ok) {
console.error(restored.error)
}
Checks the structure of untrusted snapshot data. Names and values are checked against the schema by the runtime.