Architecture shows the whole engine at once. The pages in this section take it apart: each one follows a single part of the engine through its code, in the order the code runs, until you can change it without breaking the rules it keeps. Read them after Architecture, with the repository open beside you; the Learning Path says when.

Like the rest of Working on Monogatari, these pages assume a computer with the repository’s toolchain (Setting Up).

The parts

The numbers are the reading order. The first four follow a statement from the script to the screen and back; the last four are the parts around it.

# Page What it covers Start reading at
1 Statements and Actions The Action class, how a statement finds its action, the run cycle, next() and proceed(), the engine’s start-up, input and listeners, auto-play and skip src/engine/lifecycle.ts, src/lib/Action.ts
2 State, Storage and Rollback The state, history, storage and globals, rollback through each action’s revert(), going back across labels, resetting a game src/monogatari.ts, revert() in src/engine/lifecycle.ts
3 Saving and Loading The storage engines, the save format and its slots, autosave, screenshots, loading a slot, migrations src/engine/persistence.ts
4 Text and the Type-Writer The Dialog action, the text box in ADV and NVL modes, the type-writer and its effects, centered text, the dialog log src/actions/Dialog.ts, src/components/type-writer/
5 Components and Screens Pandora 0.5 and the Component classes, registration, the host markup, screens, menus and modals src/lib/Component.ts, src/engine/ui.ts
6 Media and Assets Assets and AssetsPath, the audio player, music, sound, voice and video, images and sprites, preloading and the caches, the service worker src/engine/assets.ts, src/lib/AudioPlayer.ts
7 Translations and Strings The UI translations, string() and localize(), _(key) and {{variables}} in statements, multi-language scripts src/engine/i18n.ts, src/translations/
8 Errors and the Debug Module FancyError and its catalog, the debug module, the console logger and its levels, what reaches the developer src/lib/FancyError.ts, debug/index.js

The parts follow the code’s own seams. Input, listeners, auto-play and skip live in Statements and Actions because each of them ends in a call to proceed(). Sprites and character layers are in Media and Assets, where their files are resolved; the character’s name, color and side image are in Text and the Type-Writer, where Dialog uses them. Pandora is covered where the engine uses it (Components and Screens), and Artemis’s storage spaces in Saving and Loading. v2 has no plugin or hook system beyond registering actions, components, listeners and event handlers, so there’s no page for one: Extending Monogatari covers registration from the game’s side.

How the parts connect

Each arrow is something one part hands another. Follow one when a change in one page’s code surprises you in another’s.

flowchart LR statements["1 Statements and actions"] state["2 State, storage and rollback"] saving["3 Saving and loading"] text["4 Text and the type-writer"] components["5 Components and screens"] media["6 Media and assets"] i18n["7 Translations and strings"] errors["8 Errors and the debug module"] statements -- "rollback runs revert()<br/>on the previous statement" --> state state -- "a save is the state, history and storage;<br/>a load resets and runs the saved statement" --> saving statements -- "Dialog is the catch-all action" --> text statements -- "proceed() asks every component<br/>shouldProceed()" --> components text -- "the text box, the type-writer<br/>and the dialog log are components" --> components media -- "play, show and video are actions<br/>that record their statements in the state" --> statements i18n -- "_(key) and {{variables}} are filled in<br/>before a statement is matched" --> statements i18n -- "data-string elements are<br/>translated on every localize()" --> components statements -- "actions report mistakes,<br/>run() reports a failing function" --> errors components -- "Pandora's errors become<br/>engine:component:lifecycle_error" --> errors

What every page contains

Each page has the same nine parts, so you can go straight to the one you need:

  1. What it’s for, and where it sits in the architecture.
  2. The files: every file the part is made of, what it does, and the Cypress specs in cypress/e2e/ that cover it.
  3. How it works: the code path in the order it runs, with function names and at least one diagram. Function names don’t drift the way line numbers do: search for them.
  4. The data: the shapes that matter, copied from the types in src/lib/types/.
  5. Rules and why: what this code keeps true, and what breaks when it doesn’t.
  6. How to make the common changes: short recipes with real code.
  7. Pitfalls and history: what broke before and why the code looks the way it does, from git log and the comments in the code. v2 has no changelog file.
  8. Exercises: real tasks, each with a hint and a check.
  9. Related pages: the pages for game developers that describe the same feature from the outside.

Doing the exercises

v2’s tests are Cypress specs that load dist/index.html (Testing), so every exercise ends in a spec you write and run, or, when Cypress can’t reproduce the situation, a short Playwright script against a served copy of dist/. Each page’s solutions were run against v2.8.1 when the page was written, and fail without the change they teach where the exercise says so.

Work in a scratch copy, so rebuilt bundles and exercise specs never reach your branch:

git worktree add --detach ../monogatari-exercises HEAD
cd ../monogatari-exercises
bun install

Then, for each exercise:

  1. Write the spec in cypress/e2e/exercises/<name>.spec.js. Use the repository’s commands from cypress/support/commands.js: cy.open() loads the starter game and keeps the engine as this.monogatari (write tests as function () {}, not arrow functions, so this works), cy.start() presses Start, cy.proceed() and cy.rollback() move the game, cy.save(id) and cy.load(id) save to and load from the slot Save_<id>, cy.clearStorage() empties the storage engine, and cy.loadTestAssets() points the game at the test assets online. cy.open() doesn’t wait for init() to finish: before reading anything the engine sets up while it mounts, press Start or wait for it with cy.get('@monogatari').invoke('global', '_didInit').should('equal', true). To run code before init(), the way a game’s js/main.js does, Components and Screens shows how to visit the page with an onBeforeLoad callback.

  2. If the exercise changes src/, rebuild the bundle the spec loads: bun build.ts --browser. If it changes debug/, bun build.ts --debug.

  3. Run the spec:

    bunx cypress run --browser chromium --spec 'cypress/e2e/exercises/<name>.spec.js'

    --browser chromium picks Chromium; Testing lists the browsers Cypress can use. A spec takes a few seconds.

  4. Put the copy back before the next exercise:

    git checkout -- src debug dist
    git clean -fd src debug cypress/e2e/exercises

Try each exercise before you open its solution. The hint says where to look; the solution is in a “Solution” block under the exercise. If a solution fails for you, the engine changed since the page was written: find out what changed, and fix the page.

When you’re done, remove the copy with git worktree remove ../monogatari-exercises from your clone.