Skip to content

Latest commit

 

History

History
309 lines (246 loc) · 11.8 KB

input.md

File metadata and controls

309 lines (246 loc) · 11.8 KB
title Input
order 17
description Ask the player for a value, such as their name, a number or a pick from a list, in a dialog, and store it.

Input

An Input opens a dialog over the game that asks the player for a value: their name, a number, a date, or a pick from a list. Your Validation function checks the value, your Save function stores it, and the game goes on. Until the player submits a value it accepts, the game waits.

[TOC]

The palace behind a dialog that asks "What is your name?", with a text field holding "Mio" and an OK button|An input asking for the player's name

Syntax

{
  'Input': {
    'Text': '<the question>',
    'Type': 'text',
    'Default': '<a starting value>',
    'Validation': function(input) { … },
    'Save': function(input) { … },
    'Revert': function() { … },
    'Warning': '<text shown when Validation fails>',
  },
},
Property Type Default What it does
Text text (required) The question. HTML and {{variables}} work in it; a variable's value shows as text.
Type text 'text' The kind of field: see Field types.
Default text or number none The value the field starts with. For a list, the value of the option that starts selected.
Options list none The choices for select, radio and checkbox: [{ value: 'tea', label: 'Tea' }, …].
Validation function accepts anything Gets the value; return true to accept it. Anything else shows the Warning and keeps the dialog open.
Save function does nothing Gets the accepted value. Store it here, usually with this.storage().
Revert function none Undoes what Save did when the player goes back. Without it, the player can't go back past the input.
Warning text none Shown under the field when Validation doesn't return true. {{variables}} work in it.
Attributes object none HTML attributes for the field, such as { 'maxlength': 12, 'placeholder': 'Your name' }.
Class text none CSS classes for the dialog (the text-input element), separated by spaces.
actionString text 'OK' The button's label: the text itself, or a translation key. See The button's label.
Timer object none A countdown: { time: <milliseconds>, callback: function }. See Add a countdown.

Inside the functions and the timer's callback, this is the engine (the monogatari object), as long as you write them as function () { … }.

The value is always text, even from a number field: '18', not 18. Convert it with parseInt(input) or Number(input) before you do maths with it. A checkbox field gives a list of the checked values instead.

Ask for the player's name

The starter game's script starts with an input like this one:

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

monogatari.script({
  'Start': [
    {
      'Input': {
        'Text': 'What is your name?',
        'Validation': (input) => {
          return input.trim().length > 0;
        },
        'Save': function(input) {
          this.storage({
            player: {
              name: input.trim(),
            },
          });
        },
        'Revert': function() {
          this.storage({
            player: {
              name: '',
            },
          });
        },
        'Warning': 'You must enter a name!',
        'Attributes': {
          'maxlength': 12,
          'placeholder': 'Your name',
        },
      },
    },
    'y Hi, {{player.name}}!',
    'end',
  ],
});

js/storage.js declares player.name:

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

The dialog asks for a name. Pressing OK (or Enter) with the field empty shows You must enter a name! under it; with a name, the dialog closes and Yui greets the player by it. The field takes at most 12 letters.

The same dialog with an empty field showing "Your name" in grey, and the words "You must enter a name!" under it|The Warning, shown when Validation doesn't return true

Field types

Type The field
text One line of text.
textarea Several lines of text.
password Text shown as dots.
number A number, with arrows to change it.
email, url Text that the browser checks is an email address or a web address before the dialog accepts it.
tel Text for a phone number.
color A color picker. The value is a color such as #5bcaff.
date, datetime-local, month, time, week The browser's date or time picker. The value is text such as 2026-05-01.
range A slider.
file A file picker. The value is the text the browser gives for it, such as C:\fakepath\photo.png; the file itself isn't loaded.
select A drop-down list of Options.
radio A list of Options where the player picks one.
checkbox A list of Options where the player picks any number.

Default works for every type except file: the browser doesn't let a page fill in a file field. Date and time fields take the browser's format: '2026-05-01' for date, '13:45' for time.

Pick from a list

{
  'Input': {
    'Text': 'What would you like to drink?',
    'Type': 'select',
    'Options': [
      { value: 'coffee', label: 'Coffee' },
      { value: 'tea', label: 'Tea' },
      { value: 'water', label: 'Water' },
    ],
    'Default': 'tea',
    'Save': function(input) {
      this.storage({ drink: input });
    },
  },
},
'y One {{drink}}, coming up.',

The list starts on Tea. The value saved is the option's value, so picking Coffee stores 'coffee'. With 'Type': 'radio', the same options show as round buttons; a radio input submitted with nothing picked gives ''.

