Managing objects in a game

Hello everybody.

Suppose that in a game, the objects are exchanging information like position and speed, etc. so they can track one another.

At some point, objects will have to die, and all their tracker objects must stop tracking them (i.e ask them for information), or they can't be destroyed.

I started implementing this with dumb pointers and counters, but it feels wrong.

So the question is: should I implement an object manager, or use smart pointers like std::shared_ptr, or try something in-between, or completely different? How would you do it?

Thanks for reading.
I'd do it the other way around: Not letting the objects track each other, but letting the objects themselves broadcast their position, speed and so on to all objects in the game. I mean you probably have an object which keeps track of how many objects there are in the game, right? Then you can use a broadcast function in that object which the objects can access periodically and broadcast their data.

Along with this data objects would also broadcast a live signal, indicating they still exist. When they are destroyed the other objects would notice that simply by not receiving a live signal within a defined period of time, like a timeout.

Or, even better, let the destructor of the object broadcast a death message to all other objects. That way the other objects can simply delete the destroyed object from their array or list of objects they have to avoid collision with.

That's probably simplified and would not work literally this way, but that's how I would do it.
Last edited on
It sounds like you need to implement an AI system. I agree with Serious Sam that instead of objects tracking other objects and being notified of state changes they should request the information when needed. It really depends on the type of game and the overall design I think.
Last edited on
closed account (zb0S216C)
Serious Sam does have a point. It's a bit like talking on a phone:

Man A) "Where are you?"
Man B) "Just north of KFC."
Man A) "What are you wearing?"
Man B) "Why do you need to know that?"
Man A) "I don't know."

Man A only wanted the position of Man B. Man A asked Man B to describe the clothing worn by Man B, but why would Man A need such information? He doesn't.

Wazzak
Yes... the idea of broadcasting makes sense:

A broadcasts info to ObservableWorld (maybe including something like "my id is A").
B retrieves info from ObservableWorld and analyzes it.

Where:
- A and B are Objects
- ObservableWorld is a global pool of info, which is pointed to?

The second bullet doesn't seem quite right.
If there's only one ObservableWorld, making it a global then pointing to it seems clumsy.
More thoughts?
Last edited on
Topic archived. No new replies allowed.