Storage Engines
A game keeps two things on the player’s side: their saves and their preferences. The Storage setting in js/options.js decides where. Most games never change it: the starter game’s choice, IndexedDB, keeps everything in the player’s browser and needs nothing else.
The Storage setting
monogatari.settings({
'Storage': {
'Adapter': 'IndexedDB',
'Store': 'GameData',
'Endpoint': '',
},
});| Option | Default | Starter game | What it is |
|---|---|---|---|
Adapter |
'LocalStorage' |
'IndexedDB' |
Where the data goes: 'IndexedDB', 'LocalStorage', 'SessionStorage', 'RemoteStorage' or 'FileSystem'. Any other name means 'IndexedDB'. |
Store |
'GameData' |
same | The name of the container inside the storage. Don’t change it once your game is released: with the other adapters old saves are no longer found, and with IndexedDB the game can’t add the new container to the player’s database, so it goes on with the old one and an error dialog asks you to change the name back. |
Endpoint |
'' |
same | The address of your server, for 'RemoteStorage' only. |
The game sets up its storage when js/options.js reaches its monogatari.preferences({ … }) call, from the Name, Version and Storage settings at that moment. A change to any of them after that, in js/script.js for example, isn’t seen by the storage. Keep all three in the monogatari.settings({ … }) call at the top of js/options.js, above monogatari.preferences({ … }).
What the storage holds
| Entry | What it is |
|---|---|
Settings |
The player’s preferences. |
Save_… |
Each manual save. |
AutoSave_1 … |
Each autosave slot. |
Two more browser databases sit next to it, whatever adapter you choose: <name>_Screenshots keeps the pictures of save slots, and <name>_AudioCache keeps the music and sounds loaded by the preload statement or a default preload block, already decoded, for the next visit. <name> is the storage name described below.
Compare the adapters
| Adapter | Where the data is | Kept | Needs |
|---|---|---|---|
IndexedDB (starter game) |
A database in the player’s browser. | Until the player clears the site’s data. | Nothing. Works served and opened from a file. |
LocalStorage (engine default) |
The browser’s local storage, a few megabytes per website, shared by every game on it. | Until the player clears the site’s data. | Nothing. |
SessionStorage |
The browser’s session storage. | Until the player closes the tab. | Nothing. |
RemoteStorage |
Your own server. | As long as your server keeps it. | A server that answers the requests below. |
FileSystem |
Files on the player’s computer, through a desktop app. | As long as the files exist. | A desktop build made with the starter game’s Electron or Electrobun app. |
WARNING
In a private (incognito) window, the browser deletes everything the game stored when the window closes, with any browser-side adapter. Only RemoteStorage keeps a private window’s saves.
IndexedDB (the starter game’s choice)
IndexedDB is a database built into every current browser, with room for many saves. Each game gets its own database, named after its Name, and the version of that database is worked out from Version. Each entry keeps its own name in an id field, which you’ll see if you read Settings from code.
IndexedDB needs a name and a version of at least 0.0.1 (see The name and version of the storage). With an empty Name nothing is saved, and while you develop an error dialog says The game’s storage could not be opened. With a Version such as '0.0.0', the game still saves, and the dialog The Version “0.0.0” can’t be used with IndexedDB reminds you to set a real one before you release.
LocalStorage and SessionStorage
LocalStorage keeps each entry as a line of text in the browser’s local storage, under a name made of the storage name, the Store and the Version: my-visual-novel::GameData::0.1.0_Settings. It’s limited to a few megabytes for the whole website, which is enough for many saves but not for an unlimited number of them.
SessionStorage works the same way, but the browser keeps its data for one tab: it stays when the page reloads and is gone when the tab closes. A game opened in a new tab starts with no saves and default preferences. It suits a demo shown on a shared computer.
RemoteStorage (your own server)
RemoteStorage sends every read and write to a web server you run, so saves follow the player from one device to another. Endpoint is its address and Store is added to it:
monogatari.settings({
'Storage': {
'Adapter': 'RemoteStorage',
'Store': 'GameData',
'Endpoint': 'https://example.com/saves',
},
});With these values, the game reads the player’s preferences from https://example.com/saves/GameData/Settings. The game’s Name and Version are not part of the address.
The server must answer these requests. Every request carries the header Content-Type: application/json, and the bodies are JSON:
| Request | What the game wants | Answer |
|---|---|---|
GET {Endpoint}/{Store}/{key} |
One entry, such as Settings or Save_1795891887668. |
The entry’s JSON, or the status 404 when there’s no such entry. The game reads 404 on Settings as a first visit. |
GET {Endpoint}/{Store}/ |
Every entry, to list the saves. | One JSON object, each entry’s name pointing to its contents. {} when there are none. |
GET {Endpoint}/{Store}/?keys=true |
The names of all entries. | A JSON array of names. |
POST {Endpoint}/{Store}/{key} |
Store a new entry (a new save, or the preferences the first time). | The stored JSON. The save screen uses it to show the new slot at once. |
PUT {Endpoint}/{Store}/{key} |
Replace an entry (changed preferences, an overwritten save). The game reads the entry first, and sends POST instead when it gets 404. |
The stored JSON. |
DELETE {Endpoint}/{Store}/{key} |
Delete an entry (a deleted slot). | The deleted JSON, or an empty answer. Not plain text, which the game can’t read. |
DELETE {Endpoint}/{Store}/ |
Delete every entry. The game itself never asks for this. | An empty answer. |
The requests carry no player name and no login of their own, so the address is the same for every player. Your server has to tell players apart by itself, for example with the login cookie of a website the game is part of: the browser sends that site’s cookies when the game and the server share the same address.
Two rules of the browser apply:
- The starter game’s
index.htmlonly lets the game connect to its own address and tohttps://addresses. A server on another address must use https, or you must add it to theContent-Security-Policyline at the top ofindex.html. - A server on another address must allow the game’s address with CORS headers, including the
Content-Typeheader and theGET,POST,PUTandDELETEmethods, since the browser asks before each request.
NOTE
These requests were checked against a small test server that stores entries in memory, with the game and the server on the same address. Saving, loading, deleting a slot and changing a preference all worked.
FileSystem (desktop builds)
'FileSystem' is for desktop builds: the game asks the desktop app to read and write its data as files on the player’s computer, one plain-text (JSON) file per save, plus one for the preferences. Players can back up those files or copy them to another computer. Both desktop apps in the starter game, Electron and Electrobun, answer these requests; Desktop says where each one puts the files.
'Storage': {
'Adapter': 'FileSystem',
'Store': 'GameData',
'Endpoint': ''
},- In a browser, with no desktop app, the game uses IndexedDB instead, and while you develop the error dialog FileSystem storage requires a desktop environment says so.
- In a desktop app that doesn’t answer (one whose
engine/electron/files come from before Monogatari 2.9.0), the game waits 5 seconds for the first answer, then keeps its data in IndexedDB from then on, and the error dialog The desktop app did not answer a storage request explains why.
monogatari.upgrade() works with FileSystem: the files remember the Version that wrote them, and the next start with a newer Version runs the upgrades in between, once.
The messages a desktop host must answer for FileSystem
This is for developers writing their own desktop host; the starter game’s hosts answer them with engine/desktop/storage.js. The game finds the host through window.electron or window.electrobun, an object with send(channel, data) and on(channel, callback). For each operation it sends a message whose data includes a requestId, plus name and store (the storage name described below and the Store setting, which tell the host which game’s data it is), and waits for an answer on the channel monogatari:storage-response with the same requestId, and either data or error:
| Channel | Data sent | Answer |
|---|---|---|
monogatari:storage-get |
key |
data: the value. For a key that doesn’t exist, error with any message: the game reads it as “not found”. |
monogatari:storage-set |
key, value |
Any data once written. |
monogatari:storage-update |
key, value |
Any data once written. When both the stored value and value are objects, the browser adapters and the starter’s hosts merge them, key by key; otherwise value replaces the entry. |
monogatari:storage-remove |
key |
Any answer once removed. |
monogatari:storage-get-all |
nothing | data: an object of every key and value. |
monogatari:storage-keys |
nothing | data: an array of keys. |
monogatari:storage-contains |
key |
An empty answer when the key exists, error when it doesn’t. |
monogatari:storage-clear |
nothing | Any answer once cleared. |
A message unanswered for 5 seconds makes the game give up on the host and use IndexedDB, as described above. The storage keeps one entry of its own, __monogatari_storage_version__, with the Version that last opened it; keys() and getAll() never list it.
The name and version of the storage
The storage is named after your Name setting, lowercased, with accents removed, spaces turned into - and any character other than Latin letters, digits, _ and - dropped: 'My Visual Novel' becomes my-visual-novel.
A Name with no Latin letters or digits at all, such as '物語', keeps its own letters instead: its storage is named 物語. A name made only of symbols or emoji is named after their character codes ('🌸' gives game-1f338).
NOTE
Before 2.9.0, such a name gave the storage an empty name: with IndexedDB nothing was saved, and LocalStorage kept the entries with no game name in front of them (Settings, Save_1). The first time a 2.9.0 game with LocalStorage opens, it copies those entries into its own storage, so the players keep their saves and preferences.
Version names the version of the stored data. Raise it with each release, in semantic versioning ('1.0.0', then '1.1.0'), and the player’s saves come along:
- with IndexedDB, the game opens the same database with the new version number;
- with LocalStorage and SessionStorage, the game copies the old entries to names with the new version, then removes the old ones.
Never lower it once your game is released. If it goes down, the game goes on with the player’s data at the higher version it was stored with, so the saves stay, and while you develop the error dialog The Version … is lower than the version of the saved data names both versions. Set Version back to that one or higher.
Keep the player’s data
On the web, the game asks the browser to keep its data even when the device runs low on space. The browser decides; when it says no, the console shows Persistent Storage permission has been denied. When your device gets low on storage, it may choose to delete your game files. The game works either way. Desktop and Cordova apps don’t ask.
Releasing a new version
Raising Version is enough for saves to keep working, as long as your script keeps its labels (see What a save keeps). When an update changes the shape of your variables, monogatari.upgrade() can rewrite the stored data the first time a player opens the new version:
monogatari.upgrade('0.1.0', '0.2.0', {
storage: (oldData) => {
const newData = {};
for (const [key, value] of Object.entries(oldData)) {
if (key.startsWith('Save_')) {
newData[key] = { ...value, name: value.name + ' (from 0.1.0)' };
}
}
return newData;
},
});storage receives every stored entry, its name pointing to its contents, and returns the entries to write back. Entries it doesn’t return stay as they were, unless you add replaceStorage: true next to storage, which removes them. The first version must be the exact version the player’s data was stored with. The Engine API describes the call in full.
Put the call in any of your game’s files (js/options.js, js/script.js or js/main.js), outside the function you give to monogatari.init(): the upgrade runs when the game starts and opens its storage. A call made after that never runs, and an error dialog says so.
- It works with
'IndexedDB'(the starter game’s adapter) and'LocalStorage'. With IndexedDB,storagemust return the entries themselves, 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 while you develop the dialog The upgrade of the saved data to version … failed shows the error. - With
'SessionStorage'the function is never run, and with'RemoteStorage'the browser’s console shows a warning. Upgrade the data on your server instead.
NOTE
Before 2.9.0, an upgrade only worked with LocalStorage, and only if it was called before monogatari.preferences({ … }) in js/options.js. With IndexedDB it left the player with a blank page.
A player with the old version still open
With IndexedDB, a new Version changes the database the player’s data is in, and the browser can’t change it while the old version is open in another tab or window. The game handles it:
- A tab running Monogatari 2.9.0 or later lets go of the data at once and asks its player to reload: This game was updated in another tab or window. Reload this page to keep playing. Clicking the message reloads the page.
- A tab running an older Monogatari can’t, so the new version waits before its main menu and says This game is open in another tab or window, in an older version. Close it to go on. It starts as soon as the player closes the old tab.
Related pages
- Saving: the saves themselves.
- Player Preferences: the
Settingsentry. - Game Settings:
Name,Versionand the other settings.