Skip to content

Storage and restoration contracts

Capture, serialization, file writes, and restored gameplay state are separate operations. Design your UI and error handling around the boundary that actually completed.

Local JSON

LocalJsonStore writes beneath Application.persistentDataPath using validated folder/slot keys, not caller-supplied arbitrary paths. A single-context default is Tunal/autosave.json. World defaults use TunalWorlds/world1, a manifest, and per-context bank files.

Writes use a temporary file and retain a backup when replacing an existing target. Where File.Replace is unavailable, a copy-based fallback is used; WebGL also uses copying in its virtual filesystem. This is recovery-oriented behavior, not a promise of atomic filesystem replacement on every platform. Constructors can reject invalid keys or an unavailable persistent data path; Try methods return errors for handled IO failures.

Do not share the single-context filename between independently saved contexts. TrySaveLocal checks existing ownership and refuses invalid/foreign data. Keep backups when debugging corruption; do not delete an entire save directory as the first troubleshooting action.

Schema and profile compatibility

Current Snapshot schema is 4; the serializer accepts supported earlier schemas 2/3 and supplies compatibility behavior for missing newer fields. Unsupported schemas, invalid values, mismatched required Context ID, or unresolved profile IDs fail validation. A save may still omit optional sections. Do not edit numeric enum values or GUID strings by hand to make an incompatible snapshot appear valid.

StateSerializer.ToJson/FromJson use portable snapshot data, not Unity scene references. Parsing can throw for invalid input. Maintain your own game-level schema/versioning for non-Tunal data and content migrations. Profile IDs refer to currently shipped assets, not embedded historical copies.

Restoration order and failure boundaries

ValidateRestore performs preflight without mutating the Tunal instance. On application, the serializer restores Time, reconciles Calendar, applies system profiles/location, refreshes context modules and dependent state, publishes restored clock/calendar state, then restores available presentation parameters and lifecycle state. Lifecycle progression is suspended during the operation and released afterward.

The order is designed for coherent chronology, but it is not global event suppression. Profile changes, refreshes, and final notifications can invoke listeners during restoration. A callback that saves immediately or queries another not-yet-restored context can observe an in-progress operation. Coordinate your own load UI/gameplay gate around the returned result.

Preflight cannot guarantee that runtime application or customer listeners cannot fail. Single-context restore has no general rollback; world application can leave earlier members restored if a later member fails. Avoid irreversible gameplay actions in state-update listeners unless your integration deliberately handles load/reconciliation.

System-clock modes resynchronize to the actual clock; restoring selected simulated elapsed seconds does not force an operating-system clock backward. Excluded sections retain scene configuration where supported rather than becoming a complete old-world snapshot.

WebGL confirmation

WebGL writes first reach Unity's virtual filesystem. TrySaveLocal/TrySaveWorld return after those writes and initiate background synchronization, with a warning that browser commit is pending. Do not show durable-save success from that return alone.

Use SaveLocalConfirmed, SaveWorldConfirmed, or the UI facade's SaveToSlotConfirmed coroutine and wait for its callback. StorageSync.FlushAndWait waits for confirmation or a 30-second timeout. Non-WebGL builds report completion without this browser sync phase.

Confirmation reports the IndexedDB synchronization result; browser policy, quota, eviction, private browsing, page closure, origin changes, and future user actions can still affect retention. Test the real hosted WebGL build, reload, failure handling, and browser settings. This documentation does not guarantee permanent browser storage.

Interpret results

  • success == false: show error and do not imply a successful save/load. A load failure can still follow partial application.
  • Warnings: record omissions, skipped contexts/sections, compatibility behavior, or pending sync. A green success flag does not prove every desired section was included.
  • Exceptions: low-level parsing, invalid lifecycle RestoreState data, or customer callbacks may throw. Design your surrounding load boundary accordingly; do not assume every arbitrary extension exception becomes a result object.

For missing profile IDs, rebuild the correct catalog/restore the expected assets. For Context ID mismatch, select the intended save/owner. For skipped optional state, fix module/scope readiness or explicitly accept the omission. For world mismatch, review the registry and policy rather than loosening it blindly.