Save to a cloud or a console save API
Goal: your game’s save lives in a place you control, such as a cloud service, a console’s save API or your own database, and not in the Save System’s slot files.
If you only want the slot files somewhere else, a custom storage is less work and keeps the slot list. This recipe is for when you hold the bytes yourself, because a platform asks for its own save API, because you sync saves to a server of your own, or because the save has to live inside an asset such as Easy Save.
- Capture the world. This is the step that can refuse, so test the result. It runs on the main thread, on the host only.
- Encode it to bytes.
- Store the bytes. From here any thread may do it.
Example, copied from SaveSamples.cs. Compiled against the package’s runtime assembly; not run in Unity. myStore stands for your own storage:
var captured = IntrigueSave.State.Capture(); // main thread, host onlyif (!captured.Ok) return false;var encoded = IntrigueSave.Codec.Encode(captured.Data, new EncodeOptions { Title = "Cloud save" });if (!encoded.Ok) return false;myStore["intrigues"] = encoded.Bytes; // from here any thread may store the bytes- Decode the bytes. It is checked and never throws.
- Stage the data. Nothing changes yet. The report tells you what a load would find in your game.
- Restore.
Restoreworks in place. UseRestoreAsync(staged, scene)to load a scene.
Example, copied from SaveSamples.cs. Compiled against the package’s runtime assembly; not run in Unity.
var decoded = IntrigueSave.Codec.Decode(bytes); // checked; never throwsif (!decoded.Ok) return "decode: " + decoded.Reason;var staged = IntrigueSave.State.Stage(decoded.Data); // nothing in the game changes yetvar restored = IntrigueSave.State.Restore(staged); // in place; across scenes use RestoreAsyncreturn restored.Ok ? "ok " + staged.Report.Actors : "restore: " + restored.Reason;| Step | Signature | Source file |
|---|---|---|
| Capture | static CaptureResult Capture(CaptureOptions options = null) |
State/IntrigueSave.cs |
| Encode | static EncodeResult Encode(SaveData data, EncodeOptions options = null) |
State/IntrigueSave.Facade.cs |
| Decode | static DecodeResult Decode(byte[] bytes, DecodeOptions options = null) |
State/IntrigueSave.Facade.cs |
| Stage | static StagedLoad Stage(SaveData data, RestoreOptions options = null) |
State/IntrigueSave.cs |
| Restore | static LoadResult Restore(StagedLoad staged) |
State/IntrigueSave.cs |
You will see: every result has Ok and a Reason. Nothing here throws.
Test the flag, and read the reason only when it is false, so that a refused capture or a damaged save tells you what happened in words you can show to a player.
What can go wrong
Section titled “What can go wrong”- Capture refuses: for example on a multiplayer client or while a scheme’s node is running. Capture also needs the Intrigue Manager and the main thread. Read
captured.Reason. - Your game has secret save parts: these bytes hold them as plain text, because only the slots encrypt them. Use the slots for such a game.
- A second
Restoreof the same staged load answersAlreadyRestored. Stage again. - The save is binary. Never store it as a plain string. Keep the bytes, or turn them into Base64 text if your store only takes text, and remember that PlayerPrefs suits small saves only.
Everything the steps accept, the options and the statuses: Storing the bytes yourself and Save and load results.