external next up previous contents index
Next: Cleaning up Up: 8.4 MOO tutorialLevel Previous: The second verb ``dump'':

The show verb

Now let's program a verb that allows you to display an item to the people in the room or everything. So we want a verb that does something like:

   show 3 on holder

This would display item 3 in the list if it existed. If it doesn't it would warn the player that such an item does not exist.

   show all on holder

should display all information in the holder. Here is the verb and you can grab the verb from here. Now let's examine the code. Note that to list a verb in the MOO you can use the @list command as below. Please note, that the line numbers in front of each lines are added by the '@list' command for your convenience. So don't ever type them when you program the verb yourself! It is useful to display line numbers when you quickly want to list a verb in order to see where it broke (grin).

>@l holder:show
#2108:"show"   any (on top of/on/onto/upon) this
 1:"This verb will print out either all objects or a selected item
   on the holding";
 2:"property to the room. Tests are made to insure that the direct
   object string";
 3:"represents a number AND an number not bigger than the length of the list";
 4:  n_items = length(this.holding);
 5:  " --- showing all the items ---- ";
 6:  if (dobjstr == "all")
 7:    player.location:announce_all(player.name, " shows all the items on ",
       this.name);
 8:    for k in [1..n_items]
 9:      player.location:announce_all(k, ": ", this.holding[k]);
10:    endfor
11:  else
12:    " ---- testing bad dobjstr's ----";
13:    NUM = tonum(dobjstr);
14:    if (NUM == 0)
15:      player:tell("The correct syntax for the show command is:
         'show all on <object>' or show <number> on <object>");
16:    elseif (NUM > n_items)
17:      player:tell("Sorry this list has less items then ", NUM);
18:    else
19:      " ---- displaying a single element ----";
20:      player.location:announce_all(player.name, " shows item ", NUM,
         " on ", this.name, ": ", this.holding[NUM]);
21:    endif
22:  endif
--
.

Now, please program this verb by entering the code or by copying it:

  1. First, create the verb argument specification like this. In case your are working at TECFAMOO skip this step and go directly to the next step, i.e. use the @copy command (see next step) without defining the verb first.
         @verb holder:show any on this
    Note that if you don't have an idea on verb argument specification (the ``show any on this'') you now should go an read section 10.4.2 on verb argument specification on page gif. This section will also teach you how to change arguments in case you entered a wrong specification.
  2. Then program the verb in the same way as you programmed your other 2 verbs, i.e. type: '@edit holder:show'. Now instead of manually typing the code, you can grab it over the Net (see above) and paste it into the editing buffer (or whatever technique for uploading code you prefer). At TECFAMOO you can simply '@copy #2108:show to <your object>'. This will copy the verb from and object on which it already exists to your object.

Then, play around with the verb trying out various variations to convince yourself that it works. This verb is not perfect and we suggest that make some modifications later. The main goal of this part of the tutorial is to teach you how a basic algorithm works and how to understand somewhat longer code. In case you don't understand in detail how this code works, there are 3 solutions:
  1. You try to figure out by ``hand'' what the code does, i.e. you try to imagine step by step what will happen when this procedure runs.
  2. You insert a lot of ``print'' statements in the code that will print out variable values. To do that, you can insert either something like:
  3. The best solution is to run TECFA's (Boris Borcic's) Code animator in the Xemacs client. It will in the editor buffer replace variables by values in the code in step by step fashion, so you can really see what the code does.

Now let's examine new features of the MOO language we use in this verb.

  1. You can insert comments in your code that will be remembered by the server. Simply write a statement enclosed by quotes, e.g.
        " ---- displaying a single element ----";
  2. In this verb we use another variant of the ``for'' loop statement. In the ``dump'' verb we used a ``do list'' construction, whereas here we use a ``do times'' construction, i.e. we don't tell the loop to run once over each item of a list, but we tell it to run N times (from number n1 to number n2)
       for <variable> in [<beginning>..<end>]
    Here is an example:
       for k in [2..10]
    will do 2 things. (1) It will loop <end> - \verb<beginning> times , e.g 10-2 = 8 times in the above example. (2) It will assign the ``index'' of the current loop to the counter <variable>. I.e. the loop will assign k=2 in the first loop and finish with k=10. Note that in the ``show'' verb we use the iteration index ``k'' to access the nth element in the list hold by the ``this.holding'' property.
  3. Elements of lists can be accessed with the following syntax (see the section on Indexing into Lists and Strings in the MOO Programmers Manuel for more details):

       <list>[<element number>]
    
    Examples:
       {0,1,2,3,4}[2]   ==> will return 1
       this.holding[3]  ==> will return the 3rd element

    Note that accessing lists out of range (e.g. element 5 in a list of 4 elements) will produce an error ! The built-in ``length()'' will tell you the length of the list and you can test list access like we did in the ``:show'' verb:

     4:  n_items = length(this.holding);
     ......
    16:    elseif (NUM > n_items)
    17:      player:tell("Sorry this list has less items then ", NUM);
  4. The most important new item of the MOO programming are conditional statements (IFs). See the section on Statements for Testing Conditions in the MOO programmers Manual. Here we just reproduce an abstract pseudo code definition of what happens in the ``show verb''.

     6:  if (``the direct object string is EQUAL to "all")
         ``do print out all statements'';
    11:  else
    14:    if (``the direct object string is not a number, e.g. 0!'')
           ``then complain to the user'';
    16:    elseif (NUM > n_items)
           ``else if we were able to create a number BUT the number is
            too big complain too'';
    18:    else
           ``print a single element;
    21:    endif
    22:  endif
    So IFs can be nested and alternative conditions ``else'' or ``else if'' exist. Note that the (conditional) and the keywords ``else'' and ``elseif'' belong to the same statement, so no ``;'' is needed for them! Here is the same same structure as in the ``show verb'' even more simplified.
       if (condition1)
         ``do something'';
         ``do something more complex'';
       else
         if (condition2)
            ``do something else'';
         elseif (condition*)
            ``do something else'';
         else
            ``do something else again''
         endif
       endif
  5. Conditional expressions are used for example in IF statements and they return a truth value, i.e. the expression on line 6:
          (dobjstr == "all")
    is either 1 (= true) or 0 (=false). Please remember that equality tests are done with the ``=='' operator and NOT with a simple ``='' which is an assignment.
  6. The tonum() built-in function in line 13 will translate a string to a number. If conversion is not possible, the number is 0. Try it out by typing ';tonum(``bla'') to the MOO client. The length() function in line 4 returns the length of a list. As we explained in section 8.2.4 functions always return a value. You will learn later how to build your own function verbs. All built-in functions are explained in the MOO manual or in the MOO (type 'help functions').
  7. An other built-in function allows displaying messages to the people in the same room. Each room (normally) has a ``announce_all'' verb that works in a similar way as player:tell. The latter only displays something to the player who typed the command. Each player has a location property containing the room object the player is currently in. So all we need to run this verb is to use the following kind of statement:
     9:      player.location:announce_all(k, ": ", this.holding[k]);

That's it ! Again: try to study this verb until you are sure how it works. As we said you can insert statements that print out intermediary variables or whatever you want to you. I.e. if you want to be sure that the counter works right in the for loop, insert this into the loop after line 8 or 9:

player:tell("K= ", k);


next up previous contents index external
Next: Cleaning up Up: 8.4 MOO tutorialLevel Previous: The second verb ``dump'':

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