ENGL 379M: Cybertexts and Cybermedia

http://www.glue.umd.edu/~mgk/courses/fall2002/379/

 

Tossing (and Packing) MattK’s Snowball: An enCore Xpress MOO Verb Tutorial

 

Let’s assume you’ve created and described a generic thing called “snowball.” You want to be able to toss that snowball—what’s the fun otherwise? (Note that we’re steering clear of the verb throw since that one’s already predefined for all generic things in the MOO. Our verb toss will be much more interesting.) Anyway, tossing the snowball is satisfying for you maybe, but you’d also like other people to be part of the action by feeling it go whizzing by their head. And it occurs to you that the act of tossing the snowball isn’t very realistic if it still remains in your possession afterward. So, you want the act of tossing the snowball to remove the snowball from your possession and deposit it in the room. Finally, for extra-enhanced realism, you decide that before the snowball can be tossed again, it first needs to be packed. In the end, we need two verbs to do all this, one called toss and one called pack.

 

To program verbs in the MOO using the enCore Xpress editor, click the program icon button at the top of the screen. Then use the “My Objects” button to view a list of your objects and to select one for which to program a verb. Ignore the options for Flags that the editor presents to you. You’ll notice that you have the opportunity to program both verbs and properties. We’ll actually need to do both for our snowball, but we’ll start with the verbs. Click “New Verb” and dismiss the pop-up OK window. You now need to do two very simple but very important things. If you don’t do them, your verbs will never work, and you’ll get frustrated. So, best to get into the habit of doing them. What are they? Well, first you must change the name of your verb from the default newVerb to whatever you really want it to be called (in our case, toss). Second, you must change the arguments of the verb from the default “none/none/none” to “this/none/none”. To do this, pull down the menu list next to “Direct Object” and change its value from “none” to “this”.  That’s it. You’re now ready to start programming the verb itself.

 

All of your programming will be done in the big text window. When you’re done entering the text of your program, click “Compile Verb.” If you don’t click “Compile Verb” your verb won’t get defined and your efforts will be for naught. The little window at the bottom right will tell you if your verb compiled successfully or if there are errors. If there are errors, check your typing and try again. Compiling may sound scary, but don’t worry about it: I promise you won’t crash the MOO or start World War III.

 

You might want to try entering this simplified syntax for toss first, just to get the feel of things:

 

player:tell("Man! That snowball goes flying across the room!");

 

Compile the verb, then switch back to your player window and try it out by typing “toss snowball”. You should see this on your screen: “Man! That snowball goes flying across the room!”

 

But now let’s program toss for real. Here’s the complete syntax:

 

if (this.snow == 0)

 

player:tell("Alas, the snowball has splattered all over. Try packing the snowball again.");

 

else

 

player:tell("Man! That snowball goes flying across the room!");

 

player.location:announce(player.name + " tosses a snowball which goes whizzing right by your head!");

 

  this:moveto(player.location);

 

  this.snow = this.snow - 1;

 

endif

 

The verb is built around a simple if/else control structure, which also includes an end statement. We first check to see if the snow in the snowball has been packed (you’ll learn more about packing as you read). The current state of the snow (either packed or unpacked) is represented by what’s known as a property. In this case, our property is called “snow” and its default value is 1. At the beginning of the control structure, if the value of “snow” should happen to match “0” then the user is instructed to pack the snow and nothing further transpires. On the other hand, if the value of snow matches anything other than 0 (and in fact, the only other option is 1) then several things transpire:

 

 

 

 

 

Now let’s program pack. You’ll need to create another new verb—and don’t forget to rename it from the default and to change the arguments to “this/none/none”. Pack will be a simpler verb than toss, but just as important for the full effect of the snowball. Here’s the syntax:

 

if (this.snow == 1)

 

     player:tell ("It's already packed nicely.");

 

else

 

player:tell("You pack the snow into what is once again a very satisfying snowball.");

 

     this.snow = 1;

 

endif

 

Once again we see an if/else control structure used as the backbone of the verb. Technically we could have done without it, but alerting the user if the snow’s already been packed is a nice little touch that adds to the realism. The remainder of the verb simply sets the value of the property “snow” to 1 so that the next time someone tries to toss the snowball it can escape the first part of that verb’s control structure.

 

One other thing: you may have noticed that when we reset the value of “snow” to 1 above, we did so using a simple equal sign (this.snow = 1). But, at the beginning of the control structures in both verbs, when we’re checking to see if “snow” has been packed or not, we use a double equal sign (==). Why is that? Well, it’s because of something that’s a subtle difference to most humans, but a very significant difference to computers. When we use the single equal sign to perform the operation that sets the value of “snow” to 1, that value is being manipulated numerically. In our control structures, however, the value of snow is not being manipulated numerically—instead, it is being compared to see if matches a certain value. The difference between one value equaling another mathematically and two values compared and found matching is the difference that accounts for the single and double equal signs. You don’t really have to get your brain around this, or even worry about it, but I did want to offer the explanation.

 

Only one thing we haven’t done, which is to actually define this property we’ve been using called “snow”. To do that, select “New Property” instead of “New Verb” from the editor menu. Change the default name “newProperty” to a name of your own choosing (“snow” in this case). Then, in the text window, enter a value. We’re going to enter the numeral 1, which is what we’re using to represent the snowball in its packed state (remember that this value drops to 0 once the snowball is tossed). That’s it, just that single keystroke: 1. Click “Save Property” and you’re done with your snowball!

 

Thus, you can see that we can now have a pretty realistic snowball fight, with the snowball whizzing by people’s heads as they go scrambling to repack the snow to toss it again. Whee! Note that it doesn’t really matter what order you program toss, pack, and “snow” in, but until all three are done things won’t work as they should. If we had more time, we might have wanted to program the verbs to check to make sure the player is holding the snowball before being allowed to toss or pack it (as it stands now, once the snowball’s out in the room a player can toss or pack it without really picking it back up). You’ll notice, by the way, that when you go back and re-edit verbs the editor automatically adds line numbers and a closing comment statement that indicates when the verb was last revised and by who. Just ignore this stuff. You don’t even have to renumber the lines when you enter more code. The editor will do all that for you once you compile again, and it won’t get confused by your newly entered unnumbered lines.

 

For the ultimate in realism, we could eventually add a timer function that gradually melts the snowball as it gets tossed around. But that’s a lesson for another day . . .

 

More tutorials, tips, and wisdom are available here:

 

http://cmc.uib.no/moo/

 

Comments welcome:

 

Matthew G. Kirschenbaum

Department of English, University of Maryland

http://www.glue.umd.edu/~mgk/

mk235@umail.umd.edu