Allowing classes to access each others members without globals.

Started by
5 comments, last by johnnyBravo 18 years, 9 months ago
As far as I can see, a major advantage to adopting an object oriented approach is that classes can more or less take care of themselves. If I write a method function for a class called Draw_Object and then call it, the object will draw itself. This means that every instance of the class I make can also draw itself, so I don't have to specifically tell the game to do it every time. However, I'm confused about what to do if, in order to perform the calculations, the instance of the class requires infomation about the member variables of other classes (assuming the access to these variables is public, and that they are friends or of the same type as the original class). For example, if I was writing a collision detection function, how could I make an instance of a "CGameObject" class aware of the the positions and velocities of the other game objects without passing them as paramaters when I call the Detect_Collision member function. What's the best way to go about this? Make them global (seems kinda messy)? Pass the the function an array with all the other objects in it? Or the head of a linked list? Or something clever with pointers? Any help, or better solutions than the anaemic ones I was able to come up with, would be much appreciated.
Advertisement
The best way to do it is to have a game manager class of some sort that knows about all collisions, etc and stores all your game objects. Then when something significant happens that a particular game object needs to know about, the manager informs it, through a function call or some form of message.
edit: useless crap
Pass them as parameters to the function, pass them to the object's constructor and save a reference for further use or make them global somehow, go through some additional layer which keeps track of objects for you, etc..
You obviously can't get rid of all dependencies and which method to use is hard to determine with so little information to go on.

The major decision to make is whether you'll ever need to have *two* simulations running simultaneously (on server hosting multiple games for example). If one is enough then you can use some global mechanism (such as singletons), otherwise you'll generally have to pass around pointers.
In many cases, needing to make global variables or friend classes or such like that indicates a design that is in need of some improvement. In the case of a Detect_Collision(), I would highly recommend not making that a member function of an object. A single object doesn't know about other objects, so it shouldn't be its responsibility to detect collisions. The collision detection should be the responsibility of something that already knows about all the objects. Maybe you could have a Physics class that stores a collection of all the objects (or pointers/references to the objects that are stored elsewhere), and let this physics manager handle movement and other physics-related issues, whether it be through mass/acceleration/velocity calculations, collision calculations, etcetera.

[edit]More or less what Rebooted said, is what I'd recommend. Avoid making classes friends of each other as much as possible, especially in the early stages of a project when redesign is less painful.[/edit]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Ok, thanks for the help. I'll write a Physics "overseer" class to deal with it.

In that case, would a better use of member functions be to have the Physics class detect the collisions, then run member functions of the two objects that intersected, informing them of where the collision was, what velocity it occured at, what type of object (polygon/sphere/point, or terrain/bomb/player) was involved, and then let their internal logic sort that out?
I'm doing it by having the object classes call each other once to check themselves for collisions, and then each object can access every other object's privates as they are in the same class.

and for rendering I've got the object classes themselves sending their vertices to be drawn to the render class which at the end of the loop renders all the vertices that are in its list, and then clears the list.

Maybe you could also have a Level/World class which the object's send their vertices to for collision detection against the world.

I don't know if this is the best idea, but it's the best i've come up with so far.

This topic is closed to new replies.

Advertisement