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:
@verb holder:show any on thisNote 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 . This section will also teach you
how to change arguments in case you entered a wrong specification.
'@copy #2108:show to <your object>'
.
This will copy the verb from and object
on which it already exists to your object.
" ---- displaying a single element ----";
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.
<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);
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
(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.
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);