A game describes itself to the engine before it starts: its settings, the player’s default preferences, its script and the configuration of its menus. Then init() sets up the engine on the page and shows the first screen. The starter game already makes these calls in js/options.js, js/script.js and js/main.js; this page lists what each one accepts.

Start the engine

init()

Starts the engine: it sets up every action and component, reads the player’s saved preferences, connects the buttons and keys, and shows the first screen.

init(selector?: string): Promise<void>
Parameter Type Default What it is
selector string '#monogatari' The CSS selector of the element that holds <visual-novel>: the <div id="monogatari"> of the starter game’s index.html.

Register everything the game is made of (settings, script, characters, assets, your own actions and components) before you call it, and call it once the page has loaded, inside $_ready(). The starter game does it in js/main.js:

const { $_ready, $_ } = Monogatari;

$_ready(() => {
  monogatari.init('#monogatari').then(() => {
    // The engine runs
  });
});

The promise resolves once the engine is set up and the first screen is on its way. At that moment the loading screen is showing while the assets load; the main menu (or the splash screen) appears when they finish. In a game in several languages that a player opens for the first time, the language selection screen shows instead, and the rest follows once they pick a language.

Call it once. A second call runs the whole start-up again: every component is defined a second time, which opens an error dialog titled Unable to register component "alert-modal", and the start-up events fire again.

What init() runs, step by step

init() calls these methods itself, in this order. A game doesn’t need to call them, but they are public, so you may see them in a plugin or in the code of an older game. The Events page lists the events fired along the way.

Method What it does
setup(selector) Reads the player’s saved preferences from the storage (the Settings key) over the ones js/options.js gave. Defines every registered component. Registers the service worker when the game is served and the ServiceWorkers setting is on. Remembers the variables storage() holds at this point, for resetGame(). Registers the built-in listeners (start, open-screen, dismiss-alert, distraction-free, skip, auto-play) and runs every component’s and action’s setup(). Returns Promise<void>. If a step fails, it prints Initialization error on the console and resolves anyway.
bind(selector) Connects the clicks on elements with a data-action attribute to their listeners, the back buttons of the menus, the built-in keyboard shortcuts and the ForceAspectRatio setting, then runs every component’s and action’s bind(). Returns Promise<void>.
displayInitialScreen() Runs preload(), then showSplashScreen(). If the script has no label named by the Label setting, it opens an error dialog instead.
showSplashScreen() Plays the label the SplashScreenLabel setting names (_SplashScreen by default) on the game screen, or calls showMainScreen() when the script has no such label.
showMainScreen() Shows the main menu. With the ShowMainScreen setting off, it starts the game at once instead.
onStart() Runs the onStart() of every component and action. The Start button calls it before the first statement runs. Returns a promise that rejects if any of them rejects.
onLoad() Runs the onLoad() of every action (in groups, by each action’s loadingOrder) and of every component. loadFromSlot() calls it to rebuild the screen from a save.

version

The engine’s version, as text.

version: string;
console.log(monogatari.version); // '2.9.0'

Settings, preferences and configuration

Three kinds of values shape a game:

  • Settings are yours: the game’s name, the label it starts at, the storage it saves to. The player can’t change them.
  • Preferences are the player’s: the language, the volumes, the text speed. The settings screen changes them, and the engine saves them.
  • Configuration describes parts of the interface: the buttons of the main menu and of the quick menu, and the credits.

Configuration lists every setting and preference, with its default and what it changes.

settings()

Reads every setting, or changes several at once.

settings(object?: Partial<GameSettings> | null): GameSettings

With an object, it merges the object into the settings: the settings you don’t name keep their values. It returns every setting either way. The starter game calls it at the top of js/options.js:

monogatari.settings({
  'Name': 'My Visual Novel',
  'Version': '0.1.0',
  'Label': 'Start',
});

Some settings are read only once, while the game starts: Storage, ServiceWorkers, AutoSave, ForceAspectRatio, Orientation and LanguageSelectionScreen among them. Set those in js/options.js. Others, such as Skip, TypeAnimation and InstantText, are read each time the engine needs them, so changing them while the game runs takes effect at once.

setting()

Reads or changes one setting.

setting(key: string): any
setting(key: string, value: any): any
monogatari.setting('Label'); // 'Start'
monogatari.setting('Skip', 200); // lets the player skip, at 200 ms per statement

It returns the setting’s value. Reading a setting that doesn’t exist throws an error, Tried to access non existent setting with name 'Nmae'., so a misspelled name stops your code right there. A value of null or undefined reads the setting instead of changing it.

preferences()

Reads the player’s preferences, or changes several at once.

preferences(object?: Partial<PlayerPreferences> | null, save?: boolean): PlayerPreferences
Parameter Type Default What it does
object object null The preferences to merge in.
save boolean false true also writes every preference to the storage, under the Settings key.

It returns every preference. The starter game calls it in js/options.js to give the preferences a player starts with:

monogatari.preferences({
  'Language': 'English',
  'TextSpeed': 20,
  'AutoPlaySpeed': 5,
});

These are defaults. Once a player has opened the game, their saved preferences replace them when init() runs, so changing TextSpeed in js/options.js doesn’t change it for a player who already played.

The first call also prepares the storage the game saves to, from the Storage, Name and Version settings. That’s why js/options.js calls settings() before preferences(). The storage opens later, when init() starts the game, so upgrade() can be called before or after preferences().

