Registering Actions and Components
Everything a script can say is an action: show scene, jump, a line of dialog. Every element of the interface is a component: the text box, the main menu, the save screen. The engine comes with 36 actions and 27 components, and a game can add its own or replace the built-in ones. This page lists the methods that do it. Extending Monogatari shows how to write an action or a component.
Actions
registerAction()
Adds an action to the engine.
registerAction(action: typeof Action, naturalPosition?: boolean): void| Parameter | Type | Default | What it does |
|---|---|---|---|
action |
class |
The action: a class that extends Monogatari.Action, with a unique id. |
|
naturalPosition |
boolean |
false |
Where the action goes in the list. false puts it first; true puts it last. |
For each statement, the engine asks the actions in order whether they recognize it, and runs the first one that does. An action registered with false (the default) is asked before every other, so it can even take over a statement a built-in action knows. An action registered with true goes after Dialog, which recognizes any text: it never gets a text statement, only objects.
Register actions before init(), at the top of js/main.js, so the engine runs their setup(), bind() and init() with the others. An action registered later still runs statements, but those three never run for it.
const { Action } = Monogatari;
class Stamp extends Action {
static id = 'Stamp';
static matchString([word]) {
return word === 'stamp';
}
async apply() {
this.engine.storage({ stamps: (this.engine.storage('stamps') || 0) + 1 });
}
async didApply() {
return { advance: true };
}
}
monogatari.registerAction(Stamp);The statement stamp now adds one to the stamps variable and goes on to the next statement.
unregisterAction()
Removes an action.
unregisterAction(id: string): voidThe id is matched without regard to case. Remove an action before init(): its statements then mean nothing to the engine (a text statement becomes a line of dialog).
action()
Returns an action’s class.
action(id: string): typeof Action | undefinedThe id is matched without regard to case. Some actions keep their own configuration in methods of their class, which is how the starter game’s js/script.js defines its messages and notifications:
monogatari.action('message').messages({
'Help': {
title: 'Help',
subtitle: 'Some useful Links',
body: '<p>…</p>',
},
});The pages of Script Actions describe each one: messages() for messages, notifications() for notifications, particles() for particles, objects() for canvas, blocks() for preload.
actions()
Returns the registered actions, in the order the engine asks them.
actions(): (typeof Action)[]Experimental actions are left out unless the ExperimentalFeatures setting is on. In 2.9.0 two of the 36 are experimental, Show::Character::Layer and Hide::Character::Layer, so the list has 34.
prepareAction()
Finds the action that recognizes a statement and creates it, without running it.
prepareAction(statement: string | object, options: { cycle: 'Application' | 'Revert'; extras?: object }): Action | nullA text statement has its variables replaced and is split into words first. It returns null when no action recognizes the statement (and for a function). cycle tells the action whether it will be applied or reverted. The engine calls it inside run() and revert(); a placeholder calls it too.
Components
registerComponent()
Adds a component to the engine, or replaces a built-in one.
registerComponent(component: typeof Component): voidThe component is a class that extends Monogatari.Component (or ShadowComponent, ScreenComponent, MenuComponent), with its tag in its static tag.
- Before
init(): the engine defines it with the others whileinit()runs. A component with the tag of one already registered replaces it, which is how a game replaces a built-in component with its own. - After
init(): a new tag is defined at once. A tag that’s already defined can’t be replaced any more: the engine opens an error dialog titledUnable to register component "…".
const { ScreenComponent } = Monogatari;
class HelpScreen extends ScreenComponent {
render() {
return `
<button class="top left" data-action="back"><span class="fas fa-arrow-left"></span></button>
<h2>How to play</h2>
<p>Click or press the right arrow to go on.</p>
`;
}
}
HelpScreen.tag = 'help-screen';
monogatari.registerComponent(HelpScreen);The main menu’s Help button now opens this screen instead of the built-in one.
unregisterComponent()
Removes a component.
unregisterComponent(tag: string): voidIt works only before init(). Afterwards the engine opens an error dialog titled Unable to unregister component "…" and keeps the component. Remove its element from index.html too: an element whose component isn’t registered stays empty.
component()
Returns a component’s class.
component(tag: string): typeof Component | undefinedThe tag is matched without regard to case.
components()
Returns the registered components.
components(): (typeof Component)[]Experimental components are left out unless the ExperimentalFeatures setting is on. None of the 27 built-in components is experimental in 2.9.0.