Storing the bytes yourself
What this page is for
Section titled “What this page is for”The Save System keeps slots as files. If your game keeps saves somewhere else, such as a cloud, a console’s save API or your own database, you can skip the slots and handle the bytes yourself: Intrigues gives you the world as data and a codec that turns it into bytes and back, and you decide where the bytes go. That is the whole idea.
If you only want the slots in a different place, a custom storage is less work, because the slot list and the load report keep working and you supply only the place where the bytes go.
When you come here
Section titled “When you come here”- A platform requires its own save API.
- You sync saves to a server.
- You want a save in PlayerPrefs or in an asset such as Easy Save.
The steps
Section titled “The steps”Saving takes two calls. Loading takes three. Each one hands its result to the next.
| Step | Call | Returns | Source file |
|---|---|---|---|
| Capture | IntrigueSave.State.Capture(options) |
CaptureResult. .Data holds the SaveData. |
State/IntrigueSave.cs |
| Encode | IntrigueSave.Codec.Encode(data, options) |
EncodeResult. .Bytes holds the save. |
State/IntrigueSave.Facade.cs |
| Decode | IntrigueSave.Codec.Decode(bytes, options) |
DecodeResult. .Data holds the SaveData. |
State/IntrigueSave.Facade.cs |
| Stage | IntrigueSave.State.Stage(data, options) |
StagedLoad. .Report says what the load would find. |
State/IntrigueSave.cs |
| Restore | IntrigueSave.State.Restore(staged) or RestoreAsync(staged, scene) |
LoadResult, or a LoadOperation |
State/IntrigueSave.cs |
Capture and Restore need the Intrigue Manager (Restore also needs the Save System in the scene) and run on the main thread, on the host only. Encode and Decode run their JSON steps on the main thread too. Once you hold the bytes, any thread may store them.
Nothing here throws. Each result has Ok, a status and a reason, so you test one flag and read the reason only when it is false.
The capture needs the Intrigue Manager and the main thread, and it is the step that can refuse, for example on a multiplayer client or while a scheme’s node is running, so test the result before you encode anything.
Example, copied from SaveSamples.cs:
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 bytesCaptureOptions has Mode, FailOnPartError and InsideNode. The default mode, Settled, finishes a day’s thinking first, so the capture is taken between two days. Unsettled reads the world as it is and loses the rest of a running day. Do not use it for autosaves.
EncodeOptions fills the header: Title, SlotId, SceneName, SceneIndex, PlaySeconds, GameVersion, SavedAtUtc, Thumbnail, Custom and DefinitionStamps. It also has Protector (for example new SaveCipherProtector(password)), Compression and PrettyPrint. SaveCompression.Gzip is available, but the code states that it is not proven on IL2CPP, mobile or WebGL. Where it does not work, Encode answers CompressionFailed.
Secret save parts are plain text in these bytes, because only the slots encrypt them. A secret part also does not read a plain section back: it is told Absent. Use the slots for a game with secret parts.
Example, copied from SaveSamples.cs:
if (!myStore.TryGetValue("intrigues", out var bytes)) return "nothing stored";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;Decode checks the container. A damaged or cut-off save, a wrong password, and an older or newer format each come back as a DecodeStatus with a reason. An encrypted save needs DecodeOptions.Protector. Without it, you get PasswordRequired.
Stage changes nothing in the running game. Its report lists what a load would find: missing lists, actors without an ID, definitions the save and your database do not share. See Loading after the database changed. If you also pass the header, Stage(decoded.Data, decoded.Header, null) (the third argument is the RestoreOptions; null gives the defaults), the check compares the definition stamps in the header with your database and says which scheme graphs changed.
A staged load can be restored once. A second Restore of the same object gives AlreadyRestored.
In place or across scenes
Section titled “In place or across scenes”| Call | Where the game is restored |
|---|---|
Restore(staged) |
In place, in the scene that holds the actors now. |
RestoreAsync(staged, scene) |
The game is committed, then scene loads alone. Pass null to load the game scene yourself. The operation completes when that scene’s actors have run Start. |
RestoreOptions holds OnFailure, UnsavedActors and ActorWaitFrames.
Header only
Section titled “Header only”IntrigueSave.Codec.ReadHeader(prefix, withThumbnail) reads the header from the first bytes of a save, so a list can show titles without the body. When the bytes are too short, the result is Truncated and NeededBytes says how many are needed.
PlayerPrefs and similar
Section titled “PlayerPrefs and similar”Save files are binary. Store them as Base64 text or as bytes, never as a plain string. PlayerPrefs is for small saves only: its size is limited per platform, about 1 MB on WebGL, says the README.
How it connects
Section titled “How it connects”- The slot way, with files: Saving and loading.
- Your own data goes into the capture as parts: Your own data in a save.
- The statuses of each result: Save and load results.
- A cloud save recipe in short form: Save to a cloud or a console save API.