Skip to content

Latest commit

 

History

History
181 lines (138 loc) · 6.41 KB

javascript.md

File metadata and controls

181 lines (138 loc) · 6.41 KB
title Functions
order 9
description Run your own JavaScript at a point of the story, and undo it when the player goes back.

Functions

A function in the script runs your own JavaScript when the story reaches it: change a variable, call something on the page, wait for something to finish. There are two forms. A plain function is the quickest to write. A Function object adds a Revert that undoes what the function did when the player goes back, which a plain function can't.

[TOC]

Syntax

// A plain function
function() {
  // your code
},

// A function the player can go back over
{
  'Function': {
    'Apply': function() {
      // your code
    },
    'Revert': function() {
      // undo what Apply did
    },
  },
},
Part Type What it does
plain function function Runs when the story reaches it.
Apply function Runs when the story reaches the statement.
Revert function Runs when the player goes back over the statement. Reverse is an older name for it that still works.

Inside these functions, this is the engine (the monogatari object), as long as you write them as function () { … } and not as arrow functions (() => { … }). So this.storage() reads and changes variables, and this.state() reads where the game is.

What the function returns decides what happens next:

It returns The game
nothing, or anything but false moves on to the next statement at once.
false stays on the function, showing what was on screen, until the player clicks.
a Promise waits for it, then moves on, unless it resolves to false.

Returning a statement such as 'jump Ending' doesn't run it: the engine never runs what a function returns. Use a Conditional to branch.

Run code between two lines

monogatari.characters({
  'y': { name: 'Yui', color: '#c28cf0' },
});

monogatari.script({
  'Start': [
    'y Let\'s see how much money we have.',
    function() {
      this.storage({ gold: this.storage('gold') + 10 });
    },
    'y We have {{gold}} coins now.',
    'end',
  ],
});

Declare the variable in js/storage.js:

monogatari.storage({
  gold: 0,
});

After the first line, the next click runs the function and shows We have 10 coins now. The player never sees the function itself.

Decide whether the game goes on

Return false to stop on the function until the player clicks, for example to let the player take in something your code changed:

function() {
  document.body.style.filter = 'grayscale(1)';
  return false;
},

The whole game turns grey, and the next click goes on. Going back doesn't undo it: a later function (or a Revert, below) has to set document.body.style.filter = ''.

Wait for something to finish

A function that returns a Promise holds the game until it resolves: the player can't click on, and auto-play waits.

function() {
  return new Promise((resolve) => {
    setTimeout(resolve, 2000);
  });
},

The game waits two seconds, then goes on to the next statement. For a pause with nothing else to do, wait 2000 is simpler.

Reuse a function

Define the function once in js/script.js, above monogatari.script(…), and use its name as a statement wherever you need it:

const addGold = function() {
  this.storage({ gold: this.storage('gold') + 10 });
};

monogatari.script({
  'Start': [
    'y Here is your allowance.',
    addGold,
    'y And here is your birthday present.',
    addGold,
    'y You have {{gold}} coins.',
    'end',
  ],
});

Undo with Revert

Going back doesn't undo what your code changed. A plain function can't be gone back over at all: the player stays where they are. A Function object can, and its Revert runs on the way back:

monogatari.script({
  'Start': [
    'y Here, take this.',
    {
      'Function': {
        'Apply': function() {
          this.storage({ gold: this.storage('gold') + 10 });
        },
        'Revert': function() {
          this.storage({ gold: this.storage('gold') - 10 });
        },
      },
    },
    'y You have {{gold}} coins now.',
    'end',
  ],
});

Going back from You have 10 coins now. runs Revert, which takes the coins away, and the player lands on Here, take this. again.

Revert decides whether going back continues past the function: if it returns false, the player stops on the function, with the line after it still on screen, and the next click goes forward again. Anything else continues to the line before.

How it advances

See the table under Syntax: the function's return value decides. While the function runs, clicks and the advance keys do nothing.

Going back

  • A plain function can't be gone back over. Going back from the line after it types that line again, and the player stays there. One that threw an error is stepped over, since it didn't finish.
  • A Function object runs its Revert and continues back to the line before, unless Revert returns false. Without a Revert, going back over it opens an error dialog, then continues.

What the debug module reports

Dialog title When What happens
An error occurred while trying to run a Function. A plain function threw an error or returned a rejected Promise. The dialog shows the error message. The game waits there, and the next click goes on with the statement after the function. Fix the function.
An error occurred while trying to revert a Reversible Function. (the text under it names Apply) Apply threw an error or returned a rejected Promise. The game moves on.
An error occurred while trying to revert a Reversible Function. (the text under it names Revert) Revert threw an error, returned a rejected Promise, or is missing. Going back continues.

Errors in Apply and in Revert get the same title, which says revert for both; the line under the title names the one that failed. The details of the error are in the browser's console (see When something goes wrong).

Related pages