Skip to content

Object Signals

Alex May edited this page Apr 30, 2020 · 1 revision

Objects can be connected together using signals. When an object emits a signal then any connected objects will receive that signal and have the connected verb code called.

Why is this a good thing?

Let's look at an example of an object called Clock that has a verb that we want to call at midnight:

Clock:midnight() verb code:

moo.location:tell( "The <object:name/> chimes midnight" )

We start a scheduled task to call this verb:

;moo.schedule{ hour = 0, minute = 0, task = "o( "..Clock.id.." ):midnight()" }

Great, that works, but what if we want a mouse to poke his nose out of his mousehole when the clock strikes midnight? How might we do that without adding another task?

Without using signals, we would have to do this:

Clock:midnight() verb code:

moo.location:tell( "The <object:name/> chimes midnight" )

-- call the mouse verb
moo.here.Mouse:peek_out()

But this means that the Clock verb now depends on the Mouse object being in the same location. What if the Mouse runs away? The Clock verb will cause an error.

We can solve this linked dependency by using signals:

Clock:midnight() verb code:

moo.location:tell( "The <object:name/> chimes midnight" )

-- emit a midnight signal
moo.emit( "midnight" )

We can now connect our Mouse, who has a verb called peek_out() defined on it:

;moo.connect( Clock, "midnight", Mouse, "peek_out" )

When the Clock emits "midnight" the Mouse will now "peek_out()"

Clone this wiki locally