external next up previous contents index
Next: The second verb ``dump'': Up: 8.4 MOO tutorialLevel Previous: Creating a property "holding"

The first verb ``add'':

 

Now let's create a verb that will allow you to add an information bit to this object. There are basically 2 uses for verbs. Either one uses them as user commands or as procedures called by other procedures (or user commands). User commands are a more tricky matter because you have to specify verb argument types that will allow the command parser to work correctly. Here we basically will create a verb called ``add'' that could be used in the following way (don't enter that command now, it won't work yet!)

   >add "The MOO is great" to holder

You may now have a glance at section 10.4 on page gif to see how command parsing works. Otherwise do that later and just type in the following without understanding details. It is important to type ``@verb ....'' only once for each given verb you want to program. Else you will wind up with several verb definitions and the MOO (and yourself) will get confused! In case you want to kill a verb (including its definition) type @rmverb <your object>:verb, e.g. @rmverb holder:add in this case. Now, proceed with entering the verb definition:

   >@verb holder:add any to this

   Verb added (0).
What we did here was to define a verb argument specification for a verb called ``add'' that will be attached to the ``holder'' object. The verb will be activated if it encounters a command like 'add ``the moo is great'' to holder'. The ``any'' keyword means that any kind of input could be entered. The ``to'' keyword means that the ``to'' preposition has to typed. The ``this'' keyword means that the indirect object should match the object to which the verb belongs (or as you will learn later some generic object from which an object inherits the verb).

Warning: if you forgot to enter the right verb specifications e.g. ``any to this'', do not add another verb with the @verb command but change the argument specification with the @args command, e.g. type: @args holder:add any to this.

Now let's program the verb itself. Note that we assume here that you know how to use the in-MOO text editor, or that you use a client that can ship editing to a local editor (see section 1.4). In the latter case, make sure to have local editing set. ('@edito +l will do that).

Now type '@edit holder:add'.

(1) If (and only if) you ship editing to a local client, you will see this in your editing buffer:

----------------------------------------------------------------------
@program #2108:add
.
----------------------------------------------------------------------
DON'T delete the first and the last line and insert all programming lines in between those! To ship the text back to the MOO in the rmoo client, type ctrl-c ctrl-s. In other MOO clients, the command may be different. Note that you can reuse the same buffer as many times as you like. In other words, once you are editing a verb you can make as many modifications as you like and ship them back to the MOO each time.

(2) Alternatively, if you use the in-MOO Verb editor (discouraged but sometimes you have to) you have to learn how to use it (type 'look' and 'help' and you will see something like this:

----------------------------------------------------------------------
Verb Editor

Do a 'look' to get the list of commands, or 'help' for assistance.

Now editing #111:test (any any any).
----------------------------------------------------------------------

Now back to programming. The ``add'' verb needs to do 2 basic things: (1) add the ``information bit'' to the ``holding'' property and (2) inform you that it did so. To do that we need to program the following lines:

  --------------------------------------------------------------------
  this.holding = {@this.holding, dobjstr};
  player:tell("Inserted ''", dobjstr, "'' to ", this.name);
  --------------------------------------------------------------------
If you are using a MOO client that does local editing (tkMOO lite or emacs for instance, you should produce something like this in your editing window (the object number will be different of course):
--------------------------------------------------------------------
@program #2108:add
  this.holding = {@this.holding, dobjstr};
  player:tell("Inserted ''", dobjstr, "'' to ", this.name);
.
--------------------------------------------------------------------

Those two lines already contain several features of the MOO programming language. If the MOO complains about bad syntax, like in the following example, you mistyped something:

   Line 2:  parse error
   1 error(s).
   Verb not programmed.

Be sure to type in those 2 lines as above. Note that statements in the MOO language need to separated by ``;'' so don't forget them! Errors are usually due to:

  1. Missing ``;''
  2. Misspelling of builtin functions or verbs
  3. Unmatched parenthesis or quotes

You can now try out your new verb. Use it as in the specification above (e.g. type 'add ``anything you like'' to holder' to the MOO). If you want to be sure that it worked, examine the object ``holder''. E.g. type:

   @d holder.
     - or -
   ;#<object>.holding

The ``;'' lets you evaluate simple expressions directly in the MOO without really programming a verb. Now let's look at the first line (reproduced below) in the ``add'' verb you started programming.

  this.holding = {@this.holding, dobjstr};

  1. It assigns a value to the slot ``holding'' on the object on which the verb runs (the object named ``holder'' in our case). Assignments are done with a ``='' assignment statement. So permanent assignment statements are done in the following way:

           <object>.<property> = <something...>
    
           Examples:
    
           Player.name = "Animal" ;
           this.holding = {"not very much", "a lot more''} ;
           #237.xxx = 123+7-a ;

    The value of the expression to the right of the ``='' sign is set as value of the property addressed to the left. To access the ``holding'' property on the ``holder'' object, we can use a construction like this.holding. ``this'' is a built-in variable that represents the object on which the verb runs. Those built-in variables allow you to write code that can be used by a whole class of objects.

  2. On the right hand side of the ``='' we find a list construction expression: {@this.holding, dobjstr} which does the following thing:

If you don't understand the concept of splicing consider the following cases. In the first case we use an @a and the list created is a flat list. In the second case we don't use the @ and we produce a list within a list, something we do not want here.

>;;a={"a","b","c"}; a={@a,"d"}; return(a)
=> {"a", "b", "c", "d"}

>;;a={"a","b","c"}; a={a,"d"}; return(a)
=> {{"a", "b", "c"}, "d"}

Note that we did not program a verb here but we used the evaluator functionality of the moo, which allows you to test simple things without writing a procedure. Please read section 16.6.1 on page gif if you want to know more about playing around with the evaluator. But now, let's examine line 2:

   player:tell("Inserted ''", dobjstr, "'' to ", this.name);

It runs the ``tell'' verb that (this time) is attached to the player, i.e. the person who typed the command. This line shows how to call other verbs from within a verb you are programming. The general syntax for verb calls is the following:

<verb>(<argument 1>, <argument 2>, <argument 3>, ...);
          e.g. 
player:tell("Inserted ", dobjstr, "  to ", this.name);
i.e. arguments are given in parenthesis, separated by commas. gif ``:tell'' accepts an arbitrary number of arguments, you can either use values or literals (e.g. strings in our case).

Be sure to put commas between EACH argument and watch out for quotes (") to be displayed to the player! Since " are used to delimit a string, you can't just have some " inside the string. Therefore, either use some other character to put quotes around words (e.g. '' (2 simple quotes) or better use escaped quotes: \". The following expressions are conceptually equivalent:

                "escaping" the quotes:

  player:tell("Inserted \"", dobjstr, "\" to ", this.name);

                or using any kind other character(s):

  player:tell("Inserted ''", dobjstr, "'' to ", this.name);
  player:tell("Inserted `", dobjstr, "' to ", this.name);
  player:tell("Inserted <<", dobjstr, ">> to ", this.name);

It is left as an exercise to the reader to figure out what each argument to the ``:tell'' verb will print. Play around with the MOO evaluator if you want, however the dobjstr variable will be empty, but something like this would work:

      ;player:tell("this is my ", player.name)


next up previous contents index external
Next: The second verb ``dump'': Up: 8.4 MOO tutorialLevel Previous: Creating a property "holding"

Daniel K. Schneider
Thu Apr 17 12:43:52 MET DST 1997