preference()

Reads or changes one preference.

preference(key: string): any
preference(key: string, value: any): any
monogatari.preference('TextSpeed'); // 20
monogatari.preference('TextSpeed', 40); // changes it and saves it

Changing a preference this way saves every preference to the storage right away. It returns the preference’s value. Reading one that doesn’t exist throws an error, Tried to access non existent preference with name 'Nmae'.

The preferences are Language, Volume (an object with Music, Voice, Sound and Video, from 0 to 1), Resolution, TextSpeed and AutoPlaySpeed. Changing Language from code doesn’t translate the interface by itself: call localize() after it.

configuration()

Reads or changes the configuration of a part of the interface.

configuration(): object
configuration(key: string): object
configuration(key: string, object: object): object
configuration(parts: object): object
Form What it does
configuration() Returns the whole configuration: an object with main-menu, quick-menu and credits.
configuration('credits') Returns one part.
configuration('credits', { … }) Merges the object into that part and returns it. It fires configurationElementWillUpdate, configurationElementUpdate::credits and configurationElementDidUpdate (see Events). For a menu ('main-menu', 'quick-menu'), the menu on screen redraws with its new buttons.
configuration({ credits: { … }, 'main-menu': { … } }) Merges each part as the form above does, with its events, between configurationWillUpdate and configurationDidUpdate, and returns the whole configuration.

The parts the engine reads:

Key What it holds Read by
main-menu buttons: the buttons of the main menu. <main-menu>
quick-menu buttons: the buttons of the quick menu. <quick-menu>
credits The sections of the credits screen. <credits-screen>

The starter game’s js/script.js already has an empty credits call. Fill it in, and the main menu gets a Credits button that opens a screen listing them:

monogatari.configuration('credits', {
  'Development': {
    'Writing': 'Ana',
    'Art': ['Luis', 'Mei'],
  },
});

Lists are joined with commas: the screen shows “Writing Ana” and “Art Luis, Mei” under the heading “Development”. The button is added while init() runs, so this call has to come before it.

An array inside the object replaces the array that was there: configuration('main-menu', { buttons: [ … ] }) replaces every button of the main menu. To change one button, read the list first. This takes the Help button out of the main menu:

const mainMenu = monogatari.configuration('main-menu');

monogatari.configuration('main-menu', {
  buttons: mainMenu.buttons.filter((button) => button.string !== 'Help'),
});

The Interface shows what a button object holds.

Before 2.9.0, configuration() with an object as its only argument fired its two events but changed nothing.

Give the engine the script

The script is the story: a set of labels, each a list of statements the engine runs one after the other. A statement is a line of text ('show scene forest', 'y Hello.'), an object ({ Choice: { … } }) or a function. The Script and Its Labels explains how to write one; these are the methods that hold it.

script()

Reads the script or adds labels to it.

script(): object
script(label: string): array | undefined
script(object: object): void
Form What it does
script() Returns every label, by name.
script('Start') Returns the statements of one label, or undefined when there is no such label.
script({ … }) Adds the labels the object holds. A label with the name of one that exists replaces it; the others stay.
monogatari.script({
  'Start': [
    'show scene #f7f6f6 with fadeIn',
    'y Where are we?',
    'end',
  ],
});

With the MultiLanguage setting on, the script holds one set of labels per language: the keys of the object are language names, and script() and script('Start') read the labels of the player’s language. If the script has none in that language, an error dialog names the languages it has.

monogatari.script({
  'English': {
    'Start': ['Hello.', 'end'],
  },
  'Español': {
    'Start': ['Hola.', 'end'],
  },
});

Each call replaces whole languages: a second script({ 'English': { … } }) replaces every English label the first one gave. To add one label to a language, use label().

label()

Reads a label, or adds one.

label(): array
label(name: string): array | undefined
label(name: string, statements: array): void
label(name: string, language: string): array | undefined
label(name: string, language: string, statements: array): void
Form What it does
label() Returns the statements of the label the game is in now (the label in state()).
label('Ending') Returns the statements of a label, like script('Ending').
label('Ending', [ … ]) Adds a label, or replaces the one with that name.
label('Ending', 'Español') Returns the statements of a label in one language of a multi-language script.
label('Ending', 'Español', [ … ]) Adds a label to one language of a multi-language script, leaving the language’s other labels as they are.
monogatari.label('Ending', [
  'y That was the last chapter.',
  'end',
]);

Placeholders

A placeholder is a statement you store under a name and run from the script with $ name. Use it for a statement you repeat: a long choice, a scene change with many options.

$(name: string, statement: any): any
$(name: string): any
$(object: object): void
$(): object
Form What it does
$('name', statement) Stores a statement and returns it.
$('name') Returns the stored statement.
$({ name: statement, … }) Stores several.
$() Returns every placeholder.
monogatari.$('sunset', 'show scene #f4a259 with fadeIn');

monogatari.script({
  'Start': [
    '$ sunset',
    'y The sun is going down.',
    'end',
  ],
});

The statement $ sunset runs show scene #f4a259 with fadeIn. A placeholder whose name starts with _ holds a function instead: the engine calls it with the words that follow its name and runs the statement it returns.

monogatari.$('_greet', (name) => `y Hello, ${name}.`);

// In the script: '$ _greet Ana' shows "Hello, Ana."

Placeholders has more on the statement.