Your browser (Internet Explorer 6) is out of date. It has known security flaws and may not display all features of this and other websites. Learn how to update your browser.
X

Solkraft Tutorial 5: Messages

This tutorial will contain a quick overview of the message system and also cover how to create your own new messages.

Messages are a form of communication between different parts of the engine. They can be defined to contain any type of data. When you send a message you send it to an entity. The receiving entity will then take this message and ask all of its attached properties if they want to accept it. Every property on the entity that is set to receive that type of message will receive that message and process it however the property has defined to process it.

Let’s look at how you create entirely new messages. First we go to the \blueprints\ directory from earlier tutorials and create a new file with an appropriate name like ProjectNameMessageProtocol.xml. This is the file where we want to define our new messages in. Messages needs only a name to be able to be sent and received, but you can attach any type of data to it as well if desired. Let’s look at how it could look in this message protocol file:

<Message name=”SimpleMessage”>
</Message>

<Message name=”AdvancedMessage”>
<Dependecy path=”string.h”/>
<var name=”VariableName1″ type=”string” value=””/>
<var name=”VariableName2″ type=”string” value=””/>
<var name=”VariableName3″ type=”float” value=”0.0″/>
</Message>

The “var”s are the data that the message will contain, and the “Dependency path” is used if a variable requires a specific #include to work. Now, once this file is done we have to compile it into c++ code, which a separate program will do for us. Navigate to theĀ SolKraftEngine\bin\ExternalCompiler\ directory and run ExternalCompiler.exe. Now all we have to do is include the newly generated file where we need to use the new messages:

#include “Tools\ProjectNameMessageProtocol.h”

That’s it for creating new messages, now for recieving messages.

Every property has an inherited function called setupAcceptedMessages(), in this function you are supposed to add strings to the std::vector called m_AcceptedMessages. These strings are the names of the messages the property wants to accept. When a message comes in, the property checks the name of the incoming message and compares it to all names stored in its m_AcceptedMessages std::vector. If there is a match the message will continue on to the property’s handleMessage() function where it can be handled. To access the variables in the messages there are generated functions which can be run on the message. If we for instance attached a variable to the message called “ExampleVar”, we access it by running the functions setExampleVar() and getExampleVar().

To send a message we first create a new message object:

AdvancedMessage* t_Message = new AdvancedMessage();

Then we set whatever values we want to set:

t_Message->setVariableName2(“Example String”);
t_Message->setVariableName3(4.7);

And then we send it to an entity like this:

t_ExampleEntityPointer->distributeMessages((MessageBase*)t_Message);

Note that you have to cast the message pointer to it’s base class MessageBase before sending.

Leave a comment  

name*

email*

website

Submit comment