Triggering Events Using Notifications
<< previous | Chapter 7. Interactions Between Agents | next >>

## Triggering Events Using Notifications

Notifications allow instances to make announcements to an arbitrary set of "listeners". This allows instances to communicate somewhat indirectly— the announcing instance does not care or keep track of who is listening to the announcements.

An object may make an announcement at any time by using the method announce in the class Object. The announcement may reflect a state change in the object or a reaction to a specific event.

The announcement itself does not effect the other instances in the simulation unless they have explicitly decided to "observe" the announcer using the method observe. The observe method expects the object to be observed, the name of the announcement to look for, and the method to call when the announcement occurs. For example, an instance interested in monitoring the controller for a (fictional) "New Object" notification would call:

    self observe instance controller for-notification "New Object" with-method "notify-of-new-object".

The observing object would then get a call to the method notify-of-new-object whenever the controller made the announcement "New Object".

Once an observer instance begins to observe another instance, the observer will get a callback every time the desired notification is announced. The callback method must take 2 arguments: the source of the announcement and the announcement string. The keywords are unimportant, but a simple example is:

    + to notify-of-new-object from-instance announcer (object) with-message message (string):
            print "I got the message $message from $announcer!".

<< previous | Up | next >>
Finding the Closest Object | Table of Contents | Finding All Objects of a Given Type