Yui should know who she’s talking to. On this page the game asks the player’s name, remembers it, and uses it in the dialog.

Where the name is kept

The game remembers things about the player in variables: named values that saves keep, and that going back in the story can undo. They live in js/storage.js. Open it: the starter game already has one for the name.

// Persistent Storage Variable
monogatari.storage({
  player: {
    name: '',
  },
});

player.name is the variable, and '' (two quotes with nothing between them) is its starting value: no name yet. You don’t need to change anything in this file.

Ask for the name

In js/script.js, add this right after Yui’s first line, "y Oh, you're still here too?",:

    {
      'Input': {
        'Text': 'What is your name?',
        'Validation': (input) => {
          return input.trim().length > 0;
        },
        'Save': (input) => {
          monogatari.storage({
            player: {
              name: input.trim(),
            },
          });
        },
        'Revert': () => {
          monogatari.storage({
            player: {
              name: '',
            },
          });
        },
        'Warning': 'Please write a name.',
      },
    },
    'show character y smiling',
    "y Nice to meet you, {{player.name}}. I'm Yui.",

Then use the name once more, in Mio’s last line of the 'ReturnIt' ending:

    'm Thank you, {{player.name}}. Do you want to see what I was drawing?',

Save, reload and play. After Yui’s first line, a box asks your name.

The palace and Yui, dimmed, behind a white box that asks "What is your name?". The text field holds "Sam", with an OK button under it
The game asks the player's name.

Type a name and press OK, or Enter. Yui smiles and greets you by it.

Yui, smiling broadly, in front of the palace. The text box says "Nice to meet you, Sam. I'm Yui."
Yui uses the name the player typed.

Press OK with the field empty, or with only spaces in it, and the box stays, with Please write a name. under the field.

The Input, piece by piece

Like the choice, the Input is an object. Its parts:

  • 'Text' is the question above the field.
  • 'Validation' decides whether the answer is acceptable. It’s a function: a small piece of JavaScript that the game runs with what the player typed, here called input. input.trim() is the answer without the spaces around it, and .length > 0 checks that something is left. The function gives back true to accept the answer and false to refuse it.
  • 'Warning' is what the box says when Validation refuses an answer.
  • 'Save' runs once the answer is accepted. monogatari.storage({ … }) puts it in the variable player.name, without the spaces around it.
  • 'Revert' runs when the player goes back past the question with the Back button: it empties the name again, and the box asks for it once more.

You don’t need to understand every symbol to use this. Copy it as it is, and change only the texts between quotes.

WARNING

Keep 'Revert'. Without it, the Back button stops working at the line after the question: the game can’t undo the answer, so it doesn’t go back at all.

Use the name in the dialog

{{player.name}} in a line is replaced by the value of the variable player.name when the line is shown: two curly brackets on each side, and the variable’s name between them. It works in any line of dialog or narration, as many times as you like.

The name stays with the game: a save keeps it, and loading the save brings it back. When a story ends and the player starts a new game from the main menu, the variable goes back to its starting value, '', until they type a name again.

Variables and storage explains variables further: numbers that go up and down, choices the game remembers, and more.

The whole script at this point
monogatari.script({
  'Start': [
    'show scene #2b2340',
    'play sound bell',
    'The bell rings at the end of the drawing class.',
    'show scene afternoon with fadeIn',
    'play music theme with loop',
    'Everyone hurries off, and soon the path to the Palacio de Bellas Artes is quiet.',
    'Someone left a sketchbook on the low wall by the path.',
    'show character y happy with fadeIn',
    "y Oh, you're still here too?",
    {
      'Input': {
        'Text': 'What is your name?',
        'Validation': (input) => {
          return input.trim().length > 0;
        },
        'Save': (input) => {
          monogatari.storage({
            player: {
              name: input.trim(),
            },
          });
        },
        'Revert': () => {
          monogatari.storage({
            player: {
              name: '',
            },
          });
        },
        'Warning': 'Please write a name.',
      },
    },
    'show character y smiling',
    "y Nice to meet you, {{player.name}}. I'm Yui.",
    'show character y worried',
    'y Hey, look. Someone forgot their sketchbook.',
    {
      'Choice': {
        'Dialog': 'y What should we do with it?',
        'Read': {
          'Text': 'Read the first page',
          'Do': 'jump ReadIt',
        },
        'Return': {
          'Text': 'Find its owner',
          'Do': 'jump ReturnIt',
        },
      },
    },
  ],

  'ReadIt': [
    'show character y smiling',
    'y Only the first page. Nobody will know.',
    'The first page is a drawing of the palace, with two people on the path in front of it.',
    'show character y happy at right',
    'show character m worried at left with fadeIn',
    'm That... is my sketchbook.',
    'show character y worried at right',
    'y Sorry, Mio. We should have asked first.',
    'show character m happy at left',
    "m It's fine. You two are in the drawing anyway.",
    'stop music with fade 3',
    'centered Ending 1 of 2: The Drawing',
    'end',
  ],

  'ReturnIt': [
    'y It says Mio on the cover. She always stays to draw the palace when the lamps come on.',
    'show scene night with fadeIn',
    'Night falls over the palace.',
    'show scene nightLight with fadeIn',
    'One by one, the lamps light up.',
    'show character y happy at right with fadeIn',
    'show character m happy at left with fadeIn',
    'y Mio! We found your sketchbook.',
    'show character m smiling at left',
    'm My sketchbook! I looked everywhere for it.',
    'show character m happy at left',
    'm Thank you, {{player.name}}. Do you want to see what I was drawing?',
    'stop music with fade 3',
    'centered Ending 2 of 2: The Lamps',
    'end',
  ],
});

Next

Saving and loading: what your players can do with their game.