About Message Processing System

Started by
1 comment, last by Skeleton_V@T 18 years, 7 months ago
Hi everybory! I'll try to describe it in short and sweet: As game programmers, you often encounter this situation: When two classes, say classA and classB exist in your projects. Both classes sometime need to invoke each other class's method(s) (classA -> m_rpclassB -> FunctB () and classB -> m_rpclassA -> FunctA ()) (m_rpclassB is a reference to a classB's instance and it is a member of classA). This seems to be simple at first, but when we compile this, we get caught by a compiler's error message: "class<whatever>'s method<whatever> is UNDEFINED". People have developed ways to get around this (and quite popular): One of this is to build a message processing system (MPS) that plays as a message receiver/sender between classes of the current project. Each time a class needs to know *something* about other classes, it sends a message to the MPS, MPS in role sends that message to the target object, then the target object process that message, send the result message to the source object through MPS (again). This MPS is useful for systems that needs to *post* a message from any object to all the other objects, but is often complicated for newbies and not capable of doing all kind of messages too. So I've though of a much simpler way to workaround this. Lets review: WHAT is your first problem (the problem made us implement all the message processing system things) ?. You only want *some* class to invoke *some other* classes' methos without worrying too much of this tedious compiler error message. Example: two classes: CBrick and CBall: The ball needs to know about brick's position at runtime for use of collision detection. So a member variable of ball as a reference to the brick is needed. But what if the brick needs to know WHAT kind of that ball colllided with itseft for use of reaction. So a member variable of brick as a reference to the ball is also needed. And bang, when you implement things like this, you got that annoying compiler message. ----Solution----: Build a base class that has a pure-virtual member function: say ProcessMessage (...), each class which need to receive message from another classes needs to inherit from this base class. When Ball needs to get Brick's position, it calls Brick -> ProcessMessage (<message infos>) and vice versa, no compiler error message anymore bacause the Process message is defined and other classes derive from the base class. This post is getting quit long, so I should'n continue, the topic is large too. But I hope you all got my idea. Sorry if this is not a short-and-seet post anymore :-P I'll appreciate any positive feedback: the advantage, disadvantage of this. Thank you in advance :D
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Advertisement
Quote:Original post by Skeleton_V@T
Example: two classes: CBrick and CBall:
The ball needs to know about brick's position at runtime for use of collision detection. So a member variable of ball as a reference to the brick is needed.
But what if the brick needs to know WHAT kind of that ball colllided with itseft for use of reaction. So a member variable of brick as a reference to the ball is also needed. And bang, when you implement things like this, you got that annoying compiler message.


I'm a little confused so I hope my post is on topic. Are you saying you use a messaging system whenever you have cyclic dependencies? Couldn't you just use a pointer or reference and forward declare the classes?

Using your Example:
// Brick.hclass Ball;class Brick{public:   bool collides(const Ball &b) const;};// Brick.cpp#include "Ball.h"bool Brick::collides(const Ball &b) const{   return b.intersects(*this);}

--Michael Fawcett
Thanks for the reply!. Yes, I meant I used to do that when have a cyclic dependencies. You can forward-declare a object before its declaration *but* try the situation I raised, you will know what I meant:

Forward-declare classB helps only if classA uses some methods of classB but whatif our system has 5, 10 or 50 different classes ??? (over 2!). How can you forward-declare them without confusing which should be forward-declare first :D.
Trust me, I tried, and tried hard, that leaded me to this solution :).

My previous post maybe a little short (though I want it to be shorter, but couldn't). I will give you a link to C++ FAQ Lite (probably you read it), they say something about this topic:

Here's the link: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--

This topic is closed to new replies.

Advertisement