User Tools

Site Tools


code:message_system

Message System

The message system is used to communicate between all the different systems of the games. Its main advantage is to avoid creating dependencies between systems. It also avoid having to pass and handle references through the systems. The systems don't need to know each other anymore.

For example, during a fight, the player chooses an attack then ask to the ui system to show the attack name. To do so, the fight system sends a message “SHOW_ATTACK_NAME” with parameters. It doesn't know who will receive it or how it will be executed. The fight system doesn't care anyway. On the other side, the ui system connects to the “SHOW_ATTACK_NAME” message to receive them and handle them. The ui system doesn't know who sent the message nor why.

Message Manager

The message manager is a singleton. It is the entry point for everything that wants to use messages. It lets you Connect/Detach/Send messages:

  • Connect : Let's you pass a method to execute everytime a certain message is sent.
  • Detach : Remove the method to execute when a message is sent.
  • Send : Send a message. It will execute right away all the methods that were connected to the message.

The callback method to pass must have the following signature : void Callback(IMessage _msg); The IMessage parameter can then be casted to another message object specific to the message received.

Messages

You have two ways to define a message:

  • If you don't need to pass argument with the message : then you just define a new enum value in MessageId.cs. You can then send the message using Send(MessageId _msg). The callback connected to a message send this way will hhave a IMessage parameter set to null.
  • If you need to pass arguments then you need to define a new class inheriting from IMessage. You then send the message using Send(IMessage _msg). The callback receiving the message will have a IMessage object that you will be able to cast into the new message class you created.

Internal Machinery

The message manager stores connection to messages using a map between MessageId and MessageSlot. A message slot contains an event and method to add/remove callbacks and execute the callbacks. IMessage classes contains a MessageId element. This is the id used when sending a message with the Send(IMessage _msg) method. It needs to be set in the constructor of objects.

code/message_system.txt · Last modified: 2015/05/01 20:18 (external edit)