external next up previous contents index
Next: Adding help Up: 8.4 MOO tutorialLevel Previous: The show verb

Cleaning up

To make this holder object more useful we need at least the 3 following functionalities: (1) A verb for removing elements, (2) a verb for clearing all messages and (3) a help message attached to the object, so that 'help holder' would work.

A simple ``clear'' verb is really simple as you can see below.

@verb holder:clear this

>@l holder:clear
#2108:"clear"   this none none
 1:  this.holding = {};
 2:  player:tell("You erased all information bits from ", this.name);

Of course this verb is highly dangerous. People might erase all their information by mistake. So what we should do actually is asking confirmation from the user, like in the example below. You can add that verb now. Note that we won't tell you any more ``edit the verb please and such'', you should know that by now from the description given below.:

>@l holder:clear
#2108:"clear"   this none none
 1:  answer = $command_utils:yes_or_no
     ("Do you really want to erase all information bits?");
 2:  if (answer)
 3:    this.holding = {};
 4:    player:tell("You erased all information bits from ", this.name);
 5:  else
 6:    player:tell("OK, information on", this.name,
       " was too important for erasing");
 7:  endif

The only new things here is are in the first 2 lines: $command_utils:yes_or_no is a Lambda Core utility function. Those functions are live savers, so please look at them as soon as your really start programming. Utility functions are verbs attached to various $objects. Type 'help utils' for a list of the different packages. Note that you NEED command utilities for interacting with users.

The :yes_or_no verb returns 1 if the persons types ``yes'' and 0 for anything else (including ``no''). The conditional (answer) is true if answer is set to 1, otherwise false and the else clause will be executed.

Finally we will show you an erase verb that does not ask confirmation. You should add this functionality as an exercice if you wish to have it.

@verb holder:erase any from this

>@l holder:erase
#2108:"erase"   any (out of/from inside/from) this
 1:  NUM = tonum(dobjstr);
 2:  if (NUM == 0)
 3:    player:tell("The correct syntax for the show command is:
       'erase <item> from <object>'");
 4:  elseif (NUM > length(this.holding))
 5:    player:tell("Sorry this list has less items then ", NUM);
 6:  else
 7:    player:tell("You erased element ", NUM, ": \"", this.holding[NUM],
       "\" from ", this.name);
 8:    this.holding = listdelete(this.holding, NUM);
 9:  endif

Additional exercice: If you wish you could also ask the user for confirmation before you erase an item.

Now, sometimes people don't want to type in commands like 'show all on holder' and then type 'erase 3 from holder', but rather have all elements displayed and then have the program ask which elements you want to erase. Even better we could write a verb which also would let you add elements. All this in ``menu style''. We will address this issue in an other tutorial.

next up previous contents index external
Next: Adding help Up: 8.4 MOO tutorialLevel Previous: The show verb

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