How Each Part Works
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.
What every page contains
Each page has the same nine parts, so you can go straight to the one you need:
- What it’s for, and where it sits in the architecture.
- The files: every file the part is made of, what it does, and the Cypress specs in
cypress/e2e/that cover it. - 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.
- The data: the shapes that matter, copied from the types in
src/lib/types/. - Rules and why: what this code keeps true, and what breaks when it doesn’t.
- How to make the common changes: short recipes with real code.
- Pitfalls and history: what broke before and why the code looks the way it does, from
git logand the comments in the code. v2 has no changelog file. - Exercises: real tasks, each with a hint and a check.
- 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 installThen, for each exercise:
Write the spec in
cypress/e2e/exercises/<name>.spec.js. Use the repository’s commands fromcypress/support/commands.js:cy.open()loads the starter game and keeps the engine asthis.monogatari(write tests asfunction () {}, not arrow functions, sothisworks),cy.start()presses Start,cy.proceed()andcy.rollback()move the game,cy.save(id)andcy.load(id)save to and load from the slotSave_<id>,cy.clearStorage()empties the storage engine, andcy.loadTestAssets()points the game at the test assets online.cy.open()doesn’t wait forinit()to finish: before reading anything the engine sets up while it mounts, press Start or wait for it withcy.get('@monogatari').invoke('global', '_didInit').should('equal', true). To run code beforeinit(), the way a game’sjs/main.jsdoes, Components and Screens shows how to visit the page with anonBeforeLoadcallback.If the exercise changes
src/, rebuild the bundle the spec loads:bun build.ts --browser. If it changesdebug/,bun build.ts --debug.Run the spec:
bunx cypress run --browser chromium --spec 'cypress/e2e/exercises/<name>.spec.js'--browser chromiumpicks Chromium; Testing lists the browsers Cypress can use. A spec takes a few seconds.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.
Related pages
- Architecture: the overview these pages go deeper into.
- Learning Path: when to read each page while you learn the engine.
- Testing: Cypress as v2 runs it.
- Reading the Code and Repository Layout: where each folder of
src/is and how to find your way. - Debugging the Engine: the tools for a bug you can’t place yet.
- Glossary: the engine’s terms.