Variables, Saving & Scene Changes
Read, set, and watch story variables, and keep them across scenes and saves.
The blackboard is where you store all your story's variables: gold, trust, quest flags. In Unity it's also how the story and your game share state. Your game can read a variable to open a door, change one when the player picks up an item, or do something whenever one changes. This guide covers all three, then how variables carry across scenes and into save files.
Everything here uses the Poetix class from Controlling Dialogue from Code, in the PoetixSDK namespace.
Reading and changing variables#
int gold = Poetix.GetVar<int>("Gold");
bool hasRelic = Poetix.GetVar<bool>("Has Relic");
string name = Poetix.GetVar<string>("Player Name", "Traveler"); // used if there's no such variable
Poetix.SetVar("Gold", gold + 50);
Poetix.SetVar("Has Relic", true);Variables are found by the name you gave them on the blackboard in Poetix, ignoring upper and lower case. Both calls work at any time, not only during a conversation, and a change made from code is seen by the story right away: by the next line's conditions, by {{Gold}} in its text, and by the next conversation.
Each blackboard type has a matching C# type:
- Number: read it as
int,float, ordouble. - Boolean:
bool. - String:
string.
SetVar converts the value to the variable's type, so SetVar("Gold", 10f) works on a Number. A value that can't be converted, like text for a Number, is logged as an error and changes nothing. So is a name the story doesn't have.
To avoid typing mistakes, click Generate under “Constants for Code” on the story Inspector and use its constants, like ForsakenOutpostVariables.Gold. See Beat names without typos.
Watching a variable#
Poetix.WatchVar calls your code whenever a variable changes, with its old and new value. It's the easiest way to keep a HUD, a door, or a quest log in step with the story:
using PoetixSDK;
using TMPro;
using UnityEngine;
public class GoldCounter : MonoBehaviour
{
[SerializeField] private TMP_Text label;
void Start()
{
Poetix.WatchVar<int>("Gold", (before, after) => label.text = after.ToString(), callNow: true);
}
}It fires however the variable changed: a line or choice result, your own SetVar, loading a save, starting a new game, or a new scene starting. callNow: true also calls it once right away, so the counter shows the current value from the start.
Watching stops by itself when the object is destroyed, as long as the callback only uses the object's own fields, like label here. If it uses a local variable or a method parameter, C# can't tell which object it belongs to, so pass owner: this. To stop earlier, keep what WatchVar returns and call Dispose() on it.
An error inside your callback is logged and doesn't stop the story.
Variables between conversations and scenes#
Variables carry over by themselves while the game runs: between conversations, and between scenes. Each scene can have its own Dialogue Box, and the next scene's box starts from the values the last one left. You don't need to save anything at a scene change.
Each story keeps its own variables. A game with one story per chapter never mixes them up, even when two stories both have a variable called "Gold". GetVar and SetVar use the story the Dialogue Manager is playing, which changes when you call Poetix.Play(otherStory).
One Dialogue Box for the whole game#
By default each scene uses its own Dialogue Box. If you'd rather keep one box for the whole game, turn on Keep Between Scenes in the Dialogue Manager's Advanced settings. The box then survives scene loads, and a conversation playing during the change carries on where it was.
You can still leave a Dialogue Box in every scene, so each one can be played on its own while you build it. When a kept box already exists, the new scene's box removes itself and its Dialogue Starters use the kept one.
With Keep Between Scenes on, connect the box's Inspector events only to objects that stay too. A connection to an object in the old scene stops working once that scene is gone.
Saving and loading#
Poetix doesn't write save files. It gives you the story state as text, and you store it with the rest of your save, however your game saves:
// Save
string dialogue = Poetix.SaveState();
mySaveData.dialogue = dialogue;
// Load
Poetix.LoadState(mySaveData.dialogue);
// New game: every variable of every story back to its starting value
Poetix.ResetState();- One save covers your whole game. If it uses several Poetix stories (one per chapter, say), the text holds the variables of every story played so far, not just the current one, and loading it restores them all.
- A save made between conversations holds only the variables. Loading it ends any conversation that's playing and opens none.
- A save made during a conversation reopens it at the saved line, with its choices.
- Text that isn't a Poetix save is refused with an error in the Console, and nothing changes.
Saves and story updates#
You'll keep editing your story after players have saved, and old saves still load:
- Variables the story no longer has, or whose type changed, are left out, with one warning in the Console.
- New variables start at their starting values.
- A conversation whose beat was deleted isn't resumed. Its variables still load.
Renaming a variable in Poetix is safe: the saved value follows it to its new name. Changing its type isn't, so a Number that becomes a String starts over at its starting value.
Testing with different values#
To try a conversation with 0 gold and then with 1000, you don't need code. The in-game Debug Menu shows every variable and lets you change it while the game runs. See Debugging & Tools.