Why Message Queue
#1
Posted 27 November 2007 - 06:24 PM
I’m planning to use a component-based game object system. I’ve checkout some implementations (Yake, OGE) and threads. Everyone is so far also using a Message Queue System for events. I’m trying to figure out why. Why use a message queue instead of directly calling objects?
Cons with Message Queue:
• Slower than direct
• Must define message types and parameters
• More work/coding
Supposed Pros:
• Can spread work across frames by delaying event handling – however this can create interesting effects where objects lag behind. Besides queuing is slower.
• Can send a physics collision event to a non-physical object – however why even consider sending to an irrelevant object? The non-physical object shouldn’t even exist to the physics engine.
I’m looking for the advantages with a message queue. Any ideas?
#2
Posted 27 November 2007 - 08:07 PM
Your problems with message queues: Slower than direct? Sure, it adds another level of indirection. But, with the number of objects a tytpical system has and sitations and threads that it needs to deal with using a direct approach can become very messy, and probably even slower. Plus, I don't believe I've ever come across a situation where the speed of my message queue was significant in any way. Defining types, sure, but you're going to have to define something at the end of the day. More work/coding? not unless your other method is significantly less work/coding and provides the same amount of flexibility.
Quote
Advantages over what else?
- Me blog
#3
Posted 28 November 2007 - 04:07 AM
bladder said:
If your game is reasonably simple, then those problems may be small, but then why do you need queued messages for such a game?
#4
Posted 28 November 2007 - 08:26 AM
You have to create complicated and error prone mechanisms to deal with all kinds of situations that arise from "interrupt scheme" of handling events.
If I am drawing image, the LAST thing I want is event_resize, and actually going and changing backbuffer dimensions WHILE in middle of rendering! That is one example of where you want the shadow state, which you then read when you BEGIN a frame and apply changes, THEN render the image.
Well, sir, message queue does just that; you can handle the pending messages before processing a new frame. That kind of arrangement is simple and just works, maybe that's why it is very popular. When and if you need something different, one can always go out and do it. You said yourself a lot of engines and crap use message queues, maybe that is because the need to do something else doesn't arise too often?
Just a thought.
#5
Posted 28 November 2007 - 09:16 AM
Quote
- Me blog
#6
Posted 28 November 2007 - 06:06 PM
The advantage here is that you hardly need any net code if you properly set up your messages (i.e. also use "move" messages that an entity sends to itself and so on)
#7
Posted 28 November 2007 - 08:02 PM
Another advantage of a message queue is that you don't expect any specific interface from your objects. Let say that one day you want to add "set light brightness" message. If you would implement this with direct call, you would have to add virtual SetLightBrightness(float)=0; to your objects base class (or ensure the type and typecast to a sub-class with that interface). With message system you just add new message type called "SetLightBrightness" and process that message in the message handler of the object class (other types just ignore it). Of course it depends on the situation which solution is better, i.e. if you have some functionality that's required practically by all objects, then you should implement it as direct call.
#8
Posted 28 November 2007 - 10:11 PM
Quote
I'd say that generally is where the standard implementation would use multiple inheritance from interface classes, which also ensures that you don't pass messages to objects that do not support them.
Cheers,
- Wernaeh
#9
Posted 29 November 2007 - 05:58 PM
#10
Posted 29 November 2007 - 06:56 PM
#11
Posted 29 November 2007 - 07:27 PM
#12
Posted 29 November 2007 - 08:43 PM
J22 said:
No you don't. The virtual inheritance problem occurs when you create a diamond structure and the base class of the diamond has fields, which are then inherited twice unless you use virtual inheritance. But interfaces contain no fields, so the problem doesn't arise.
Quote
You can make pure abstract classes.
Quote
What are you referring to? I'm not familiar with the "empty base class optimization".
Quote
Sure, you do have to use some kind of RTTI to make things work, but that certainly doesn't mean that interfaces aren't useful at all. It allows you to design so that all entities that support a particular functionality expose it through the same interface.
#13
Posted 29 November 2007 - 09:45 PM
Quote
Actually, the point with interface classes is to avoid RTTI as much as possible.
Say you have an interface class CAiTarget for every object that can be used as a navigation target for AI calculations.
This class might look as this:
class CAiTarget
{
virtual CVector GetNavigateToPosition() = 0;
virtual AITargetType GetTargetType() = 0;
/* ... */
};
Now, several other classes can derive from this interface, and provide appropriate methods - i.e. health pickups, camping positions, other AI controlled objects, each providing a different type, and target position.
However, the target type (i.e. semantics in AI calculations) is seperated from the actual target class. Moreover, if you then, inside your bots, only keep a pointer to CAiTarget, rather than some ominous entity base class, you make sure that you only have a valid AiTarget to begin with (... this gives you compile-time type security, rather than RTTI...)
Cheers,
- Wernaeh
#14
Posted 29 November 2007 - 10:19 PM
#15
Posted 29 November 2007 - 10:33 PM
#16
Posted 29 November 2007 - 11:32 PM
However, because of type safety, I'd usually prefer the "exposed interface" over the "message sending" approach.
For example, sending a "Take Damage" message may be implemented by a "LivingObject" interface with a TakeDamage() call, and then, you wouldn't randomly send out this damage message to entities (in the hope that they might react to it), but be enforced to only call it on entities that support that interface.
Yet, the interface system is not easily run-time extensible, it is difficult (though not impossible) to add scripted additional interfaces to objects - though the same holds true for messages, just at a different place: If a message was not properly handled for at compile time within the message handler, you need some sort of scripted message handler, while if a interface didn't exist at compile time, you'd need a general scripted interface component that allows runtime binding of additional interfaces.
An advantage (maybe) of message systems is their relative simplicity: The sender needn't worry about the interface of the targeted object, just send on the message and hope that it does what it should.
Hope I didn't miss your point again ;)
Cheers,
- Wernaeh
#17
Posted 30 November 2007 - 07:03 AM
Wernaeh said:
Wernaeh said:
#18
Posted 30 November 2007 - 10:50 AM
Quote
No, I wouldn't. This is a workaround for the entire type safety you get with the interface system :) Basically, the point with interfaces is to see that you never ever need to downcast in the derivation tree. Don't keep a general list of "entities" unless you only are interested in things an entity class provides. Keep - per interface - a list (or more, you may encapsule this in a class) of objects implementing this interface. Obviously these lists then may properly be typed, and you don't need RTTI at all.
Quote
Okay, adding entity attributes on a per entity basis (possibly at runtime) is a valid point, this is more difficult to do with interfaces. I think I finally got what you meant ;)
Don't get me wrong - I'm using a message system for certain components as well, such as for my gui system (though I didn't go for attributes there) :) My point rather was to detail what other alternatives are there.
Cheers,
- Wernaeh
#19
Posted 01 December 2007 - 04:10 AM
Wernaeh said:
#20
Posted 02 December 2007 - 11:48 AM
Quote
Yup, that becomes a problem with my approach.
Cheers,
- Wernaeh
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











