Life Cycle

An action describes the functionality for a Monogatari statement. When Monogatari reads a part of the script (a statement), it will look for an action that matches the statement and run it.

The life cycle of an action is divided in three parts: Mounting, Application, and Reverting.

Mounting Cycle

The mounting cycle runs once when the engine initializes. It has 3 steps:

1. Setup

Here the action sets up everything it needs for working. This includes:

  • Registering state variables

  • Registering history objects

  • Adding HTML content to the document

static async setup(selector) {
    // Register a history for tracking applied actions
    this.engine.history('myaction');
    
    // Register state variables
    this.engine.state({
        myActionActive: false
    });
}

2. Bind

Once setup is complete, bind event listeners or perform DOM operations:

3. Init

Final initialization after setup and binding are complete:

Statement Matching

Before executing an action, Monogatari checks if the current statement matches. Actions must implement matching functions:

matchString

For string statements (most common). Receives the statement split into an array by spaces:

matchObject

For object/JSON statements:

Application Cycle

The Application cycle runs when an action is executed from the script.

1. Will Apply

Called before the action is applied. Use for pre-application checks or setup:

If this method throws or returns a rejected promise, the action will not be applied.

2. Apply

The core logic of the action. This is where the actual work happens:

3. Did Apply

Called after the action is applied. Use for cleanup, history updates, and determining if the game should advance:

Revert Cycle

The Revert cycle runs when the player goes back in the game.

1. Will Revert

Called before reverting. Check if reversion is possible:

If this method throws or returns a rejected promise, the action will not be reverted.

2. Revert

Undo the action's effects:

3. Did Revert

Called after reverting. Cleanup and determine navigation behavior:

Flow Control Methods

These static methods are called by the engine to check if the game can proceed or rollback:

shouldProceed

Called when the user clicks to advance or auto-play triggers:

willProceed

Called after shouldProceed passes, before advancing:

shouldRollback

Called when the user tries to go back:

willRollback

Called after shouldRollback passes, before reverting:

Event Methods

These static methods respond to game events:

onStart

Called when the player starts a new game:

onLoad

Called when a saved game is loaded. Restore visual/audio state:

onSave

Called when the game is saved:

reset

Called when a game ends or before loading a new game:

Hook Methods

These static methods allow intercepting action execution:

beforeRun / afterRun

Called before/after an action's application cycle:

beforeRevert / afterRevert

Called before/after an action's revert cycle:

Instance Methods

interrupt

Called to interrupt an ongoing action (e.g., skipping typewriter animation):

Helper Methods

Complete Life Cycle Diagram

Last updated

Was this helpful?