Save and Load (World Snapshot) Reference¶
This page documents the snapshot API on World.
Methods¶
registerComponentSnapshot<T, D>(key, codec): this¶
Registers a component snapshot codec.
key: ComponentCtor<T>is the component token/class used in ECS storage.codec: SnapshotCodec<T, D>defines:key: stringstable snapshot type key (for example"comp.position")serialize(value: T): Ddeserialize(data: D): T
Notes:
codec.keymust be non-empty.- Snapshot type keys must be unique per registry.
- Calling again with the same ctor replaces the previous codec.
unregisterComponentSnapshot<T>(key): boolean¶
Removes a registered component snapshot codec.
Returns:
trueif removedfalseif none existed
registerResourceSnapshot<T, D>(key, codec): this¶
Registers a resource snapshot codec.
Same rules as component registration.
unregisterResourceSnapshot<T>(key): boolean¶
Removes a registered resource snapshot codec.
snapshot(): WorldSnapshot¶
Exports world state.
Behavior:
- throws if called during iteration/structural lock
- flushes pending commands first
- serializes only registered components/resources
- returns deterministic ordering for entities/components/resources
restore(snapshot: WorldSnapshot): void¶
Loads world state.
Behavior:
- throws if called during iteration/structural lock
- validates snapshot format string
- clears pending commands
- clears event channel buffers
- clears current resources
- restores allocator + entities + registered data
restore() does not restore systems/schedules.
SnapshotCodec<T, D>¶
1 2 3 4 5 |
Guidelines:
- keep
keystable across versions - output JSON-safe data if you persist via
JSON.stringify - avoid embedding runtime handles/functions in codec data
WorldSnapshot shape¶
Persisted vs non-persisted¶
Persisted:
- alive entities
- registered components
- registered resources
- allocator state
Not persisted:
- systems
- schedules
- events
- command queue
Error conditions (common)¶
- unsupported snapshot format
- duplicate snapshot type entries in payload
- missing codec for a component/resource type during restore
- invalid allocator/entity ids or generations
- entity id marked both alive and free
Determinism guarantees¶
For the same world state and codec outputs:
entitiesare ordered by entity id- each entity
componentslist is ordered by codec key resourceslist is ordered by codec key