Extending Monogatari
Monogatari is made of parts you can add to. Every statement in your script is run by an action, and every piece of interface on screen is a component. You can write actions and components of your own, change or replace the built-in ones, and change or translate the text of the engine’s buttons and messages.
These pages are for you once you’re comfortable with JavaScript classes. If you haven’t made a game with Monogatari yet, begin with Start Here: it explains the game folder, index.html and the script.
What you can add
-
New words for your script, like
'flash 200'or'location Palacio de Bellas Artes': what they do, whether the game moves on or waits for the player, and how they undo themselves when the player goes back. -
New pieces of interface, like an affection meter or a screen of your own, and changes to the built-in ones: the text box, the menus, the screens.
-
The text of the engine’s buttons, labels and messages, in any language, including the strings your own components use.
-
Artemis and Pandora, the two libraries Monogatari 2 is built on, and the parts of them your game can use.
-
Packaging an action or a component as files another game can add.
-
Optional: writing your extensions in a project built with Node.js or Bun and the npm package.
What you need
The same as for the rest of your game: a browser and a way to edit the text files in your game’s folder. Everything in this section is plain JavaScript in files that index.html already loads, so there’s nothing to install and no build step.
An editor on a phone or a tablet works too, as long as it can open your game’s folder, create and edit .js and .css files, and open index.html in a browser.
The examples use the scenes and characters of the tutorial: the Palacio de Bellas Artes in the afternoon and at night with its lamps lit (nightLight), and Yui and Mio. Any scene or character of your own works the same way.
Every example in this section works with the game opened from a file as well as served from a web host. Other features of Monogatari don’t; What works opened from a file lists them.
Where your code goes
Open your game’s index.html. In its <head>, it loads the debug module, the engine and then your game’s own scripts, in this order:
<!-- Debug Library. Remove this file for release builds -->
<script src="./engine/debug/debug.js"></script>
<!-- Monogatari JavaScript Libraries -->
<script src="./engine/core/monogatari.js"></script>
<script src="./js/options.js"></script>
<script src="./js/storage.js"></script>
<script src="./js/script.js"></script>
<script src="./js/main.js"></script>Once engine/core/monogatari.js has loaded, every later script can use two names:
| Name | What it is |
|---|---|
monogatari |
Your game’s engine: monogatari.script(), monogatari.registerAction(), monogatari.storage() and the rest of the Engine API. |
Monogatari |
Everything you build extensions from: the base classes Monogatari.Action, Monogatari.Component, Monogatari.ScreenComponent and Monogatari.ShadowComponent, Monogatari.FancyError, and the core libraries’ tools such as Monogatari.$_. |
js/main.js explains, in the comment at its top, that it has three places for code. Your actions and components go in the first one, above the $_ready(…) call. That part of the file runs before monogatari.init(), which is the moment the engine sets up everything registered so far:
'use strict';
/* global Monogatari, monogatari */
const { $_ready, $_ } = Monogatari;
// 1. Outside the $_ready function:
// Your actions and components go here.
monogatari.debug.level(5);
$_ready(() => {
// 2. Inside the $_ready function:
monogatari.init('#monogatari').then(() => {
// 3. Inside the init function:
});
});The third place, inside .then(…), runs once the engine has set itself up and shows the main menu. A few changes to built-in components can only be made there, and their pages say so.
Once your extensions grow, move them into a file of their own, such as js/extensions.js, and load it with a <script> tag of its own between js/script.js and js/main.js in index.html:
<script src="./js/script.js"></script>
<script src="./js/extensions.js"></script>
<script src="./js/main.js"></script>WARNING
Code written straight into index.html between <script> and </script> doesn’t run. The <meta http-equiv="Content-Security-Policy"> tag near the top of index.html only allows script files, and the browser’s console reports “Executing inline script violates the following Content Security Policy directive”. Keep your code in .js files.
The scripts index.html loads share their top-level names. js/main.js already declares $_ready and $_ with const, so a second const { $_ } = Monogatari; in js/extensions.js stops the page with “Identifier ‘$_’ has already been declared”, and the game never starts. The examples in this section write Monogatari.Action and Monogatari.Component in full, so they declare nothing but their own class names: give every class a name of its own.
How the parts fit together
- An action runs one kind of statement. It decides whether the game moves on by itself or waits for the player, and how to undo what it did when the player goes back.
- A component is a custom HTML element with its own markup and behavior. Some live in
index.htmlfor the whole game; others are created by an action while a statement needs them.
Learn from the engine’s own code
Every built-in statement and every piece of the interface is written the way yours are. The engine’s source for this version is on GitHub, at Monogatari 2.9.0: one file per action in src/actions/ and one folder per component in src/components/. They’re written in TypeScript, so they carry type annotations and the word override; leave those out and the rest reads as the JavaScript these pages use.
With the debug module loaded and monogatari.debug.level(5) in js/main.js, as the starter game has them, the browser’s console shows a collapsed group for every statement the engine runs, named after the action that ran it (Run Cycle [Location]), and one for every statement it undoes (Revert Cycle [Location]). When Something Goes Wrong shows how to open the console.