Sunday, April 7, 2019

Implementing Help in a Story

Using tables

The next few posts I'd like to cover uses for tables, that I've picked up in the last week and a half. Now this won't cover every single thing that I've recently found a use for tables, but it hopefully will inspire folks on ways they can use tables.

Today's table is a help text table. So by default if you are in a story and type the command help you get a pretty useless That's not a verb I recognise. response. I'm going to make the command a little more useful by adding a simple help command.

The idea I have for a help command is that when you simply type help by itself, it will present a list of things you can get help about. Then by typing help something you get help about the something that you are asking help about. To do this kind of help we will need a table of help topics that we can provide help on. So let's begin with that.

A help-topic is a kind of value. Some help-topics are defined by the Table of Help Topics.

Table of Help Topics
help-topic          reply
move commands       "Use cardinal directions like north/south/northwest (also can be written as n/s/nw) and up and down."
take                "This will take an item that can be picked up. [italic type]Ex: 'Take pickle'"
pick up             "Same as take command. [italic type]Ex: 'Pick up pickle'"
drop                "Will remove item from your inventory and place it in the room. [italic type]Ex: 'Drop pickle'"
put down            "Same as drop command. [italic type]Ex: 'Put down pickle'"
look at             "This will look at a particular item you indicate. [italic type]Ex: 'Look at pickle'"
look inside         "Looks inside a container. [italic type]Ex: 'Look inside box'"
look                "This will just look at the room you are currently in, in general."
examine             "Same as look at command. [italic type]Ex: 'Examine the pickle'"
ask for             "This allows player to ask person for/about some item. [italic type]Ex: 'Ask Dave about the pickle'"
hit                 "Commits violence on person/item. [italic type]Ex: 'Hit Dave'"

First we define a kind of value known as help-topic. We also tie help-topic values to the Table of Help Topics. Next we define our Table of Help Topics, it has two columns. help-topic and reply.

We now can get a particular help-topic from the table and return it's particular reply that's associated with it. If you've ever been a Python developer then you might recognize this as a dictionary.

The next thing we need to do is actually create the verb for Inform to understand. We can do that with.

Understand "help [help-topic]" as asking for help about. Asking for help about is an action out of world, applying to one help-topic.
Understand "help" or "help [text]" as a mistake ("Help is available on the following topics: [help-topics list]").

Now there's two things here. The first Understand here is indicating that when the player types in help [help-topic] we are doing the action asking for help about. Inform is super literal and so creating an action called Asking for help about would require the player to literally type in exactly that, which isn't what we want the player to type instead we'd like for them to just type help [help-topic]. So internally the action is indeed called asking for help about but to the player the action is just help [help-topic].

Also notice the [help-topic] in that. That means it has to be the command help plus something in the help-topic column. If the player types help banana, well then that doesn't match anything we have in the table and thus help banana isn't the asking for help about command.

Okay moving to the next part, Asking for help about is an action out of world, applying to one help-topic. We define the action as out of world since we want asking for help to not consume time. Thus, when the player asks for help, Inform will not increment the turn counter, time will not progress, and so on. Basically, an out of world command is treated as if nothing actually happened, which is great for our help command and pretty much anything else where we need to break the forth wall. Finally we indicate that the action applies to only one help-topic.

Now, let's say the player just simple types help or types help banana. That's not asking for help about and the second line covers this. We tell Inform to understand help by itself or help with some other text that isn't in the help-topic as a mistake. A mistake is a way to indicate to Inform that the player has done something that isn't an action we've created and we're not going to attempt to process the command through the rule books. So mistakes don't get processed and thus mean they also don't take any time. It's like an out of world command, however, unlike an out of world command, we're simply just not even going to process the text.

Mistakes allow you the writer to provide a hint text to the player to maybe ask them to clarify what they were trying to do or poke them in some other direction that isn't a mistake. Sort of like if a player typed something like drink from teapot and you the writer might want to come back and say Did you mean drink from the tea cup?. In this case we tell the player Help is available on the following topics: [help-topics list]

However, we've now defined something new here say [help-topics list]. Inform doesn't know how to say [help-topics list] so we need to define that as well.

To say help-topics list:
    repeat through the Table of Help Topics:
        say "[line break] [help-topic entry]".

This defines how to say [help-topics list]. Basically we create a loop here with the repeat through the Table of Help Topics and for each entry in the table we say what's in the help-topic column.

Now finally, we're going to actually define the asking for help about command.

Carry out asking for help about:
    repeat through the Table of Help Topics:
        if the help-topic understood is the help-topic entry:
            say "[reply entry][paragraph break]";
            break.

I'm sure there is a better way to implement this, but when this is doing is setting up a loop and going through each entry in the table. When the help-topic the player typed in matches the help-topic entry we're currently on in the loop, we say the text that is in the reply column and then exit the loop with a break.

And there we go that's everything, so all of it in a one piece.

A help-topic is a kind of value. Some help-topics are defined by the Table of Help Topics.

Table of Help Topics
help-topic          reply
move commands       "Use cardinal directions like north/south/northwest (also can be written as n/s/nw) and up and down."
take                "This will take an item that can be picked up. [italic type]Ex: 'Take pickle'"
pick up             "Same as take command. [italic type]Ex: 'Pick up pickle'"
drop                "Will remove item from your inventory and place it in the room. [italic type]Ex: 'Drop pickle'"
put down            "Same as drop command. [italic type]Ex: 'Put down pickle'"
look at             "This will look at a particular item you indicate. [italic type]Ex: 'Look at pickle'"
look inside         "Looks inside a container. [italic type]Ex: 'Look inside box'"
look                "This will just look at the room you are currently in, in general."
examine             "Same as look at command. [italic type]Ex: 'Examine the pickle'"
ask for             "This allows player to ask person for/about some item. [italic type]Ex: 'Ask Dave about the pickle'"
hit                 "Commits violence on person/item. [italic type]Ex: 'Hit Dave'"

Understand "help [help-topic]" as asking for help about. Asking for help about is an action out of world, applying to one help-topic.
Understand "help" or "help [text]" as a mistake ("Help is available on the following topics: [help-topics list]").

To say help-topics list:
    repeat through the Table of Help Topics:
        say "[line break] [help-topic entry]".

Carry out asking for help about:
    repeat through the Table of Help Topics:
        if the help-topic understood is the help-topic entry:
            say "[reply entry][paragraph break]";
            break.

No comments:

Post a Comment

Implementing Help in a Story