Command Line Verbs

We've already talked about several command line verbs. For instance, ball:kick and ball:bounce. These are simple enough, but what do you do if you want to throw the ball at someone? How do you make a "throw ball at <player>" command?

Direct and Indirect Objects

As you might recall, command line verbs have three arguments: the direct object, the preposition, and the indirect object. In all of our examples so far, our direct object has been "this," and we've had no preposition or indirect object.

"player" and "this" are not the only built-in variables you get free with every verb. You also get "dobj" and "iobj" -- the objects that the player targeted when he/she typed the command.

For instance, if the command is "throw ball at Steve," then "iobj" will be an object variable equal to Steve's object number.

Similarly, you get "dobjstr" and "iobjstr." These are string variables set to whatever the player typed for the direct object and indirect object. In the previous example, "iobjstr" would be the string value "Steve".

So, program this verb:

@verb ball:throw this at any
@prog ball:throw
player:tell("You throw ", this.name, " at ", iobj.name, ".");
iobj:tell(player.name, " throws ", this.name, " at you.");
player.location:announce_all_but({player, iobj}, player.name, " throws ", this.name, " at ", iobj.name, ".");

As you can see, "iobj" works exactly like all the other object variables you've seen so far. Also take note of the :announce_all_but() verb. Its first argument, "{player, iobj}", is your first look at a MOOlist in action. This argument is a list of player objects who are NOT to hear the message.

Note: There is a limited number of valid prepositions. For a list, type "HELP PREPOSITIONS" in your MOO. Most of these have several equivalent values; you need use only one of them.

Object Matching

MOO automatically takes the "dobjstr" and "iobjstr" values and searches for them in object names and aliases, hopefully coming up with a "dobj" and "iobj" that are real objects. This is called object matching. By default, MOO expects to find exactly one matching object. If no matching object is found, "dobj" or "iobj" will be equal to the object value $failed_match (#-3). Or if more than one matching object is found, "dobj" or "iobj" will be equal to the object value $ambiguous_match (#-2).

So what if there is no player named Steve? With the above verb, this would produce a traceback: a report of an error in your code. This is rather ugly for other players to encounter, so it's good practice to add some code for error checking:

if (iobj == $failed_match)
  player:tell("I can't find any object matching what you typed.");
elseif (iobj == $ambiguous_match)
  player:tell("That matches more than one object. Please be more specific.");
else
  player:tell("You throw ", this.name, " at ", iobj.name, ".");
  iobj:tell(player.name, " throws ", this.name, " at you.");
  player.location:announce_all_but({player, iobj}, player.name, " throws ", this.name, " at ", iobj.name, ".");
endif

Also, MOO databases based on LambdaCore have a :my_match_object() verb on player objects. This takes a string argument, and does better matching than what the MOO does automatically. So as the final example in this section, let's use this new callable verb:

iobj = player:my_match_object(iobjstr);
if (iobj == $failed_match)
  player:tell("I can't find any object matching what you typed.");
elseif (iobj == $ambiguous_match)
  player:tell("That matches more than one object. Please be more specific.");
else
  player:tell("You throw ", this.name, " at ", iobj.name, ".");
  iobj:tell(player.name, " throws ", this.name, " at you.");
  player.location:announce_all_but({player, iobj}, player.name, " throws ", this.name, " at ", iobj.name, ".");
endif

Argumentless Commands

It's been mentioned before, but it's worth reiterating: you can have a verb with no command line arguments by giving it "none none none" as argument definitions.

It's important to realize that the MOO has a specific order when it looks for verbs: first it checks the player, then the room that the player is in, and only then does it check other nearby objects. Of course, in order for a verb to be found on an object, your command line must refer to that object. In other words, "kick ball" has the potential to find your ball object, but "kick" by itself will not.

In conclusion, then, if you wish to make an argumentless command, it can only be defined and used on one of two places: your own player object, or the room. Otherwise it cannot target an object.

Example:

@verb me:blink none none none
@prog me:blink
player:tell("You blink.");
player.location:announce(player.name, " blinks.");

Optional Arguments

Sometimes you want a verb to run no matter what arguments the player provides, or even if they provide none at all. Your arguments in this case are "any any any". This leaves the verb wide open for any sort of command that the player wishes to type. It's up to you, as the programmer, to decide what to do with the incoming arguments.

A useful variable for these sorts of verbs is "argstr". This is a string value set to whatever command the player typed (minus the verb itself). There is no attempt to match an object to argstr, but remember, you can do so yourself by passing argstr to :my_match_object().

Example:

@verb me:@spoof any any any
@prog me:@spoof
player.location:announce_all(argstr);

Verbs like the above example are quite common among MOOers; as you can hopefully see, it announces any message you wish to the entire room. Please use it sparingly, as it can quickly get annoying.

Untypable Commands

This seems of questionable worth at the moment, but eventually you will wish to define verbs that cannot be typed as a command. The arguments for this are "this none this". No, it's not supposed to make sense.

Table of Arguments and Their Uses

It's what you've been waiting for!

Argument DefinitionPurpose
none none none Argumentless command.
this/any none none Command with direct object.
none prep this/any Command with indirect object.
this/any prep this/anyCommand with direct and indirect objects.
any any any Command with any arguments at all, or none.
this none this Untypable command.

Again, type "HELP PREPOSITIONS" in your MOO to see a list of valid prepositions.