A choice shows the player a few buttons and sends the story where the picked one says. With a variable to remember the pick, the story can branch on it much later. This page builds both, then adds a countdown to a choice.

You’ll write labels and jump statements here: Script and Labels explains them.

Show a choice

A choice is an object statement. Each option has a key (its id), the Text on its button, and Do, the statement that runs when the player picks it:

monogatari.script({
  'Start': [
    'show scene afternoon',
    'show character y happy at center',
    {
      'Choice': {
        'Dialog': 'y Where should we go now?',
        'Palace': {
          'Text': 'Inside the palace',
          'Do': 'jump Palace',
        },
        'Home': {
          'Text': 'Straight home',
          'Do': 'jump Home',
        },
      },
    },
  ],

  'Palace': [
    'y To the palace, then!',
    'end',
  ],

  'Home': [
    'y See you tomorrow, then.',
    'end',
  ],
});
  • Dialog is a line shown in the text box together with the buttons, written like any other line. It’s optional.
  • Do is a string statement, usually a jump to the label that continues that path. To carry on with the statement after the choice, use 'Do': 'next'.
  • The option’s key (Palace, Home) never shows: players see its Text. Dialog, Class and Timer can’t be option keys, because they’re the choice’s own settings.

The game waits until the player picks an option. Each path then ends with end, or jumps back to a label the paths share.

Yui in front of the palace, three buttons in the middle of the screen, and her question in the text box
A choice with a Dialog line. The second option can't be picked yet.

Choices has every property of a choice.

Let paths meet again

Paths don’t have to stay apart. Send them back to a shared label once their part is over:

monogatari.script({
  'Start': [
    {
      'Choice': {
        'Dialog': 'y Tea or coffee?',
        'Tea': { 'Text': 'Tea', 'Do': 'jump Tea' },
        'Coffee': { 'Text': 'Coffee', 'Do': 'jump Coffee' },
      },
    },
  ],

  'Tea': [
    'y Good choice, it\'s cold out.',
    'jump AfterDrinks',
  ],

  'Coffee': [
    'y You\'ll be up all night!',
    'jump AfterDrinks',
  ],

  'AfterDrinks': [
    'We sat by the window and watched the rain.',
    'end',
  ],
});
flowchart LR Start(["Start: Tea or coffee?"]) -->|Tea| Tea["Tea"] Start -->|Coffee| Coffee["Coffee"] Tea --> After["AfterDrinks"] Coffee --> After After --> End(["end"])

Remember what the player chose

To use a choice later in the story, store it in a variable. Declare the variable in js/storage.js, so every new game starts with it:

monogatari.storage({
  player: {
    name: '',
  },
  drink: '',
});

Then set it when the player picks, with onChosen, a function that runs right before the option’s Do. Give each option an onRevert too, which undoes it if the player goes back past the choice:

{
  'Choice': {
    'Dialog': 'y Tea or coffee?',
    'Tea': {
      'Text': 'Tea',
      'Do': 'jump Tea',
      'onChosen': function() {
        this.storage({ drink: 'tea' });
      },
      'onRevert': function() {
        this.storage({ drink: '' });
      },
    },
    'Coffee': {
      'Text': 'Coffee',
      'Do': 'jump Coffee',
      'onChosen': function() {
        this.storage({ drink: 'coffee' });
      },
      'onRevert': function() {
        this.storage({ drink: '' });
      },
    },
  },
},

Inside these functions, this is the engine, so this.storage(…) changes your variables. Variables and Storage explains variables.

IMPORTANT

Monogatari doesn't undo what onChosen changed by itself. Without an onRevert, the player can’t go back past the choice once they’ve picked that option: Back leaves them on the line they’re on.

Branch the story on a variable

A Conditional statement runs one of its branches depending on what its Condition function returns:

{
  'Conditional': {
    'Condition': function() {
      return this.storage('drink') === 'tea';
    },
    'True': 'y Another cup of tea?',
    'False': 'y Another coffee? Really?',
  },
},

