The Script and Its Flow
The engine plays a label one statement at a time. It keeps its place in two values of the state: label, the name of the label it is in, and step, the position of the statement in that label, counting from 0. Moving forward runs the next statement; going back undoes the previous one. These are the methods that do it.
Move the game forward
proceed()
Advances the game to the next statement, if nothing holds it. A click on the game screen and the right arrow and space keys call it; your code can too.
proceed(options?: { userInitiated?: boolean; skip?: boolean; autoPlay?: boolean }): Promise<void>| Option | Type | Default | What it says |
|---|---|---|---|
userInitiated |
boolean |
false |
The player asked for the move (a click, a key). |
skip |
boolean |
false |
Skip mode asked for it. |
autoPlay |
boolean |
false |
Auto-play asked for it. |
The options don’t change what proceed() does; they tell the actions who is asking, and an action may answer differently. A voice line is the example: while one plays, a move with neither userInitiated nor skip is refused, so auto-play waits for the voice to end. Pass { userInitiated: true } when your code moves the game because the player did something.
First, every action and component is asked whether the game may move (the checks). If one refuses, the promise rejects with its reason and nothing moves. Otherwise the engine runs the next statement, and the promise resolves once that statement has run. A statement that goes on by itself (a scene change, a sound) runs the one after it too, until a statement waits for the player.
The promise also resolves when the statement fails: the error dialog says why, and the game waits on the failed statement, so the next proceed() goes on with the one after it (When a statement fails).
monogatari.registerListener('next-line', {
callback: () => {
monogatari.proceed({ userInitiated: true }).catch((reason) => {
console.log('The game did not advance:', reason);
});
},
});A button with data-action="next-line" now advances the dialog. registerListener() explains where in the page such a button can go.
While a line of dialog is still typing, proceed() finishes the line instead of advancing, and rejects with TypeWriter effect has not finished. The next call advances. That’s why the first click on a line shows all of it and the second goes on.
A call made while another proceed() or rollback() is still asking the actions is refused, with Another proceed or rollback is being checked.: a double click moves the game once. Before 2.9.0, two calls made together could both advance and skip a statement.
Why a move is refused
A refused proceed() rejects with one of these reasons. Most are Error objects; read the text with String(reason) or reason.message.
| Reason | What holds the game |
|---|---|
Extra condition check failed. (a plain string) |
One of the engine’s own conditions: a dialog with the modal class is open (an alert, a message, an error dialog), distraction-free mode is on, another proceed() or rollback() is still running, or the deprecated block global is set. |
Another proceed or rollback is being checked. |
Another proceed() or rollback() started a moment earlier and is still asking the actions and components. |
Game screen is not visible. |
The game screen is hidden: the main menu or another screen is open. |
TypeWriter effect has not finished. |
A line was still typing. The call finished it. |
Choice is waiting for input |
A choice waits for a pick. |
Waiting for user input |
An input waits for an answer. |
Message waiting for dismissal |
A message is open. |
Wait period has not ended |
A wait statement is counting down. |
Video is still playing, Playing video must end before proceeding. |
A video shown as immersive or modal must end first. |
Canvas "name" must be removed before proceeding. |
A canvas shown as immersive or modal is on the screen. |
Contitional is still running action |
A conditional is still running the statement it chose. (The spelling is the engine’s.) |
Voice player still playing. |
A voice line plays, and the move came from neither the player nor skip mode (auto-play, or a proceed() called without options). |
With the debug level at 4 or 5, the console also prints which action or component refused, in the shouldProceed Check group.
next()
Runs the next statement without asking anything.
next(): Promise<void>It adds one to step and runs the statement there. It skips every check proceed() makes: called while a choice waits, it runs the statement after the choice anyway. At the last statement of a label it runs nothing, opens the The label “…” ran out of statements dialog and stays where it is. The engine calls it after a statement that goes on by itself. Your code should call proceed().
run()
Runs one statement now, wherever the game is.
run(statement: string | object | function, shouldAdvance?: boolean): Promise<{ advance: boolean }>| Parameter | Type | Default | What it does |
|---|---|---|---|
statement |
string, object or function |
The statement, written as it would be in the script. | |
shouldAdvance |
boolean |
true |
Whether to go on to the next statement of the label when the statement says it goes on by itself. Pass false to run it and stay where you are. |
It resolves with { advance }: whether the statement goes on by itself. It rejects when the statement fails: with Statement was null. for null, with The action did not match any of the ones registered. for an object no action recognizes (the No action can run this statement dialog), and with the action’s own error otherwise. The error dialog shows in every case, so catch the rejection (.catch(() => {})) when you have nothing else to do with it. A text statement always runs: one no other action recognizes is a line of dialog.
monogatari.run('y I almost forgot.', false);Yui says the line, and the game stays at the statement it was on. run() doesn’t change step (a jump does, because that’s what it does). The line isn’t part of the label, so going back doesn’t undo it: rollback() works through the label’s own statements.
The engine uses run() itself: the Start button runs the first statement of the starting label with it, and loadFromSlot() leaves the game ready for it.
Go back
rollback()
Takes the game back one statement, if nothing holds it. The left arrow key and the quick menu’s Back button call it.
rollback(): Promise<void>It does nothing (and resolves) when:
- the
AllowRollbacksetting isfalse; - the game is at the first statement of the starting label, with nothing before it.
Otherwise every action and component is asked whether the game may go back. The promise rejects when one refuses, or with Extra condition check failed. when distraction-free mode is on, another proceed() or rollback() is still running, or the deprecated block global is set. Then the engine undoes the previous statement through its action’s revert(). An action can ask to keep going: undoing a scene change also undoes the statement before it, and so on, until a statement that waits for the player (a line of dialog, a choice) is back on the screen.
Going back across a jump returns to the label the game jumped from. A plain function in the script can’t be undone, so going back stops at the statement after it; JavaScript Functions shows how to write one that can. A statement that failed when it ran has nothing to undo: going back steps over it to the one before.
previous()
Undoes the previous statement without asking anything.
previous(): Promise<void>Like next(), it skips the checks, and an action that can’t be undone at that moment leaves the game where it was (with an error on the console). Your code should call rollback().
revert()
The step under rollback() and previous(): undoes one statement and fires willRevertAction and didRevertAction.
revert(statement?: any, shouldAdvance?: boolean, shouldStepBack?: boolean): Promise<{ advance: boolean; step: boolean } | void>With no statement, it undoes the one before the current step. shouldAdvance (default true) lets it go on undoing when the action asks for it; shouldStepBack (default true) lets it move step back. The engine calls it; a game calls rollback().
Auto-play, skip and distraction-free
autoPlay()
Turns auto-play on or off. With it on, the game advances by itself.
autoPlay(enable: boolean): voidWith true, it calls proceed({ autoPlay: true }) every few seconds: the player’s AutoPlaySpeed preference (5 by default). A refused move is tried again at the next tick, so auto-play waits through choices and inputs until the player answers. The quick menu’s Auto button turns it on and off, and its label changes to Stop while it runs. false stops it at once: no statement advances after it.
Opening any screen with showScreen() (the save screen, the settings) stops auto-play too.
monogatari.preference('AutoPlaySpeed', 2);
monogatari.autoPlay(true);The game advances every two seconds.
skip()
Turns skip mode on or off. With it on, the game advances fast.
skip(enable: boolean): voidWith true, it calls proceed({ skip: true }) every Skip milliseconds (the setting) while the game screen shows. With the Skip setting at 0 (the starter game’s value), skipping is off for the game, and skip(true) does nothing. Skipping stops at whatever waits for the player, and opening a screen turns it off. false stops it.
monogatari.setting('Skip', 100);
monogatari.skip(true);The quick menu’s Skip button and the S key turn skipping on, and off again. The button is only there when Skip is above 0 as the game starts. Read whether it’s on with monogatari.global('skip'): the skip timer while skipping, undefined when it’s off.
NOTE
Before 2.9.0, the Skip button and the S key couldn’t turn skipping on; only skip(true) from code worked.
distractionFree()
Hides or shows the text box, so the player can look at the scene.
distractionFree(): voidEach call switches the mode. It works only while the game is being played. While it’s on, the text box is hidden, the quick menu gets the transparent class, its Hide button reads Show, and proceed() is refused. The H key and the Hide button call it. Read whether it’s on with monogatari.global('distraction_free').
The checks that hold the game
proceed() and rollback() ask every registered action and component before they move. These are the methods that ask; they are public, so your code can ask too.
| Method | What it does |
|---|---|
shouldProceed(options) |
Checks the engine’s own conditions, then calls the static shouldProceed() of every action and component. Resolves if all of them agree; rejects with the first refusal (the reasons are in Why a move is refused). options is the same object proceed() takes. |
willProceed() |
Calls every action’s and component’s willProceed(), which prepares the move (a dialog clears its centered text, a voice stops). |
shouldRollback() |
The same check for going back. |
willRollback() |
Calls every action’s and component’s willRollback(). |
All four return Promise<unknown[]>. Asking isn’t always without effect: while a line is typing, shouldProceed() finishes it.
monogatari.shouldProceed({ userInitiated: true }).then(() => {
console.log('The game can advance');
}).catch((reason) => {
console.log('Held by:', String(reason));
});Extending Monogatari shows how your own action takes part in these checks.
Start over
resetGame()
Puts the game back to how a new game starts. The end statement calls it before it shows the main menu.
resetGame(): Promise<unknown[]>It stops auto-play and skip mode, sets step to 0 and label to the Label setting, empties the history, and runs every action’s reset() and every component’s onReset(), which clear the scene, the characters, the music and the rest. It puts the storage back to what it was when the game started (what js/storage.js declared): a variable your game added while it played, one that js/storage.js doesn’t declare, is removed. Before 2.9.0 such a variable kept its value after a reset.
It doesn’t change which screen shows: call it from the game screen and the game screen stays, empty. The promise resolves when every action and component has reset.
Random numbers
random()
Returns a random whole number between min and max, both included.
random(min: number, max: number): numberIt uses the browser’s cryptographic random source when there is one. A function in the script can use it to roll a die:
monogatari.storage({ roll: 0 });
monogatari.script({
'Start': [
function() {
this.storage({ roll: this.random(1, 6) });
},
'y You rolled a {{roll}}.',
'end',
],
});Yui says “You rolled a 4.” with a number from 1 to 6.
Helpers for script functions
assertAsync()
Runs a function and turns what it returns into a promise: the engine’s way to run the functions a choice’s Condition, an input’s Validation or Save and the like give it.
assertAsync(callable: function, self?: any, args?: any[]): Promise<void>| The function returns | The promise |
|---|---|
true |
resolves |
false |
rejects |
A promise that resolves to true, or to anything that isn’t a boolean |
resolves |
A promise that resolves to false, or rejects |
rejects |
| An object that isn’t a promise | resolves |
undefined, a string, a number |
rejects |
self is what this is inside the function (null by default) and args the arguments it gets. While the function runs, the deprecated block global is set, so the game can’t advance.
stopTyping()
Finishes the line a type writer is typing.
stopTyping(component: TypeWriterComponent): voidWith the InstantText setting on, the whole line appears at once; off, the rest of the line types at full speed. Either way didFinishTyping fires once it’s all there. The engine calls it when the player clicks during a line; proceed() does it for you.