Actions
Overview
Actions are what defines what any given statement in a script should do. When Monogatari reads a part of the script (a statement), it looks for an action that matches the statement and runs it.
Creating a Custom Action
The following code displays a simple example of an action that does something when applied (advancing through the game) and something else when reverted (rolling back through the game):
class MyAction extends Monogatari.Action {
// Unique identifier for this action
static id = 'MyAction';
// Match statements starting with 'myaction'
static matchString([action]) {
return action === 'myaction';
}
constructor([myaction, ...args]) {
super();
this.args = args;
}
apply() {
// Do something when the action is executed
console.log('Action applied with args:', this.args);
return Promise.resolve();
}
didApply() {
// Return whether to advance automatically
return Promise.resolve({ advance: true });
}
revert() {
// Undo the action when rolling back
console.log('Action reverted');
return Promise.resolve();
}
didRevert() {
return Promise.resolve({ advance: true, step: true });
}
}This action will match statements like:
Registering an Action
Once you have your action class, register it with Monogatari:
Static Properties
id
string
Unique identifier for the action (required)
_experimental
boolean
Marks the action as experimental (default: false)
_configuration
object
Action-specific configuration storage
loadingOrder
number
Order for loading actions (default: 0). Higher values load first.
engine
Monogatari
Reference to the Monogatari engine (set automatically)
Instance Properties
_statement
string|object
The original statement that matched this action
_cycle
string
Current cycle: 'Application' or 'Revert'
_extras
object
Additional context passed to the action
engine
Monogatari
Reference to the Monogatari engine
Matching Methods
Actions must implement at least one matching method to identify which statements they handle.
matchString
For string-based statements (most common):
The statement is passed as an array of words split by spaces.
matchObject
For object-based statements (JSON):
Configuration
Actions can store and retrieve configuration:
Complete Example
Here's a more complete example of a custom action:
Usage in script:
Related
Life Cycle - Detailed explanation of action lifecycle methods
Components - Creating custom UI components
Last updated
Was this helpful?