Pick several

{
  'Input': {
    'Text': 'Which toppings?',
    'Type': 'checkbox',
    'Options': [
      { value: 'cheese', label: 'Cheese' },
      { value: 'olives', label: 'Olives' },
      { value: 'basil', label: 'Basil' },
    ],
    'Save': function(input) {
      this.storage({ toppings: input });
    },
  },
},
'y So that\'s {{toppings}}.',

The value is a list of the checked options' values, such as ['cheese', 'basil'], or an empty list when none is checked. In a line of dialog a list shows with commas: So that's cheese,basil. Default checks one option.

A number with limits

Attributes adds HTML attributes to the field, and the browser checks some of them itself before your Validation runs:

{
  'Input': {
    'Text': 'How old are you?',
    'Type': 'number',
    'Default': 18,
    'Attributes': {
      'min': 1,
      'max': 99,
      'required': true,
    },
    'Save': function(input) {
      this.storage({ age: parseInt(input) });
    },
  },
},
'y So you\'re {{age}}.',

Submitting 150 shows the browser's own message (Value must be less than or equal to 99 in Chrome) and keeps the dialog open.

Check the value

Validation gets the value and returns true to accept it. Anything else, including returning nothing, rejects it: the Warning shows and the dialog stays open.

'Validation': function(input) {
  return ['tea', 'coffee'].includes(input.toLowerCase());
},
'Warning': 'Please type tea or coffee.',

Validation can also return a Promise: the dialog waits for it, and only a Promise that resolves to false (or fails) rejects the value.

Warning

A Validation that doesn't return anything refuses the value, so a function that never returns true keeps the player on the input. While you develop, the dialog The input's Validation function returned "undefined" opens each time that happens. Make sure every path through it ends in return true or return false.

Store the value

Save runs once the value is accepted, then the dialog closes and the game goes on. What it returns doesn't matter, and if it fails, the game still goes on, so test that it stores what you expect. A Save that returns a Promise is waited for before the dialog closes.

The button's label

actionString is the button's label: write the text itself, like 'actionString': 'Send it', or the key of a translation. The default, OK, is a key every language the engine comes with has, so it shows in the player's language. For a label of your own in a game with several languages, add a key to the translations of each language in js/script.js:

monogatari.translation('English', {
  'Continue': 'Continue',
});

monogatari.translation('Español', {
  'Continue': 'Continuar',
});

then use 'actionString': 'Continue'. A key follows the player's language, also when they change it while the input is open; a key the player's language lacks shows in English. Before 2.9.0, only keys worked: text showed undefined. See Translating your game.

Add a countdown

Timer shows a bar along the top of the game screen that shrinks over time milliseconds. When it runs out, the engine calls callback and removes the bar. The callback decides what happens; it can fill in the field and press the button:

{
  'Input': {
    'Text': 'Quick, what\'s your name?',
    'Validation': function(input) {
      return input.trim().length > 0;
    },
    'Save': function(input) {
      this.storage({ player: { name: input.trim() } });
    },
    'Revert': function() {
      this.storage({ player: { name: '' } });
    },
    'Timer': {
      time: 5000,
      callback: function() {
        const input = this.element().find('text-input').get(0);

        if (input.content('field').value().trim() === '') {
          input.content('field').value('Nameless');
        }

        input.element().find('button').get(0).click();
      },
    },
  },
},
'y Hi, {{player.name}}!',

If the player doesn't type anything in five seconds, the name becomes Nameless and the game goes on. If they submit first, the timer stops.

Style one dialog

Class adds classes to the dialog, so you can style one input differently from the others in style/main.css:

'Class': 'name-dialog',
text-input.name-dialog .modal__content {
  border: 2px solid #5bcaff;
}

See the text input component for the element and its parts.

How it advances

The input holds the game: clicks outside the dialog and the advance keys do nothing. Once Validation accepts the value and Save has run, the game goes on to the next statement by itself.

Going back

Going back from the statement after the input runs its Revert and opens the dialog again, empty or with its Default. Going back while the dialog is open closes it and goes to the line before the input.

Without a Revert function, the player can't go back past the input: going back types the current line again.

What the debug module reports

Dialog title When What to do
The input's Validation function returned "<value>" Validation returned something other than true, false or a Promise, often nothing at all. The value was refused. End every path through the function with return true or return false.

An actionString that is a key in English but not in the player's language opens Translation for string "…" could not be found (see above), and a mistake in your functions shows in the browser's console.

Related pages