From Unify Community Wiki
Revision as of 14:08, 7 March 2010 by Tortoise (Talk | contribs)

Jump to: navigation, search

Author: Ofer Reichman (tortoise)



BooMessenger is an efficient and simple to use messaging platform. It facilitates lightweight dispatching of messages (events) between script components without the need of coupling. It is written in Boo and can be used by any scripting language. Messages are properly defined so that errors are caught during compilation. Messages can also be grouped hierarchially using inheritance for easy subscription.


Creating and using the messenger

You don't need to create the messenger. It is created implicitly by the God class, which is bundled and implements the singleton pattern. To access the single messenger simply use:

<boo> God.Inst.Hermes</boo>

The singleton mechanics were decoupled to provide a single place for all single instances.

Tip: Feel free to use the God class as a container for any classes of yours of which you only need one instance.

Dispatching a message

To create and send a message simply instantiate a message class, provided with parameters, and voila! For example, if you have a message called MessageText you can write:

<boo> MessageText("Hello!")</boo>

The message will be sent right away.

Listening to messages

You can subscribe to receive a message or a group of messages using the Listen function. It receives the message type and the listener, which is the component calling (self):

<boo> def OnEnable():

       God.Inst.Hermes.Listen(MessageText, self)</boo>

Note: The listening script must be attached to a game object.

You can unsubscribe using the StopListening function. It receives the same parameters:

<boo> def OnDisable():

       God.Inst.Hermes.StopListening(MessageText, self)</boo>

Any received message will be handled by a function named OnMsgName, where Name is the name of the message class, without the Message prefix:

<boo> def OnMsgText(msg as MessageText):

       Debug.Log("Received a text message: ${msg.Text}")</boo>

Yes, naming is important. You should give all your message class names that start with Message.

Listening to a group of messages will be explained later in the Grouping messages section.

Defining messages

Messages are defined by inheriting from the Message class:

<boo>class MessageGamePaused (Message):


You can define a message with additional properties by adding properties to your message class. For example, you might want to attach a string:

<boo>class MessageText (Message):

           return _text
   _text as string
   def constructor (text):
       _text = text
       # send the message

Make sure that the base class constructor is called last, since it actually sends the message.

Tip: Create a folder for all of your message scripts to keep your workspace organized.

Grouping messages

You can group your messages hierarchially using multiple levels of inheritance. For example, you can have a group of messages that have something in common:

<boo>class MessageBall (Message):


Inherit from it to create individual messages:

<boo>class MessageBallKicked (MessageBall):


<boo>class MessageBallInGoal (MessageBall):


<boo>class MessageBallOutOfBounds (MessageBall):


Easily subscribe to receive all of them:

<boo>import UnityEngine

class Referee (MonoBehaviour):

   def OnEnable ():
       God.Inst.Hermes.Listen(MessageBall, self)
   def OnDisable ():
       God.Inst.Hermes.StopListening(MessageBall, self)
   def OnMsgBallKicked (msg as MessageBallKicked):
       Debug.Log("Ball was kicked.")
   def OnMsgBallInGoal (msg as MessageBallInGoal):
       Debug.Log("Ball is in goal.")
   def OnMsgBallOutOfBounds (msg as MessageBallOutOfBounds):
       Debug.Log("Ball is out of bounds.")</boo>

In a similar fashion you can also listen to all messages by subscribing to the Message base class.


BooMessenger is quite efficient when it comes to speed. While it is not as fast as delegates, it is slightly faster than using SendMessage. The reason for this is that it does not iterate through all scripts attached to a game object. It keeps a direct reference to the listening script.

Still, it should be used moderately. It is ill-advised to use it inside Update or FixedUpdate.

Integrating with JavaScript/C#

It is possible to use BooMessenger together with any JavaScript/C# scripts you may have. The topic of scripting languages interoperability is already covered by Unity in: Script compilation. Simply place all the Boo code inside the "Standard Assets" folder (for example under "Standard Assets/Scripts/Messenger") and leave the code that uses it outside.


<boo>import UnityEngine

class BooMessenger:

   _listeners = {}
   """ Enlisted listeners. """
   def Listen (msgType as System.Type, listener as MonoBehaviour):
   """ Starts listening to messages derived from the specified type. """
       # verify type inherits from Message
       unless msgType.IsSubclassOf(Message) or msgType == Message:
           raise "Listened type is not a Message"
       # get list (create if necessary)
       if msgType not in _listeners:
           _listeners[msgType] = []
       list as List = _listeners[msgType]
       # add listener
       if listener not in list:
   def StopListening (msgType as System.Type, listener as MonoBehaviour):
   """ Stops listening to messages derived from the specified type. """
       # get list
       list as List = _listeners[msgType]
       return unless list
       # remove listener
   def Send (msg as Message):
   """ Dispatches a message. """
       # send message (to listeners of base classes too)
       for msgType in msg.BaseClasses:
           # get list
           list = _listeners[msgType]
           continue unless list
           # send to all listeners
           for listener as MonoBehaviour in list:
               # invoke component method by name
               cb = listener.GetType().GetMethod(msg.FunctionName)
               if cb: cb.Invoke(listener, (msg,))

<boo>class Message:

   """ Method name in listener components """
           return _functionName
   _functionName as string
   """ Inheritance route """
           return _baseClasses
   _baseClasses as (System.Type)
   def constructor():
   """ Creates and dispatches a message. """
       # replace 'Message' with 'OnMsg'
       _functionName = 'OnMsg' + self.GetType().ToString()[7:]
       _baseClasses = array(System.Type, GetBaseClasses())
   protected def GetBaseClasses():
   """ Generates inheritance route """
       msgType = self.GetType()
       while msgType != Message:
           yield msgType
           msgType = msgType.BaseType
       yield Message</boo>

<boo>class God():

   static Inst as God:
   """ Calls upon God """
           God() unless _instance
           return _instance
   static _instance as God
   """ The messenger """
           return _hermes
   _hermes as Messenger
   private def constructor():
   """ Wakes up God """
       _instance = self
       _hermes = Messenger()</boo>
Personal tools