Engine API
The engine is one JavaScript object, monogatari. Your game talks to it from its own files: js/options.js hands it the settings, js/storage.js declares the game’s variables, js/script.js gives it the characters, the assets and the script, and js/main.js starts it with monogatari.init(). This section lists everything that object offers, grouped by what you use it for.
You don’t need any tools for this. Every example in this section is plain JavaScript that goes in one of your game’s js/*.js files, the same files the starter game already has, and runs when the browser opens index.html. If you haven’t made a game yet, start with Start Here: it shows where those files are and how to edit them.
This section documents Monogatari 2.9.0.
Where the monogatari object comes from
The starter game’s index.html loads the debug module (engine/debug/debug.js), then the engine (engine/core/monogatari.js), then your own files. The engine file creates two global names your files can use:
| Name | What it is |
|---|---|
monogatari |
The engine: the object every page in this section describes. |
Monogatari |
Everything else the engine file brings. Monogatari.default is the engine again, the same object as monogatari. |
What Monogatari holds:
| Names | What they are |
|---|---|
Action, Component, ShadowComponent, ScreenComponent, MenuComponent |
The base classes for your own actions and components (see Extending Monogatari). |
AudioPlayer |
The class that plays music, sound and voice (see Assets and Audio). |
FancyError |
The error dialogs of the debug module (see Debugging). |
$_, $_ready, $_create, DOM, Util, Text, Platform, Preload, Request, Form, FileSystem, Space, SpaceAdapter, Debug, DebugLevel, the LocalStorage, SessionStorage, IndexedDB and RemoteStorage adapters and their error classes |
The helpers of Artemis, the library the engine is built on. $_ready runs a function once the page has loaded; $_ finds elements in the page. |
Luxon |
The Luxon date library the engine formats save dates with. |
RandomJS |
The random number library behind random(). |
tsParticles, loadSlim |
The particles library behind the show particles statement. |
This is why the starter game’s js/main.js has this line near its top:
const { $_ready, $_ } = Monogatari;If you build your game as a JavaScript project with a bundler instead, the engine is the default export of the @monogatari/core package. That path is optional and needs extra tools: see Using a Bundler.
When your code can call what
The browser reads index.html from the top. It runs the files in its <head> one after the other (the debug module, the engine, js/options.js, js/storage.js, js/script.js, js/main.js) before it has read the <body>, where the game’s elements are. What you can call depends on when your code runs:
- At the top of your files (everything outside
$_ready(…)): the page’s elements don’t exist yet. Hand the engine what the game is made of: settings, preferences, variables, the script, characters, assets, translations, your own actions and components. These calls only store what you give them. - Inside
$_ready(() => { … })injs/main.js, beforemonogatari.init(): the page has loaded and the<visual-novel>element exists, so this is where event listeners go (on()). Anything that reaches the game’s elements needs this point at least. - After
init()resolved (insidemonogatari.init('#monogatari').then(() => { … })): the engine is set up and the player’s saved preferences have been read. In a served game the loading screen is showing while the assets load, and the main menu appears once they finish. Methods that move the game, open screens or save belong here, or in code that runs while the game plays (an event listener, a function in your script, a button’s listener).
// js/main.js
const { $_ready, $_ } = Monogatari;
// 1. The page is still loading: describe the game
monogatari.debug.level(5);
$_ready(() => {
// 2. The page has loaded: listen to events
monogatari.on('didInit', () => {
console.log('The engine finished setting up');
});
monogatari.init('#monogatari').then(() => {
// 3. The engine runs
});
});Open the game and the browser console prints The engine finished setting up.
How the methods behave
A few patterns repeat across the whole API:
- One method reads and writes.
monogatari.storage()returns every variable,monogatari.storage('points')returns one, andmonogatari.storage({ points: 3 })changes it. The same goes forstate(),settings(),characters(),assets()and most of the others. Each method’s section says which forms it has. - Objects merge. Giving an object to
settings(),preferences(),storage(),state(),characters()orconfiguration()merges it into what’s there: the keys you don’t mention keep their values. - Methods that take time return a promise.
init(),proceed(),rollback(),saveTo(),loadFromSlot()and the rest resolve when they finished. Use.then()or, inside anasyncfunction,await. - A refused move rejects.
proceed()rejects when something holds the game: a line still typing, a choice waiting for a pick, a menu open. See The Script and Its Flow. thisis the engine in script functions. A function in your script runs with the engine asthis, sothis.storage('points')works there.- A misspelled name is an error.
setting('Nmae')andpreference('Nmae')throw an error; most other readers returnundefined.
The pages
- Setting Up and Starting
init(),settings(),preferences(),configuration(),script(),label(), placeholders with$(),version, and the stepsinit()runs. - The Script and Its Flow
proceed(),rollback(),run(),next(), auto-play, skip, distraction-free mode, the checks that hold the game,resetGame()andrandom(). - State and Storage
state(),storage(),history(),global(),temp(),object()and variables in text. - Saving and Loading
saveTo(),loadFromSlot(), slot keys, what a save holds,upgrade(), theStorageobject and save screenshots. - Screens and Interface
showScreen(),alert(), the game’s elements, buttons withregisterListener()and keyboard shortcuts. - Assets and Audio
assets(),asset(),preload(), the caches, the media players and the main menu’s music. - Characters
characters()andcharacter(), and every property a character can have. - Translations
translation(),translations(),string(),translate(),localize()andlanguageMetadata(). - Events
on(),off(),trigger(), and every event the engine fires. - Debugging
The debug module,
debugand its levels, andFancyError. - Registering Actions and Components
registerAction(),registerComponent()and the methods that look them up. - Using a Bundler
Optional, needs Node or Bun: the
@monogatari/corenpm package and its entry points.
Internal members
The engine object also carries members that hold its own bookkeeping. They are not part of the API: they can change in any release, and changing them by hand skips the work the public methods do (merging, events, saving). Use the method in the last column instead.
| Member | What it holds | Use instead |
|---|---|---|
_settings, _preferences |
The settings and the player’s preferences. | settings(), preferences() |
_state, _storage, _history, _globals, _temp |
The stores. | state(), storage(), history(), global(), temp() |
_script, _characters, _assets, _translations, _languageMetadata, _configuration |
What the game registered. | script(), characters(), assets(), translations(), languageMetadata(), configuration() |
_actions, _components |
The registered actions and components. | actions(), components() |
_listeners |
The listeners registered by name. | registerListener() |
_$, _functions, _templates |
What $(), fn() and template() stored. |
$(), fn(), template() |
_mediaPlayers |
The players of music, sound, voice and video. | mediaPlayers() |
_audioBufferCache, _imageCache, _audioBufferSpace, _screenshotSpace, _indexedDBAvailable |
The asset caches and the IndexedDB databases behind them. | The cache methods in Assets and Audio |
_upgrade |
The storage upgrades. | upgrade() |
_pendingScreenshot, _hasCustomSaveScreenshot |
The screenshot taken when the save screen opened, and whether the game replaced onSaveScreenshot. |
Save screenshots |
_selector, _id |
The selector init() received, and the name 'visual-novel'. |
parentElement(), element() |
_events |
An empty object the engine never reads. Deprecated. | on() |
_setupRegistry() |
Connects the component registry’s error and mount reports to the engine. init() calls it. |
Nothing: init() does it. |
asEngine(), _blobToDataURL(), _onSaveScreenshot |
Private to the engine’s own code. | Nothing. |
Your game’s own data has a public home: variables in storage(), values that live only while the page is open in global().