Saving and Loading
A save is a copy of the game at one moment: where it is, what’s on the screen and every variable. The engine writes it to the browser’s storage under a key, the slot. The save and load screens call the methods on this page for the player; your code can call them too. Saving and Loading describes what the player sees.
Saving writes to the browser’s storage, which works for a game served from a web host or a local server. For a game opened from a file, see What works opened from a file.
Slots and their keys
A slot’s key is a prefix, an underscore and a number: Save_3, AutoSave_1. The prefixes are two settings:
| Setting | Default | Used for |
|---|---|---|
SaveLabel |
'Save' |
The saves the player makes, and the ones your code makes with saveTo('SaveLabel', …). |
AutoSaveLabel |
'AutoSave' |
The automatic saves. |
The number depends on who saves:
- The save screen’s Save button uses the time of the save, in milliseconds:
Save_1795891181829. Overwriting a slot from the save screen keeps its number. - The automatic save (the
AutoSavesetting, in minutes;0turns it off) uses the slots from 1 to theSlotssetting: the first empty one, or else the one written longest ago. - Your code picks the number.
The load screen lists every key that starts with one of the two prefixes and an underscore. When AutoSave is 0, it hides the automatic saves.
Save the game
saveTo()
Saves the game to a slot.
saveTo(prefix?: string, id?: number | null, name?: string | null): Promise<unknown>| Parameter | Type | Default | What it is |
|---|---|---|---|
prefix |
string |
'SaveLabel' |
The name of the setting that holds the prefix: 'SaveLabel' or 'AutoSaveLabel'. Not the prefix itself. |
id |
number |
null |
The slot’s number. Without one, the engine uses the current time in milliseconds, so every call makes a new slot. |
name |
string |
null |
The name the slot shows. Without one, or with an empty one, the slot shows the date and time of the save. |
It saves only while a game is being played (the playing global is true); otherwise it resolves with undefined and writes nothing. It resolves with what the storage returns: an object with the slot’s key and the value written.
A function in the script can save at the start of a chapter, so the player can come back to it:
monogatari.script({
'Start': [
'y The first chapter ends here.',
function() {
return this.saveTo('SaveLabel', 1, 'Chapter 2');
},
'y Chapter 2 begins.',
'end',
],
});The load screen now has a slot named “Chapter 2”. Slot 1 is overwritten each time a player reaches this point.
With the Screenshots setting on, the save also gets a picture of the game screen: see Save screenshots.
What a save holds
A save is an object:
| Key | What it is |
|---|---|
name |
The slot’s name. A save made without one is named with its date and time as the player reads it (Nov 28, 2026, 12:41:44 PM). |
date |
The date and time of the save, in ISO format: 2026-11-28T12:41:44.094-06:00. |
image |
The name of the scene (or background) on the screen, which the slot shows when it has no screenshot. |
game |
What object() returns: { history, state, storage }. |
screenshot |
The key of the slot’s screenshot, when it has one. |
The IndexedDB storage adds an id with the slot’s key.
Load a save
loadFromSlot()
Loads a save.
loadFromSlot(slot: string): Promise<void>slot is the whole key: 'Save_1', 'AutoSave_2'. It does this, in order:
- Reads the save, and checks that the label it was made at is in the script.
- Sets
playingtotrueand fireswillLoadGame. - Resets the game, as
resetGame()does, and hides every screen. - Restores the state, the history and the storage. Saves made by Monogatari v1.4.1 and earlier, and by older 2.x releases, are converted as they load. If the save’s step is past the end of its label (the label got shorter in an update), the game goes to the label’s last statement.
- Runs every action’s and component’s
onLoad(), which puts the scene, the characters, the music and the rest back on the screen. - Shows the game screen and fires
didLoadGame.
It doesn’t run the statement the game was on: the text box stays empty until something runs it. The load screen does that when the promise resolves, and your code has to do the same:
monogatari.loadFromSlot('Save_1').then(() => {
monogatari.run(monogatari.label()[monogatari.state('step')]);
});The scene comes back, then the line the player saved on types again.
If the slot doesn’t exist, the promise rejects with the storage’s error (Key "Save_9" not found…). If the save was made at a label the script no longer has, it rejects with an error that names the label. Either way the game stays as it was, nothing fires, and with the debug module an error dialog says which (engine:storage:slot_not_found or engine:storage:slot_label_not_found). Before 2.9.0, the game was already reset when the promise rejected.
Keep saves working after an update
When you release a new version of your game, the saves players made with the old one still load. If the new version renames a variable or changes how one is stored, upgrade() lets you fix the old saves once, when a player first opens the new version.
upgrade()
Registers a function that rewrites the game’s stored data when the Version setting changes from one version to another.
upgrade(oldVersion: string, newVersion: string, options: { storage?: (oldData: object) => object; replaceStorage?: boolean }): void| Option | Type | What it does |
|---|---|---|
storage |
function |
Gets every key the game has in the browser’s storage (Settings, Save_1…) as one object, and returns an object of the keys to write back. |
replaceStorage |
boolean |
true also deletes every key the returned object leaves out. false by default: the keys you don’t return stay as they were. |
The function runs once, the first time a player who last played oldVersion opens newVersion.
This renames the points variable to score in every save, for players coming from version 0.1.0:
// js/options.js, js/script.js or js/main.js
monogatari.upgrade('0.1.0', '0.2.0', {
storage: (oldData) => {
const newData = {};
for (const [key, save] of Object.entries(oldData)) {
if (key.startsWith('Save_') || key.startsWith('AutoSave_')) {
save.game.storage.score = save.game.storage.points;
delete save.game.storage.points;
newData[key] = save;
}
}
return newData;
},
});Then set 'Version': '0.2.0' in the settings() call of js/options.js.
The upgrade runs when init() starts the game and opens the storage, so call upgrade() anywhere before that: in any of your game’s files, outside the function you give to init(). One called after the storage opened never runs, and with the debug module the dialog engine:storage:upgrade_too_late says so.
It works with the IndexedDB (the starter game’s) and LocalStorage storage engines:
- With
IndexedDB, the upgrade runs while the database opens, sostoragemust return the new data itself, not a promise. - If
storagethrows an error, nothing is changed: the data and its version stay as they were, the game can’t save until the upgrade works, and the dialogengine:storage:upgrade_failedshows the error. SessionStorageandFileSystemnever run it, andRemoteStoragecan’t: the console shows a warning.
Before 2.9.0, upgrade() hung the game with IndexedDB (the page stayed blank), and one called after the first preferences() call never ran.
Changing the Version setting without an upgrade keeps every save as it is.
The storage
Storage
The object that reads and writes the game’s data in the browser: the saves, the player’s preferences, and anything your game stores itself.
Storage: StorageInterface;setupStorage() creates it from the Storage setting. Its methods all return promises:
| Method | What it does |
|---|---|
get(key) |
Resolves with the value. Rejects when the key doesn’t exist. |
set(key, value) |
Writes the value. Resolves with { key, value }. |
update(key, value) |
Merges the value into the one stored (for objects). |
remove(key) |
Deletes the key. |
keys() |
Resolves with every key. |
getAll() |
Resolves with every key and value, as one object. |
each(callback) |
Calls callback(key, value) for every key. |
contains(key) |
Resolves if the key exists, rejects if it doesn’t. |
clear() |
Deletes every key, saves and preferences included. |
onCreate(callback), onUpdate(callback) and onDelete(callback) register functions called with (key, value) when a key is written, updated or deleted through this object; the save and load screens use them to refresh their slots.
The engine uses the key Settings for the player’s preferences, and the save prefixes for saves. Any other key is free for your game. With the IndexedDB storage, an object you store comes back with an extra id that holds its key. Data you write here outlives every save and every new game, which suits what a player unlocks once:
monogatari.script({
'GoodEnding': [
'y And they lived happily.',
function() {
return this.Storage.set('Endings', { good: true });
},
'end',
],
});// js/main.js, inside monogatari.init('#monogatari').then(() => { … })
monogatari.Storage.get('Endings').then((endings) => {
console.log('Endings seen:', endings);
}).catch(() => {
console.log('No ending seen yet');
});Avoid keys that start with Save_ or AutoSave_: the load screen would list them as slots.
setupStorage()
Creates Storage from the settings.
setupStorage(): voidIt reads the Storage setting:
| Key | What it is |
|---|---|
Adapter |
Where the data goes: 'LocalStorage' (the engine’s default), 'SessionStorage' (lost when the tab closes), 'IndexedDB' (the starter game’s choice), 'RemoteStorage' (a server at Endpoint), or 'FileSystem' (files on disk, in the desktop app only). Any other name uses IndexedDB. |
Store |
The name of the store inside the database: 'GameData'. |
Endpoint |
The server’s address, for RemoteStorage. |
The data’s name comes from the Name setting, in lower case with dashes (My Visual Novel becomes my-visual-novel; a name with no Latin letters or digits, like 物語, keeps its own letters), and its version from Version. That’s why changing the game’s Name after release makes the old saves disappear from the load screen. FileSystem outside the desktop app uses IndexedDB.
It also asks the browser to keep the data even when the device runs low on space; if the browser says no, the console shows Persistent Storage permission has been denied…. Then it registers the upgrades.
The first preferences() call runs it, and init() runs it if nothing did. Call it again after you change the Storage, Name or Version setting from code. Configuration has more on each storage engine.
setupStorage() doesn’t open the storage: init() does, before it reads the player’s preferences. That’s when the upgrades run, and when an error dialog says what keeps the storage from opening: FileSystem outside the desktop app, a Version lower than the saved data’s (the game goes on with the saved data’s version), a Store the IndexedDB database doesn’t have (the game goes on with the one it has), or a failed upgrade. The error list has each one.
Save screenshots
With the Screenshots setting on (the engine’s default is false; the starter game’s js/options.js turns it on), each save slot shows a picture of the game instead of the scene’s image.
How it works:
- When the save screen opens while the game plays, the engine takes a JPEG picture of the game screen, 400 pixels wide. An automatic save, or a
saveTo()from your code, takes it from the game screen at that moment. saveTo()hands the picture toonSaveScreenshot, and keeps the key it returns in the save’sscreenshot.- The save slot calls
onLoadScreenshotwith that key to get an address it can show. - Deleting a slot calls
onDeleteScreenshotwith the key.
By default the pictures go to an IndexedDB database of their own, named after the game with _Screenshots added, under the slot’s key plus __screenshot. They don’t go through Storage, so they work whichever storage engine the game uses, as long as the browser offers IndexedDB. If a picture can’t be taken or stored, the save is made without one, and the slot shows the scene’s image.
To keep the pictures somewhere else, replace the three functions before the first save:
onSaveScreenshot
onSaveScreenshot: (slotKey: string, picture: Blob) => Promise<string>;Gets the slot’s key and the picture, stores it, and resolves with the key to keep in the save.
onLoadScreenshot
onLoadScreenshot: (key: string) => Promise<string>;Gets that key and resolves with something an image can show: a data: address, or a blob: address (the slot releases it when it’s removed). Resolve with '' when there is no picture.
onDeleteScreenshot
onDeleteScreenshot: (key: string) => Promise<void>;Gets the key of a deleted slot’s picture and deletes the picture.
screenshotSpace()
screenshotSpace(): Promise<Space | null>Resolves with the IndexedDB database the default functions use, or null when the browser has no IndexedDB.