Glulx Input Loops

version 1/101121 by Erik Temple

  • Home page
  • Beginning
  • Previous
  • Next

  • Section: The character input loop

    Glulx Input Loops defines the "character input" input-loop. This is intended to serve the purpose that Inform I6 library's VM_KeyChar routine handles--to get char input and return the key pressed. To require a keypress before continuing we can simply invoke the loop:

        process character input loop

    If you want to know which key the player pressed, you can check these global variables, which are available once the input loop has completed:

        keystroke: an indexed text containing the letter pressed.
        keystroke-code: the numeric code (think ASCII) of the key pressed.

    Note that the keystroke variable will only be filled if the key pressed is printable, defined as belonging to the printable ASCII or Latin Extended A character sets. If the player presses return, escape, an arrow key, or some other unprintable character, or if the player does some other input instead, such as resizing the window, then keystroke will be an empty string ("").

    So, if we want to echo the player's input back immediately, something like this provides the basic outline:

        process the character input loop;
        say "You typed: [keystroke]."

    Because by default *any* glk event, not just a keypress, will allow the input loop to complete, it will be convenient to use one or both of the keystroke variables to set up a control structure for our input processing. For example, here is how we might set up the situation where we want to wait for any key before continuing:

        now keystroke-code is the null char;
        while keystroke-code is the null char:
            process the character input loop.

    The "null char" is a value that can be tested against the keystroke-code variable to indicate whether a key has been pressed. If the keystroke-code variable is equal to the null char after processing a char-event input loop, it is certain that the player entered some input other than a keypress (such as resizing the window). So, this code uses a "while" loop to check whether a key has been pressed; as long as keystroke-code is the null char, no key has been pressed.

    It is common enough to want to wait for a key that this code block can be invoked with a single phrase:

        wait for any key

    (This replaces the "wait for any key" behavior as defined by Basic Screen Effects.)

    If we want to know what key the player pressed, we can simply check the keystroke and/or keystroke-code variable:

        wait for any key;
        if keystroke is "r" or keystroke is "R":
            try restoring the game.

    The Basic Screen Effects extension offers a phrase to allow the game to wait for the space or return key. Glulx Input Loops does the same; use

        wait for the space key

    This replaces the Basic Screen Effects implementation, and will pause the game until either the space or return key is pressed.

    By default, any user-defined input loop with the focal event type "char-event" will act in the same way as does the pre-defined character input loop. This behavior is defined by three rules in the input loop event-handling rulebook (see below). These three rules are:

        "char event null assignment rule" - assigns the null char for any event other than a keypress;
        "basic char event handling rule" - assigns the keypress to the keystroke and keystroke-code variables;
        "complete char event handling rule" - runs at the end of the rulebook and stops the input loop, provided that a key was pressed.