Skip to content

Your own data in a save

A save of Intrigues holds the world: actors, clans, running schemes. Your game has state of its own, such as an inventory, a quest log or a settings screen. A save part puts that state into the same file, so one save restores both.

  • You want a player’s inventory to come back with a load.
  • You need one part of a save kept apart, or hidden from a player who opens the file.
  • You want a small save of only some of your data.

A part is one section of the save under its own key, such as game.inventory, with its own version. You register it once. After that every save writes its section, and every load reads it back.

Keys that start with intrigues. belong to Intrigues. IntrigueSave.Register refuses them, and a key another part already has, and a part without a key. It returns false and logs one warning. Registering the same part again returns true and changes nothing.

A part that throws is reported as a problem (PartWriteFailed or PartReadFailed) and the save or load goes on without it.

Form Use it when Source file
LambdaPart Two functions are enough: one returns your text, one takes it back. Parts/LambdaPart.cs
SaveBehaviour You want a component. It registers itself while enabled and unregisters when disabled. Parts/SaveBehaviour.cs
ISavePart You want full control over a class of your own. Parts/ISavePart.cs

SaveBehaviour is abstract. You override Write and Read. If you override OnEnable or OnDisable, call the base method.

new LambdaPart(key, version, write, read, absent, phase, order). The last three are optional.

Argument What it is
key The section’s key.
version The version of what write returns. Raise it when you change the layout.
write A function that returns the section’s text. Return null for no section this time.
read An action that takes the text and the version it was written with.
absent Called when a loaded save has no such section.
phase When the section is read in a load. Default AfterActors.
order The order inside the phase, lower first. Default 1000.

Example, copied from SaveSamples.cs:

public sealed class Inventory
{
public List<string> Items = new List<string>();
public string Write() => string.Join(",", Items);
public void Read(string text) { Items.Clear(); if (!string.IsNullOrEmpty(text)) Items.AddRange(text.Split(',')); }
}
public static void Register(Inventory inventory) =>
IntrigueSave.Register(new LambdaPart("game.inventory", 1, () => inventory.Write(), (text, version) => inventory.Read(text)));

IntrigueSave.Register(key, version, write, read) is a shorter form. It makes the LambdaPart for you and returns it, or null when it was refused.

Member What it does
string Key The section’s key, unique among registered parts.
int Version The version of the section’s layout.
SavePhase Phase When the section is read during a load.
int Order The order inside a phase. Same order: by key. Your parts usually use 1000.
bool ShouldWrite() False: nothing is written for this save.
void Write(ISaveWriter writer) Hand your state to the writer.
void Read(ISaveReader reader, int version) Take your state back. An older version is yours to convert.
void Absent() A save without your section was loaded: set your defaults.

The writer has Text(string), Json(object) and Bytes(byte[]). Json uses Unity’s JsonUtility, so it writes public and serialized fields. The last call wins. The reader has Text, Version, Json<T>(), JsonOverwrite(object) and Bytes().

The interface will not gain members. New options come as separate interfaces a part may also implement, so a part you write today keeps compiling.

SavePhase When
AfterWorld Right after the world was rebuilt, before any actor loads.
AfterActors After the actors the load brings itself have loaded.
WorldReady When the loaded game is complete.

Which actors exist at AfterActors depends on how the game loads. In place, the scene’s actors of the save are there. Across scenes, only the re-created ones are, and WorldReady is the phase that sees the game scene’s actors. A slot load of the Save System is across scenes by default.

A save request can name only some parts:

Example, not compiled:

new SaveRequest { Title = "Settings", Parts = new List<string> { "game.settings" } }

The file then holds only those sections and no Intrigues world. The slot list shows it as Partial, it is never offered as “continue”, and it does not become the current slot. A slot load of it reads only those parts into the running game.

A partial save is refused (InvalidParts) for an empty list, a key that starts with intrigues., a part that is not registered or writes nothing, an autosave, and a save to the current slot (SaveCurrentAsync). Saving a partial save over a slot that holds a whole game replaces that game.

Set Secret on a part to encrypt its section inside the save, even when the file itself is plain. LambdaPart has a settable Secret; set it before you register. SaveBehaviour has a field for it. A class of your own implements ISavePartOptions next to ISavePart; it is read once, at registration.

  • A save with a secret part is refused (SecretRefused) while the Save System’s password is empty or is the one Intrigues ships with. Set your own first.
  • A load that cannot open the section reports PartSecretUnreadable and treats the section as missing: Absent() is called.
  • Turning Secret on for an existing part drops that part’s data from every older save, because its plain section is no longer read.
  • Only the slots encrypt secret parts. Bytes you get from IntrigueSave.State.Capture hold them as plain text. See Storing the bytes yourself.

A password inside a game stops accidents and casual reading, not a determined person. More in Encryption.