Welcome
Welcome to xnaquickstart

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, join our community today!

Add custom Message-Types

If you're a beginner, then post here

Moderators: Coordinators, Developers

Add custom Message-Types

Postby omme on Mon Oct 19, 2009 8:59 am

Hi,

I'm fairly new to QuickStart and I currently have problems wrapping my head around the Messaging- System in QSE.
What I do understand is the basic concept of sending messages out anywhere in the game and listening and reacting to it elsewhere.
But for the purposes of my project I would love to add my custom message-Types so that I can utilize the messaging system for my own specialized messages. How would I do that?

To boil it down to a more specific Question: Where in the Engine-Code are the Messages processed and where would I have to add my own chunks of code to make my custom messages work?
omme
Beginner
 
Posts: 1
Joined: Sat Sep 26, 2009 9:06 pm

Re: Add custom Message-Types

Postby lordikon on Thu Oct 22, 2009 4:42 am

Right now we haven't abstracted messages out into the sample project. Basically all the current messages are engine-specific.

For now you just modify the engine directly. This will be very quick and easy, but when you integrate with future versions of the engine you will have to make sure to merge your stuff in properly. There should be a message enum in MessageType.cs in the messaging folder. Just add a new enum value for each message you'd like to make.

The messages are processed by QSGame, in QSGame.cs. This is the lowest level of the engine, it sits right on top of XNA's 'Game' class.

Making a message 'work' is easy enough, you just create an instance of a message, with the template type of whatever you'd like (e.g. Vector3), and then make sure to set the MessageType to your enum value. Then wherever that message is received you'll know to look for the same template type. For example, in the code below you can see that the template type is 'GameTime', and message's type is MessageType.BeginPhysicsFrame. Now wherever I listen for BeginPhysicsFrame I know I'll be receiving GameTime as my messages 'Data'.

Code: Select all
// *** Here is the message being sent ***
Message<GameTime> msgBeginFrame = ObjectPool.Aquire<Message<GameTime>>();
msgBeginFrame.Type = MessageType.BeginPhysicsFrame;
msgBeginFrame.Data = gameTime;
this.game.SendInterfaceMessage(msgBeginFrame, InterfaceType.Physics);


Code: Select all
// *** Here is the message being received ***
case MessageType.BeginPhysicsFrame:
{
      Message<GameTime> msgBeginFrame = message as Message<GameTime>;
       if (msgBeginFrame == null)
        {
               throw new ArgumentException("Passed message was not a Message<GameTime> message");
        }

        // Begin next physics frame.
        this.physicsScene.BeginFrame(msgBeginFrame.Data); // <--- Data is of the type 'GameTime'
}
return true;
User avatar
lordikon
Administrator
 
Posts: 342
Joined: Thu Apr 03, 2008 11:26 pm
Location: Colorado


Return to Beginner's Area

Who is online

Users browsing this forum: No registered users and 0 guests

cron