Returning true runs True, and returning false, or nothing, runs False. Each branch is a string statement: a line, a jump, a show statement. A branch that doesn’t wait for the player, like 'show scene nightLight', runs and the game goes on to the statement after the Conditional.

For more than two paths, return the name of the branch to run:

{
  'Conditional': {
    'Condition': function() {
      return this.storage('drink');
    },
    'tea': 'jump TeaEnding',
    'coffee': 'jump CoffeeEnding',
    'False': 'jump PlainEnding',
  },
},

A Conditional without a False branch goes on to the next statement when Condition returns false, as if its False were 'next'. The same goes for a missing True.

  • A name that matches no branch opens a dialog titled Conditional attempted to execute a non existent branch "water" that lists the branches there are, and then runs False (or goes on to the next statement when there is no False).
  • Branch names are case-sensitive: returning true runs True, while returning the text 'true' runs a branch named true.
  • A number runs the branch with that number as its name: returning 1 runs '1'.
  • A promise is waited for: the branch is chosen by what it resolves to, and a promise that fails runs False.

Conditionals has the details.

Show or disable an option

Two functions on an option decide whether the player can pick it:

  • Condition: the option shows only when it returns true.
  • Clickable: the option shows either way, but disabled unless it returns true.
{
  'Choice': {
    'Dialog': 'y Where should we go now?',
    'Palace': {
      'Text': 'Inside the palace',
      'Do': 'jump Palace',
    },
    'Gallery': {
      'Text': 'The upper gallery (you need a ticket)',
      'Do': 'jump Gallery',
      'Clickable': function() {
        return this.storage('hasTicket');
      },
    },
    'Secret': {
      'Text': 'The hidden staircase',
      'Do': 'jump Secret',
      'Condition': function() {
        return this.storage('foundMap');
      },
    },
  },
},

With hasTicket: false and foundMap: false declared in js/storage.js, “The upper gallery” can’t be picked until the player has the ticket, and “The hidden staircase” isn’t there at all until they find the map. A function that returns nothing counts as false.

When every option is hidden, the game passes over the choice and goes on with the statement after it; while you develop, a dialog titled Every option of this choice is hidden tells you. A disabled option shows dimmed, but make sure one option can always be picked: with every option disabled, the player can only go back.

Give a choice a time limit

A Timer puts a countdown bar at the top of the screen. When it runs out, the bar goes away and its callback runs. The buttons stay, so the callback usually picks one of them for the player:

monogatari.script({
  'Start': [
    'show scene afternoon',
    {
      'Choice': {
        'Dialog': 'y A paper plane is coming right at you!',
        'Timer': {
          'time': 5000,
          'callback': function() {
            this.element().find('[data-choice="Duck"]').get(0).click();
          },
        },
        'Duck': {
          'Text': 'Duck',
          'Do': 'jump Duck',
        },
        'Catch': {
          'Text': 'Try to catch it',
          'Do': 'jump Catch',
        },
      },
    },
  ],

  'Duck': ['You duck just in time.', 'end'],
  'Catch': ['You catch it. There is a note inside.', 'end'],
});

Two buttons, Duck and Try to catch it, over the palace in the afternoon, with a thin countdown bar at the top left of the screen
A timed choice: the bar at the top empties as time runs out.

  • time is in milliseconds: 5000 is five seconds.
  • callback runs when the time is up. Here it clicks the Duck button, written [data-choice="Duck"] with the option’s key, so the story goes on as if the player had picked it. A callback that picks nothing leaves the buttons on screen, and the game waits for the player.
  • Picking an option before the time is up cancels the countdown.
  • Going back to the choice shows it again with a full countdown.

Timer Display describes the countdown bar.

Choices on an NVL page

A choice shown while the text box is a full-screen page of NVL text appears inside the page, under the last line, instead of in the middle of the screen.