Save and restore multiple contexts¶
WorldPersistence coordinates explicit TunalPersistence registrations. Each member keeps its own save options, catalog, stable ID, lifecycle list, and presentation scope. The coordinator does not control context visibility or pause clocks.
Configure a world registry¶
- Configure each context's persistence and give every member a distinct stable Context ID.
- Add Tunal > Persistence > World Persistence to a persistent scene coordinator outside switchable roots.
- Register each TunalPersistence once under Contexts. Use explicit registrations for additive scenes or independently placed contexts.
- Set World Slot (default
world1), Local Folder (defaultTunalWorlds), and Load Policy. - Validate registration, initialize all participating contexts, and keep their persistence components enabled.
- In Play Mode use Save World / Load World, or integrate the confirmed-save API below.
Duplicate components, duplicate/invalid context IDs, missing references, and lifecycle double ownership are errors. Identical member local slots are warnings: world storage keys by Context ID instead of those legacy local slot names.
Matching policy¶
Exact requires the saved and scene registries to match. It prevents accidentally applying only part of a saved world. SkipMissingSceneContexts loads the matched subset with warnings: saved contexts absent from the scene are skipped, and scene contexts absent from the save remain unchanged. No matches is still a failure.
Choose the policy explicitly for your additive-scene/loading design. A permissive policy does not instantiate missing contexts or reconcile unrelated Context IDs by display name.
Complete integration example¶
Attach this component outside switchable roots, assign WorldPersistence, and connect button events to Save/Load:
using Sital.Tunal.Persistence;
using UnityEngine;
namespace TunalExamples
{
public sealed class WorldSaveButtons : MonoBehaviour
{
[SerializeField] private WorldPersistence world;
private bool saving;
private bool Ready()
{
if (world == null) return false;
var validation = world.ValidateConfiguration();
if (!validation.success) { Report(validation); return false; }
foreach (TunalPersistence item in world.Contexts)
if (item.Context == null || !item.Context.Initialize()) return false;
return true;
}
public void Save()
{
if (saving || !Ready()) return;
saving = true;
StartCoroutine(world.SaveWorldConfirmed(result =>
{
saving = false;
Report(result);
}));
}
public void Load()
{
if (!saving && Ready()) Report(world.TryLoadWorld());
}
private void Report(PersistenceResult result)
{
if (!result.success) Debug.LogError(result.error, this);
foreach (string warning in result.warnings)
Debug.LogWarning(warning, this);
}
}
}
Keep the coroutine host alive and prevent simultaneous operations. Confirmation has the same browser-storage boundary as a single-context save. The example initializes registered contexts but does not select a presentation, load scenes, or migrate old profile IDs.
Files and failure behavior¶
The coordinator captures every member before writing. It writes one JSON file per context into the inactive bank_a/bank_b, then writes the world manifest pointer last. This protects the previously selected bank from an interrupted staging operation. It does not constitute an arbitrary filesystem transaction or a browser durability guarantee.
Loading checks the manifest (with backup recovery where valid), reads selected snapshots, resolves identity matches, and preflights every matched context before restoring the first. Application then runs sequentially. If a later restore fails, earlier contexts may already have changed. There is no automatic rollback of simulation or external event-listener side effects.
A world save also does not capture which context is currently selected for display. Preserve that game/UI preference separately if required, and apply visual selection deliberately after a successful load.
Related: single-context state selection, storage/results, context switching.