Variables and Storage
Sooner or later your story needs to remember something: the player’s name, which drink they chose, how much the other characters trust them, which endings they’ve seen. This page shows where to keep each kind of value, how to change it from your script, and what happens to it when the player saves, loads, goes back or starts a new game.
Where your game keeps its data
Monogatari keeps the data of a game in three places, and each behaves differently:
| Storage | State | Globals | |
|---|---|---|---|
| For | Your story’s variables: names, choices, points | The engine’s own record: the label, the step, the scene, the characters on stage, the music | Temporary values for your own code |
| Read and change with | monogatari.storage() |
monogatari.state(), to read |
monogatari.global() |
| Saved in save files | Yes | Yes | No |
| Going back restores it | Only what your own Revert functions undo |
Yes | No |
| A new game | Your declared variables start over | Starts fresh | Keeps them |
Use the storage for nearly everything. Leave the state to the engine: read it if you need to, but keep your own values in the storage. For data that must outlive a playthrough, like the endings a player has seen, see Remember something across playthroughs.
Declare your variables
Declare the variables in js/storage.js, with the value each one starts with. The starter game declares one, the player’s name:
monogatari.storage({
player: {
name: '',
},
});Add yours to the same object, nested however you like:
monogatari.storage({
player: {
name: '',
age: 0,
},
drink: '',
metTheDetective: false,
inventory: {
gold: 100,
potions: 2,
},
});These values are the defaults: every new game starts from them. Declare every variable your game uses, even the ones you only set later: a line that shows a variable that doesn’t exist yet comes out empty.
Show a variable in a line
Write its name between double braces, with dots for nested values:
'y Nice to meet you, {{player.name}}!',
'You have {{inventory.gold}} gold left.',Put a variable in a line covers the rest.
Read a variable
From a function in your script, storage('name') gives you one top-level variable, and storage() the whole storage:
function() {
const player = this.storage('player');
const gold = this.storage('inventory').gold;
console.log(player.name, gold);
},Inside a function written with the function keyword, this is the engine, so this.storage(…) is the same as monogatari.storage(…).
Change a variable
Pass an object to storage() with only what changes. It is merged into the storage: the variables you leave out keep their values, and so do the other keys of a nested object you change.
function() {
// Only player.age changes: player.name keeps its value
this.storage({
player: {
age: 18,
},
});
},Changes often depend on the current value. Read it, work out the new one, and write it back:
function() {
const { gold, potions } = this.storage('inventory');
this.storage({
inventory: {
gold: gold - 30,
potions: potions + 1,
},
});
},From a choice, do the same in its option’s onChosen function: see Remember what the player chose.
A list is replaced, not merged. To add an item, build the new list:
const items = this.storage('items'); this.storage({ items: [...items, 'scarf'] });An empty object changes nothing.
storage({ inventory: {} })merges no keys, soinventorykeeps them all. Usenullto empty it:storage({ inventory: null }).Assigning directly works too, as in
this.storage('player').name = 'Kim', but a value you read before the nextstorage({ … })call then belongs to the old storage, not the current one. Passing an object tostorage()avoids the surprise.
What a variable can hold
Text, numbers, true and false, null, lists and plain objects of those, nested as deep as you like. Saves store the storage as it is, so keep to these: a function in the storage makes saving fail, and other kinds of values may not come back the same when a save loads, depending on the storage engine. Store a date as a number or as text.
Going back
The player can go back to earlier lines with the Back button or the left arrow key. Monogatari doesn’t undo changes to the storage by itself: the code that changed a variable has to say how to undo it. Each statement that can change the storage has its own way:
| Statement | Undo it with | Without it |
|---|---|---|
A Function object |
Its Revert function |
The player can’t go back past it. |
| A plain function | It can’t be undone | The player can’t go back past it. |
A choice option’s onChosen |
The option’s onRevert function |
The player can’t go back past the choice once they’ve picked that option. |
An Input statement’s Save |
The input’s Revert function |
The player can’t go back past it. |
So change variables in a Function object with a Revert that does the opposite:
{
'Function': {
'Apply': function() {
const { gold } = this.storage('inventory');
this.storage({ inventory: { gold: gold - 30 } });
},
'Revert': function() {
const { gold } = this.storage('inventory');
this.storage({ inventory: { gold: gold + 30 } });
},
},
},When “the player can’t go back past it”, pressing Back on the line after the statement shows that line again. Functions, Choices and Input have the details.
Saving and loading
Saving writes the storage, the state and the engine’s history into the save; loading brings all three back. You don’t need any code for it. Saving and Loading explains what the player sees.
What a new game resets
When a game ends, with end or the Quit button, every variable declared in js/storage.js goes back to its default before the next game starts. A variable your script added along the way without declaring it in js/storage.js is removed, and so is one added to a declared object (player.nickname when js/storage.js only declares player.name). Loading a save does the same before it puts the save’s variables back. Declare every variable your game uses, so each one has its starting value.
Reloading the page starts everything from js/storage.js again.
Remember something across playthroughs
Some data belongs to the player rather than to one playthrough: the endings they’ve reached, a “seen the true ending” flag. The storage forgets it at every new game, so keep it in the game’s storage engine instead: the place on the player’s device where Monogatari keeps the saves and the settings. monogatari.Storage reads and writes it:
// In a function at the end of the true ending
function() {
return this.Storage.set('Endings', { trueEnding: true });
},// Anywhere later, even after the browser was closed
{
'Conditional': {
'Condition': function() {
return this.Storage.get('Endings').then((endings) => {
return endings.trueEnding === true;
}).catch(() => false);
},
'True': 'y Haven\'t we met before?',
'False': 'y Nice to meet you.',
},
},set(key, value)stores the value under that key, replacing what was there, andget(key)reads it back. Both return a promise, which the function returns so the game waits for it.get()fails when nothing was stored under the key yet, so thecatchabove makes that count asfalse.- Pick a key of your own, like
Endings: Monogatari already usesSettings,gallery, and keys that start withSave_andAutoSave_for its own data.
The data stays when the game ends, when the page is reloaded and when the player goes back. It’s lost only when the player clears the site’s data in their browser.
Where the storage engine keeps the data
The starter game’s js/options.js sets 'Adapter': 'IndexedDB' in its Storage setting, so the data goes into the browser’s IndexedDB database for your game’s Name. That’s why the Name setting shouldn’t change once your game is released: a new name starts an empty database, with no saves. Storage Engine describes the other adapters.
The state, and temporary values
monogatari.state()is the engine’s record of the game:state('label')andstate('step')say where the player is,state('scene')which scene is showing,state('characters')who is on stage,state('music')what is playing. Read it if your code needs it; the engine changes it as the game plays.monogatari.global(name)reads a temporary value for your own code, andmonogatari.global(name, value)sets it. Globals are neither saved nor undone when the player goes back. The engine keeps its own temporary values there too, so give yours a name of your own.
Engine API documents every method.
A complete example
A script that asks the player’s name, changes the storage from a Function object, and uses both in its lines. Every change can be undone, so the player can go back through all of it:
// js/storage.js
monogatari.storage({
player: {
name: '',
},
inventory: {
gold: 100,
},
});// js/script.js
monogatari.script({
'Start': [
'show scene #f7f6f6 with fadeIn',
{
'Input': {
'Text': 'What is your name?',
'Validation': (input) => {
return input.trim().length > 0;
},
'Save': function(input) {
this.storage({
player: {
name: input.trim(),
},
});
return true;
},
'Revert': function() {
this.storage({
player: {
name: '',
},
});
},
'Warning': 'You must enter a name!',
},
},
'Welcome, {{player.name}}. You have {{inventory.gold}} gold.',
{
'Function': {
'Apply': function() {
const { gold } = this.storage('inventory');
this.storage({ inventory: { gold: gold - 30 } });
},
'Revert': function() {
const { gold } = this.storage('inventory');
this.storage({ inventory: { gold: gold + 30 } });
},
},
},
'You bought a warm scarf. {{inventory.gold}} gold left.',
'end',
],
});If the player goes back past the Function, their gold returns to 100, and going back once more asks for their name again. Input has the details of the name prompt.
Related
- Choices and Branching: set variables from choices, and branch on them.
- Writing Dialog: show variables in lines.
- Saving and Loading: how saves work for the player.
- Engine API: every method of the
monogatariobject.