Warning: Use of undefined constant image_setup - assumed 'image_setup' (this will throw an Error in a future version of PHP) in /home/westleyw/public_html/wp-content/themes/autofocus/functions.php on line 576

Warning: Use of undefined constant image_setup - assumed 'image_setup' (this will throw an Error in a future version of PHP) in /home/westleyw/public_html/wp-content/themes/autofocus/functions.php on line 577
Westley Wood » Message System
© 2012 Westley Wood

Message System

A Message System is the implementation of a base communication system across a variety of classes.  This base system takes place in a public method that every receiver must have.  For the purpose of this exercise, we will be calling this method “Message Handler”.  Everyone who has access to the message dispatcher has access to the “Message Handler” for every Entity that is capable of receiving a message.

Message

A Message is an enumeration type variable, that’s meaning is bounded by the interpretation of its sender and receiver.  Since the Message has a corresponding integer value, it would be more efficient to just send the Message as its integer value then to pass the variable type.

 

 

Mail

The Mail class is one of the simplest and most important parts of one’s Messaging System.  The Mail is simply the standard class for which a message and its details are stored.  The reason that this information is so important in the Message System is, because no matter who sends or receives the message; the message will always be delivered.

 

 

 

 

 
With all of this data stored in small bit types, it then makes it easier to share large varieties of information across multiple platforms, without the need to define the meaning in every class that the message passes through to get to its destination.

 

 

 

If the program needs to send messages to multiple classes, then one could create a “MessageType” enumeration to represent which class in the receiving entity should handle the message.

 

Message Dispatcher

MessageDispatcher is a Singleton class that manages the message-traffic between the entities.  MessageDispatcher has access to this list of entities through the Entity Manager, but the header file should only be included in its (.cpp) file.  Since everyone sends a message by accessing this object, it can cause a “circular inclusion” linker error if linked in the header file.

For the example code in this tutorial, “EMI” will represent the singleton of Entity Manager Instance.  Unless the message is delayed, it will be discharged immediately.  Discharge looks at the Mail’s receiver and grabs the corresponding entity from EMI and then points to run its message handler.

 

 

 

The “DispachDelayedMessage” method, acts as the update function for the MessageDispatcher.  So this function should be called at the beginning of the update function in the EMI.  The Mail is arranged in the “PriorityQ” based on its dispatch time.  By using a Set container with the overloaded operators “==” and “<” for the PriorityQ, the Mail can then be represented and compared by its dispatch time.  (The source code for overloading these operators is provided in a link at the end of this tutorial.)

 

 

 

 

 

 

 

Message Handler

The MessageHandler of an entity is the method that will react/respond to the message.  Every entity that wants to receive a message must have this method.  The containing code in the MessageHandler will process in response to the dispatched message.

The chart below shows a general idea of the layout of the access from the “MessageDispatcher” to the MessageHandler of the receiving entity.

This Message System is only a basic idea of the many different types of Message System’s out there.  Message Systems are used in everything from Finite State Machines, to scripting, to Transmit Protocol Handler over networks, etc.  So getting a good understanding of how a Message System works is the literal key to many complex design patterns.

 

Reference:

– Buckland Mat, “Programming Game AI by Example” (Google books)

– Westley Wood

– “Object Manager”– This tutorial can help with creating an Entity Manager.

Source code for this example.

Post a Comment

You must be logged in to post a comment.