Now we want you to be able to show what information is stored in the object. So we want to be able to say something like: 'show 1 on holder' to display the first element or 'show all on holder' to display all elements to everybody in the same room.
Before we do that, let's program a simple ``dump'' verb that will dump the contents of the object to yourself only. If we want a verb that works like 'dump holder' we first must create a simple verb like this:
>@verb holder:dump this none none
Now you have two verb specifications as you can see with the @d command
(be sure to type the ``:
'' after ``holder''):
>@d holder:
holder (#2108) [ readable ]
Child of generic thing (#5).
Location island (#1429).
#2108:add MooBoy (#1324) r d any at/to this
#2108:dump MooBoy (#1324) r d this none none
Note, that if you don't know the @d
command yet
you can go and read
section 16.2.1 on page .
Below is the code for the ``dump'' verb. Enter it now and read the
explanation of what it does below:
----------------------------------------------------------------------
for item in (this.holding)
player:tell(item);
endfor;
----------------------------------------------------------------------
Here we use an iteration construct, i.e. the ``for looping statement''. (See also Iteration Section in the MOO Programming Manuel if you want to know more about iteration).
In the above case the ``for'' statement does the following thing:
(this.holding)
which must
return a list. Luckily we know that the ``holding'' property is a list.
The ``for statement'' is a more complex one then the ``assignment''
statement we have seen before. It can contain other statements.
You did use some very important programming concept here:
the iteration, i.e. doing something several times over.
In our case we applied the player:tell
verb several
times, i.e. once for each item in the in the ``holding'' property.
Now let's try out what we have done so far: Add a few items with the ``add'' verb like in the transcript below and then type ``dump holder'' to see what happens.
>add "item 1" to holder
Inserted ''item 1'' to holder
>add "item 2" to holder
Inserted ''item 2'' to holder
>dump holder
item 1
item 2
Now let's improve this program a bit. It would be nice to add numbers in front of each item to be displayed for various reasons. The following code will do that. An explanation of what it does is below and might read it first. To edit this verb again, just add the necessary lines in your editing buffer or edit the ``dump'' verb again if you use an in-MOO editor.
----------------------------------------------------------------------
n=1;
for item in (this.holding)
player:tell(n, ": ", item);
n=n+1;
endfor;
----------------------------------------------------------------------
Now try out again the ``dump verb'' you just programmed. If you think
that you don't have enough information in your holder, just add a few
items with the ``add'' verb, e.g. type: 'add ``new banana'' to holder'
.
In this variant of the ``dump'' verb, we used a classic programming construct: We added and displayed a counter. We also made use of a temporary variable ``n'' that only lives as long as the verb executes.
This is how counters work:
n = n+1;
n = 1;
statement
does this. Note that this statement takes place before the ``for'' statement.