Monday, September 6, 2010

Message Oriented Architecture (Intro)

After this article, I hope, you will make some nice architecture for your future games. Using a message oriented architecture, inspired by message oriented middleware (MOM), permit the complete separation of the modules. Without a static module managing the message, all your modules needed to be connected to allow any kind of communication. Without wire, we can't communicate. But this article will show you how to reinvent the WiFi in your architecture, i.e. wireless communication. Actually the wires still exist but are hidden behind the communication module.
Let's call our communication module "EventCenter". First reason is because I use it instead of the natives events of ActionScript 3. Secondly because all the principles are based on events, that encapsulate the messages in some sense.
Some notions are needed before starting. First notion is the synchronization: a synchronous message is blocking, i.e., when we call "send", the receiver sees his method "receive" called and only when this method finishes, the sender has the hand back; an asynchronous message is sent and after a given time, the receiver really receives the message, i.e. it's not blocking. Too theoretical ? Let's have two examples.
Synchronous Messages:
We can use synchronous messages when we want to simulate direct function call like "enemy.kill()" by sending the message "EnemyKillMessage". In this case, just after the send of the message, the enemy is dead.
Asynchronous Messages:
The example for asynchronous message is quite more complex. Imagine you have a loop, during a pass you could send 0,1, or n message(s). The only relevant information is "do I have received a message during the pass or not ?" Imagine the number of messages is irrelevant and we want to make a treatment only once, imagine a long compuation. If we had synchronous message, we should use a boolean to keep the information "alreadyComputed" and a message will be send after each pass to indicate we need to reset the boolean. We can also use asynchronous messages and store them into an array like a buffer. Cleaning the buffer in one time when making the computation can speedup the execution. (If you find a better example, not too complex, don't hesitate).

Next message will be about the structure of our communication center, here was just an introduction. (Will be published tomorrow)

No comments:

Post a Comment