Script and Labels
Your story lives in js/script.js, in a call to monogatari.script(). This page explains how that script is put together and how the game moves through it: from one statement to the next, from one label to another, and back to the main menu at the end.
All you need is the game folder and a text editor. If you haven’t opened the starter game yet, do Start Here first.
Labels and statements
The script is a list of labels. A label is a named list of statements, and a statement is one step of your story: a line of dialog, a background change, a choice. Think of labels as the chapters of a book and statements as its sentences.
monogatari.script({
'Start': [
'show scene afternoon with fadeIn',
'y Good afternoon!',
'jump Library',
],
'Library': [
'The library was empty.',
'end',
],
});This script has two labels, Start and Library. The game plays the statements of a label in order, one after the other.
Each label is written as its name in quotes, a colon, and its statements between square brackets. Every statement and every label ends with a comma.
A missing comma is the most common reason a script stops working. The browser can’t read the file at all, so the game has no story left. While you develop, a dialog titled “There is a mistake in js/script.js” opens when the page loads. Served, it gives the line of the mistake; opened from the file, the browser keeps the line for its console. When Something Goes Wrong shows how to find it.
Label names are case-sensitive: jump library doesn’t find the Library label.
Where the game starts
When the player presses Start, the game plays the label called Start. To start somewhere else, change the Label setting in js/options.js:
monogatari.settings({
'Label': 'Prologue',
});The starter game’s js/options.js already has a 'Label': 'Start' line: change its value instead of adding a second one.
The three kinds of statements
A statement is a string, an object or a function:
monogatari.script({
'Start': [
// A string: dialog, and most actions
'show scene afternoon',
'y Look, the doors are open.',
// An object: actions with several parts, like a choice
{
'Choice': {
'Stay': { 'Text': 'Stay a while', 'Do': 'jump Stay' },
'Leave': { 'Text': 'Go home', 'Do': 'jump Leave' },
},
},
],
'Stay': [
// A function: your own JavaScript, run at this point of the story
() => {
console.log('The player decided to stay');
},
'y I am glad you stayed.',
'end',
],
'Leave': [
'y See you tomorrow.',
'end',
],
});A string statement starts with the words that name its action: show scene, play music, jump, end… A string that doesn’t start that way is a line of dialog: see Writing Dialog, which also lists those words. Script Actions lists every statement you can write.
What makes the game move on
Some statements wait for the player and some don’t:
- A line of dialog waits. The game stays on it until the player clicks, taps or presses the space bar or the right arrow key.
- A choice waits until the player picks an option.
- Most other statements run and move on at once:
show scene,show character,play music,jump. Put a few of them in a row and the player sees them happen together, before the next line of dialog.
'Start': [
'show scene afternoon', // runs, moves on
'show character y happy at right', // runs, moves on
'play music theme with loop', // runs, moves on
'y Class ended an hour ago!', // waits for the player
'end',
],End every label
A label must end with a statement that says where to go next: end, or a jump to another label. When a label runs out of statements, the game stays on its last line, and a click opens a dialog titled The label "Start" ran out of statements (with the label’s name) instead of moving on.
Move to another label with jump
jump moves the game to the first statement of another label:
monogatari.script({
'Start': [
'You reach a fork in the road.',
'jump Forest',
],
'Forest': [
'The trees close in around you.',
'end',
],
});The player doesn’t notice the jump: after “You reach a fork in the road.”, the next click shows “The trees close in around you.”
- The text box is emptied by a jump, unless it’s showing a page of NVL text, which goes on across the jump.
- Going back after a jump returns to the label the player came from.
- A jump to a label that doesn’t exist opens a dialog titled
The label "Forest" does not exist, which lists the labels your script has. The game skips the jump: after you close the dialog, the next click shows the statement after it. Fix the name and reload the page.
Jump has the details.
End the game with end
end finishes the game: it goes back to the main menu, and every variable you declared in js/storage.js goes back to the value it starts with there, ready for the next game. Variables and Storage explains what a new game resets and what it keeps.
'GoodEnding': [
'y I am glad you came.',
'The End.',
'end',
],The Quit button of the quick menu does the same, after asking the player to confirm. End has the details.
Carry on without doing anything with next
next does nothing except move on to the next statement. Use it as the Do of a choice option that continues the story where it is:
{
'Choice': {
'Dialog': 'y Shall I tell you the story from the beginning?',
'Yes': { 'Text': 'Yes, please', 'Do': 'jump Prologue' },
'No': { 'Text': 'No, go on', 'Do': 'next' },
},
},
'y Alright, where was I...',Picking “No, go on” shows “Alright, where was I…” next.
A line of narration that starts with the word “next” (or any other action’s word) runs that action instead of being shown. Writing Dialog explains how to write it.
Run your own code with a function
A function in the script runs your JavaScript at that point of the story. Inside a function written with the function keyword, this is the engine, so this.storage(…) reads and changes your variables:
'Start': [
'You find a coin on the ground.',
function() {
const coins = this.storage('coins');
this.storage({ coins: coins + 1 });
},
'You now have {{coins}} coins.',
'end',
],This needs a coins variable declared in js/storage.js, like coins: 0. After the function runs, the game moves on to the next statement at once. Two return values change that:
false: the game waits for the player to click before it moves on, as on a line of dialog. The line on screen stays the one before the function.- A promise: the game waits for it to settle before it moves on. A promise that resolves to
falsealso makes it wait for the player.
In an arrow function (() => { … }), this isn’t the engine: write monogatari.storage(…) there instead.
WARNING
A function that throws an error opens a dialog titled "An error occurred while trying to run a Function." The rest of the function doesn't run, and the next click goes on with the statement after it. Fix the function and reload the page.
Going back past a function
The player can go back to earlier lines with the Back button of the quick menu or the left arrow key. Monogatari can’t undo what a function did, so it doesn’t let the player go back past one: pressing Back on the line after a function shows that line again.
To let the player go back, write the function as a Function object with an Apply, which runs going forward, and a Revert, which undoes it going back:
{
'Function': {
'Apply': function() {
const coins = this.storage('coins');
this.storage({ coins: coins + 1 });
},
'Revert': function() {
const coins = this.storage('coins');
this.storage({ coins: coins - 1 });
},
},
},Going back past it runs Revert, and the coin is gone again. Apply can return false or a promise, like a plain function. Functions has the details.
Run a statement only sometimes
A Conditional statement runs one of its branches depending on what a function returns, for example a different line when the player has enough coins:
{
'Conditional': {
'Condition': function() {
return this.storage('coins') >= 3;
},
'True': 'y You can afford the scarf.',
'False': 'y You need a few more coins.',
},
},Choices and Branching shows how to branch the story this way, and the rules a Conditional follows.
When a statement can’t run
While you develop, the starter game loads the debug module (engine/debug/debug.js, listed in index.html), and js/main.js turns it on with monogatari.debug.level(5). With it, a statement that can’t run opens a dialog that says what went wrong and where: a jump to a label that doesn’t exist, a character or sprite that isn’t declared, a variable that isn’t in the storage. Without the debug module, or at level 0, nothing is shown.
Most of these mistakes also stop the game on that statement, so fix them and reload the page. Diagnosing Errors lists every dialog and what to do about it.
As the script grows
- Split it into files. A long story can live in several files, one per chapter, each with its own
monogatari.script({ … }). Each call adds its labels to the ones before; a label with a name that’s already taken replaces the earlier one. Split Files shows how to add the files toindex.html. - Translate it. Each language gets its own script: see Translating Your Game.
Related
- Writing Dialog: lines, the narrator, NVL pages and text effects.
- Choices and Branching: let the player pick where the story goes.
- Variables and Storage: remember what the player did.
- Jump, End, Next, Functions and Conditionals: the statements, in full.