NOTE

This page is optional, and it needs a computer with Node.js or Bun installed and a terminal. Everything else in this section works with a browser and a text editor, in the starter game's js/ files.

Some developers build their game as a JavaScript project: they install Monogatari from npm, write their code in modules, and let a bundler (Bun, Vite, esbuild, webpack) produce the files the browser loads. Extensions work the same way there; only how you reach Monogatari changes. Installing the package and setting up the project are on Engine API.

Import instead of the globals

The package @monogatari/core exports the engine as its default export and everything on the Monogatari global as named exports. Where the pages in this section write Monogatari.Action and monogatari, import them:

import monogatari, { Action, $_ready } from '@monogatari/core';

class Flash extends Action {
  static id = 'Flash';

  static matchString([keyword]) {
    return keyword === 'flash';
  }

  constructor([keyword, duration]) {
    super();
    this.duration = parseInt(duration, 10) || 300;
  }

  async apply() {
    const gameScreen = this.engine.element().find('[data-screen="game"]');

    gameScreen.style('filter', 'brightness(3)');

    await new Promise((resolve) => {
      setTimeout(resolve, this.duration);
    });

    gameScreen.style('filter', '');
  }

  async didApply() {
    return { advance: true };
  }

  async didRevert() {
    return { advance: true, step: true };
  }
}

monogatari.registerAction(Flash);

monogatari.script({
  'Start': [
    'show scene #4a4e69',
    'flash 200',
    'What was that?',
    'end'
  ]
});

$_ready(() => {
  monogatari.init('#monogatari');
});

The named exports are Action, Component, ShadowComponent, ScreenComponent, MenuComponent, FancyError, AudioPlayer, every Artemis export ($_, $_ready, Text, Platform and the others), Luxon, RandomJS, tsParticles and loadSlim.

The rules are the ones the rest of this section gives: register your actions and components before monogatari.init(), and keep the page’s markup (<div id="monogatari"> with <visual-novel> and its screens, as in the starter game’s index.html) and the stylesheet, which the package exports as @monogatari/core/monogatari.css.

The package also includes TypeScript declarations for the engine and its classes.