Skip to content

Tutorial 2

Alex May edited this page Nov 4, 2017 · 3 revisions

If you're following on from part one, we have started to explore our world a little using programming commands, and now we want to add our first verb. Verbs are commands that users can type that execute the code that programmers have written. We're first going to add the ability to 'look' at our current location. Right now it does this:

look
I couldn't understand that.

So first we need to decide where our 'look' verb should live. Verbs are attached to an object, so we could add it to our player object and it would work great just for us but no one else, which isn't very useful!

We could attach it to the room that we're in, which would mean that any user in that room would be able to use it. That's great if we're only going to have one room in the world, which isn't going to be very interesting.

What we want to do is attach it to all rooms in the world, so no matter which room you are in, typing in 'look' will work.

Object Inheritance

Here we start on one of the most powerful features of a MOO!

We're going to rename our one and only room to become a generic room template that we can base all other rooms on. Firstly, we rename it:

;moo.notify( moo.here.name )
The First Room
;moo.here.name = "Generic Room"
;moo.notify( moo.here.name )
Generic Room

We don't actually want to be in the Generic Room - it's not supposed to be a real room after all, just a template. We need to make a new room!

;moo.create( moo.here )

And... we just made a new object! We can find out what ID is has by looking at the last created object for this connection:

;moo.notify( moo.last.id )
4

Importantly, we passed in moo.here as the first argument to moo.create(), which sets its parent object.

We want to rename this object and make it our current location:

;o( 4 ).name = "The First Room"
;moo.me.location = 4
;moo.notify( here.name )
The First Room
;moo.notify( here.parent.name )
Generic Room

Great! We've relocated our player object to the new room, and checked it's parent is correct. Now we can add that 'look' verb. Take note that we're adding it to the parent of our current location (Generic Room) and not the room itself:

;moo.here.parent:verbadd( "look" )

Oh, that was easy... Of course it doesn't do anything yet. We can check this by dumping the code:

;moo.here.parent:verb( "look" ):dump()

.

The period on its own line signifies the end of the verb. Let's add some code. There's a few ways of doing this, but the most exciting way is using the built in text editor.

;moo.here.parent:verb( "look" ):edit()

The editor will pop up. Enter the following code. Notice we don't start with a semicolon here!

moo.notify( "The look verb was called!" )

Press ^T (CTRL+T) to test the code, ^S to save it, and ^X to exit without saving. Save the code and the screen will clear.

look
The look verb was called!

It worked! At this point it's worth understanding a little more about where the MOO finds verbs. When a user types a command, the MOO will look for the verb in the following places:

  • On the user's object that typed the command
  • On the room the user is in
  • On the direct object, if any
  • On the indirect object, if any

We'll introduce the direct and indirect objects later, at this point you just need to be aware that as well as looking for a verb on a particular object (the current location, for instance), the MOO will also look at all the parents of the object as well. This is why our 'look' verb is found, even though it's defined on an object that we're not located in - it's the parent of the location that we are in.

This is object inheritance at work!

You should be aware that when the MOO finds a verb it stops looking, so in our current example it looked for a 'look' verb on the player object (and its parents) and didn't find any, so it moved on to the player's location, where it found the verb we just made on the the location's parent object. It stopped there and didn't try searching any further.

Looking Around

Currently our 'look' verb doesn't give us any feedback about where we are in the world. Let's change that by editing the code:

;moo.here.parent:verb( "look" ):edit()

Replace the current code with the following. You can press ^D to delete the current line in the editor.

moo.notify( moo.here.name )
moo.notify( moo.here.description )

Save the code (^S) and try looking again:

look
The First Room
<nil>

So we want to be able to add a description on our room although we haven't defined it yet, hence the '. We want to add a generic description so any room will return at least something:

;moo.here.parent:propadd( "description", "You don't see anything of interest" )
look
The First Room
You don't see anything of interest

OK, now let's add a better description to the room we're actually in:

;moo.here.description = "The golden walls of the entrance to this world are almost blinding in their magnificence."
look
The First Room
The golden walls of the entrance to this world are almost blinding in their magnificence.

We haven't actually overwritten the generic description - that is still safe and sound on the Generic Room - we're just overridden it by also defining it on our First Room. Like verbs, the MOO will search the object's hierarchy for properties and stop as soon as it finds a matching one.

Clone this wiki locally