Store holds the project variables: values that every scene and chapter can read and change, such as the player's name, their score or which doors they've opened. They last until the game closes, and they're what SaveManager saves. See Variable Store for how the store works with chapters.
Project variables are set up in the Variables Manager, and start at the values set there. Chapters read and change the same variables with {$name} and {set($name, value)}.
| Member | Description |
|---|---|
Get() |
Reads a variable |
Set() |
Changes a variable |
Register() |
Calls a handler when a variable changes |
Unregister() |
Removes a handler |
GetCharacterTarget() |
The character $char and $variant read |
SetCharacterTarget() |
Changes that character |
GetState() |
Everything a save holds |
SetState() |
Restores it |
Get
Store.Get<T>(key: VariableName, defaultValue?: T): T
Reads a project variable.
| Parameter | Type | Default | Description |
|---|---|---|---|
key |
VariableName |
The variable's name | |
defaultValue |
any |
None | Used if the variable doesn't exist yet |
Returns the variable's value.
How it works
If the variable doesn't exist, it's created with defaultValue, which is returned. Later calls return that value even if they pass a different default. Handlers registered for it aren't called.
Example
onEnter() {
const name = Store.Get("playerName", "Alex"); // "Alex" until the player picks a name
this.dom.greeting.textContent = `Welcome back, ${name}`;
},
Set
Store.Set(key: VariableName, value: any): void
Changes a project variable, creating it if it doesn't exist, and calls the handlers registered for it.
How it works
- Handlers are called every time, even if the value hasn't changed.
- Each change is written to the game log, which helps when following a bug in the Debug View.
- Objects and arrays are stored as they are, not copied. Changing one in place doesn't call the handlers, so
Setit again afterwards.
Example
onCoinCollected() {
Store.Set("coins", Store.Get("coins", 0) + 1);
},
Register
Store.Register(key: VariableName, handler: (value: any, previous: any) => void, owner?: object): void
Calls handler each time the variable is set, with its new value and the value it had before.
| Parameter | Type | Default | Description |
|---|---|---|---|
key |
VariableName |
The variable to watch | |
handler |
(value, previous) => void |
Called after each change | |
owner |
object |
None | Usually this. See The owner argument |
How it works
- The handler isn't called straight away, so show the current value yourself when you register.
- Changes made from chapters call it too.
previousis the same asvaluethe first time a variable that didn't exist is set.
Example
onStart() {
Store.Register("coins", this.showCoins, this);
this.showCoins(Store.Get("coins", 0)); // show the starting value too
},
showCoins(coins: number) {
this.dom.coin_label.textContent = String(coins);
},
Unregister
Store.Unregister(key: VariableName, handler: (value: any, previous: any) => void, owner?: object): void
Removes a handler added with Register. Pass the same function, not a bound copy. UnregisterScene(owner) removes every handler that owner registered.
Example
onTutorialFinished() {
Store.Unregister("coins", this.showCoinHint, this); // stop hinting once the tutorial is over
},
GetCharacterTarget
Store.GetCharacterTarget(): { key: string; variant: string }
Returns the character that $char and $variant read in chapters: its key, and the variant's name. Both are "" until a character is set.
Example
onSpeakerChanged() {
const { key } = Store.GetCharacterTarget();
this.dom.speaker_name.textContent = Assets.GetCharacter(key)?.name ?? "";
},
SetCharacterTarget
Store.SetCharacterTarget(key: string, variant?: string): void
Changes the character that $char and $variant read in chapters, the same as {character(key, variant=name)} in a chapter.
Example
onTalkToAlice() {
Store.SetCharacterTarget("alice", "smiling");
Revealer.RevealNode("alice_greeting"); // {$char.mood} in this node now reads alice's mood variable
},
GetState
Store.GetState(): Record<string, any>
Returns a snapshot of everything a save holds: every project variable, the characters' variables and the character target. The current scene is in it as scene.
Example
onExportPressed() {
log(JSON.stringify(Store.GetState())); // the whole game state, for a bug report
},
SetState
Store.SetState(data: Record<string, any>): void
Restores a snapshot from GetState. Variables in the snapshot replace the current ones. Variables it doesn't mention keep their values.
How it works
- No handlers are called.
- It restores the snapshot's
scenetoo, soGetCurrentScene()returns it, but it doesn't switch scene.SaveManager.LoadcallsSwitchScenestraight after.
Example
levelStart: {} as Record<string, any>,
onStart() {
this.levelStart = Store.GetState(); // remember how everything was when the level began
},
onRetryPressed() {
Store.SetState(this.levelStart);
SceneManager.SwitchScene(SceneManager.GetCurrentScene()); // start the level again
},