TEST website instance!

The main loop – responding to player's command

Now that the next simple command has been obtained and successfully parsed, the game needs to respond to it. In our case the corresponding REPEAT section is pretty simple, but I'll deliberately split it into two: responding to the command and, if all else fails, reporting the failure.

repeat
   ifkey quit     # If the word QUIT occurs in the command
      say "As you wish."
      stop        # Exit the game
   fin
   call here      # Execute code, if any, associated with this location
   respond north, east, south, west, "There is no such exit here."
   call arg1      # Handle player's command
   call bail.out  # Command not handled - report an error

The IFKEY directive returns true if its argument (or all of its arguments, if there is more than one) occurs in the player's command being processed.

We have already met both the CALL directive and the automatic variable HERE. Since HERE is always a pointer to a location, CALL HERE amounts to executing code, if any, associated with the current location. Such associations are provided via the AT major directive, which will feature in the next page of this tutorial.

The RESPOND directive is used to respond to a list of words which may occur in the player's command by (a) "saying" its last argument and (b) terminating the current iteration of the main loop and starting the loop from the top again.

As in all cases where a text message is required, it can be specified explicitly (delimited by double quotes) or by its name (in case of named messages) or by a variable pointing to an entity associated with some text.

CALL ARG1 executes code, if any, associated with the first word in the player's command. Such associations are achieved via the ACTION major directive. We haven't done that yet.

Code executed via either of the two CALL directives can abort the current main loop of REPEAT sections, thereby signalling that the player's command has been adequately handled. If that does not happen, the lop will proceed to the next, error handling REPEAT section. In a real game this may get quite complex, which is why I have split off such general error handling into a REPEAT of its own. In our case, though, the generic "You can't do that!" is sufficient.

The QUIP directive is a simpler version of RESPOND. It displays the text nominated by its single argument and terminates the mail loop. Since this is the last REPEAT section, just SAYing the same text would have done just as well, but I prefer to make the termination explicit.

There is one other variant of loop termination: the QUIT directive simply aborts the main loop, returning execution to the first REPEAT segment. All three such directives (QUIT, QUIP and RESPOND) could have been rolled into QUIT, with zero, one or more arguments. However, keeping them as separate directives does help code's readability.

Previous page            Next page


Back to the tutorial index
To the Mipmip home page
Feel free to leave a comment!
Mike Arnautov (06 May 2025)