Jump to content
  • Advertisement
Sign in to follow this  
leiavoia

Objects that have to know about other objects (design issue)

This topic is 4971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is actually a pretty common design issue, but i'm interested in how other people solve it. Basically, my game thus far has had some form of central, mono-state, "object processor". You put an object in the processor and the processor calls Update() and Draw() on the objects it manages. So any objects that needs processing needs to get dumped into the processor at some point in order to "work". The new problem is that i am finding that a single centralized processor will no longer work and i may need several independent processors. Now objects will have to know about particular processors to be be able to create and add objects into them. Most objects don't need to know about this, only the ones that create other objects. For instance, a gun creates a bullet. The bullet creates an explosion on contact. The gun needs to know where the procesor is in order to submit the bullets, the bullets need to submit the explosion effects. How would you solve this problem? There are a few ways to solve this that i can think of right off the bat, but i want your input: 1) The most OOP way of doing it is to give any object-creating object a reference to a processor so it can put any newly created objects into it. This has some drawbacks: It's extra memory overhead to store that reference (but not much). That's not a big deal, but eventually i want most of my game objects to be factory-made so they need a somewhat common interface. Not all of my objects create other objects, so they don't all need a ref to the processsor. 2) Make the *current* procesor global. Just have everybody use that. This can have problems too: what if it's NULL? etc. 3) Make a global function that wraps the current processor and let it handle things. We are still dealing with globals though. 4) Create all objects via factories and have only the factories have knowledge of the current procesor. However, i don't necessarily want *everything* produced from a factory. Any other ideas?

Share this post


Link to post
Share on other sites
Advertisement
Therr is a good article in one of the GameGems series that addresses this by proposinga purely message driven system. This probably matches your last point the closest; a object would simply send a message stating that a bullet needs to be created Here going There etc and the "factory" or managing object would be responsible for creating it. This is how i am approaching the idea as well, but that is no sign that it is a good odes :)

Matt

Share this post


Link to post
Share on other sites
Now, I don't do things this way, but...

From what you describe, some sort of event system might be the first thing I'd try. When the gun creates a bullet, it sends a new bullet event with the necissary parameters.

You could then have a chokepoint for such state changes, and a single point to divvy them out to your processors.

Personally, I don't have states setup this way. Game objects are stuffed into their own lists, which are acted upon by common 'end turn' and 'end round' functions. Render objects are stuffed into a big tree, and have their own rather static ->render() functions. Simple configuration/state configurations like who's turn it is, or if the mouse is in 'select' mode are just globals.

But I imagine my scenario is far different than yours.

Share this post


Link to post
Share on other sites
Quote:
Original post by leiavoia
I imagine so. Sounds like you are doing a strategy-oriented game?


Aye. And multiplayer, so the client has very little say over game state or even much guarantee on certain objects even existing...

Also, I'm a little curious as to how your processors work, or what they do [edit: more specifically]. It might help me other others provide better suggestions?

Share this post


Link to post
Share on other sites
My method is event-driven. I have a global event system that accepts and dispatchs events as they come in. I guess my system is more commonly known as a message-driven system, but it doesn't much matter in semantics.

Basically, everything has the ability to create events and send it to the EventSystem. The EventSystem has access to everything so that it can do what is needed. That way, I can limit the amount of access that objects have to other systems my filtering the events, if need be.

Anyways, this seems to work for me.

Share this post


Link to post
Share on other sites
Can you describe your EventManager in more detail?

Do classes 'register' for events? How do you represent the event ? etc
I'm very interested to know
Thanks

Share this post


Link to post
Share on other sites
Have a singleton that is your collection of processors, with overloaded AddXXX methods?

Share this post


Link to post
Share on other sites
I'm don't know your design too well but you could try a mediator who knows the object (set it as a singleton) and then you can have any number of processor and put the delegation logic in the mediator. This way no one knows about the processors and you can have has many as you like. Could work?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!