Built-in Functions

You know already what verbs looks like - #2:my_verb_name() - built in functions look similar, but don't have an object associated with them. Thus an example of a built in function is: notify(#2, "Hello!")
Where is the code for notify() then? It's built in to the server itself. The server knows that notify() is used to print a string to a player object in the above example. There's a good long list of builtin functions in the Programmer's Manual, some of which we'll go through later. Enough at the moment to recognise them and know that they're just like verbs.

Calling Functions and Our First Command

The most common thing to do in a verb is to be lazy and get other code that's already been written to do stuff for you :) This is good practice even, despite how it sounds, as it means that code doesn't get duplicated unnecessarily, taking up memory and space in the game. Also, if the code you're calling gets made more efficient, then your verbs automatically become more efficient as well, for no extra effort on your part.
We've seen how to call functions several times before - like in the previous section, so I won't repeat it here.

When are we going to do all this neat stuff? Right now. Get your programmer shoes on, and lets write a simple first verb. I'll give what the typical MOO response to the commands will be in blue after what you should type, just to let you know that you're on the right track. If it's not exactly this, don't panic, the command is probably a different version but still does the same thing.

First create an object as a child of the generic thing called 'little cat' using @create. Eg:
@create $thing called little cat
-> You now have little cat with object number #NNNN and parent generic thing (#5).

You have a little cat thing now. We want to refer to it as 'cat', so add an alias to it with @addalias:
@addalias cat to little cat
->
Alias cat added to little cat(#NNNN).
Aliases for little cat (#NNNN) are now {"little cat", "cat"}

Now we want to be able to stroke the cat and have it purr, so we need to put a 'stroke' command on the object. First of all, think about how you would issue the command - 'stroke cat' in this case. Thus we want the direct object of the command to be the little cat thing, and not have a preposition or indirect object. So to add the command:
@verb cat:stroke this none none
-> Verb added (0).

To give some code to the command, we need to edit it in the verb editor:
@edit cat:stroke
You'll be taken to the verb editor, it should say at the end something like:
Now editing #NNNN:stroke (this none none).

We want the code to tell you that you stroked the cat, and also tell everyone else in the room that you stroked it first of all. The function for telling players things is called tell(). We want to tell the player who issued the command that they did indeed stroke the cat. So enter into the editor (remember it's just like the note editor with say for entering text) the line:
player:tell("You stroke ", this:title(), ".");
-> Line 1 added.

Okay, lets run through what that line does bit by bit.
player - this is the variable that contains the player who issued the command - you.
:tell( - this calls the 'tell' function on the 'player' object, which tells the player the text that is inside the ()s.
"You stroke", - this is a string that is going to get told to the player. The comma after it means that there is going to be more text to be told following. Strictly, that there are going to be more arguments for the tell function - more information for it.
this - the variable that contains the object that has the command on it - the little cat.
:title(), - this is another function that returns a string - the name of the object it is called on. In this case, it will return "the little cat" or "little cat" in some games. There's another comma, so there's more information for tell to come.
"." - Another string, with just a period in it to finish the sentence that is going to be told.
); - the closing ) for the 'tell()' function. Then the ; that we need to put at the end of lines.

Apart from the :title() function, you knew all that already. Type compile and if what if you've typed in is okay, it should say:
#NNNN:stroke successfully compiled.

Now we need to tell everyone else in the room that the player stroked the cat. To do this there's an existing function called 'announce'. But it's stored on the room, not the player. To do this we need to find out which room the player is currently in so we can call the function. This information is stored in the location property. So the next line is:
player.location:announce(player:titlec(), " strokes ", this:title(), ".");
-> Line 2 added.

Rather than go through everything, we'll just go over the interesting bits this time.
player.location:announce( - call the announce function on the object which is stored in the location property of the player who issued the command. This tells the message to everyone but the player who issued the command.
player:titlec(), - titlec is the Capitalised version of title. C for capitalised. Otherwise you'd get the player's name or short description with a lower case letter - not very good english!
The rest of the code does the same as the previous line. Compile it again and you should get the same message.

Now we want the cat to purr in response to being stroked. A happy little cat thing is a good little cat thing. We want everyone in the room to hear this message, so we use the 'announce_all' function on the room. Therefore the line is:
player.location:announce_all(this:titlec(), " purrs happily.");
-> Line 3 added. (I think you get the picture with the added lines already, so I'll leave them out from now)

There's nothing new to this beyond the announce_all function, which tells everyone including the player. Note that we used titlec() again for the cat this time so that the name is capitalised.

Compile the verb again and 'quit' out of the verb editor. You're ready to try your verb - 'stroke cat' :)


Last modified: Tue Sep 14 23:43:01 GMT 1999