Accessing other game objects

Started by
10 comments, last by pinebanana 11 years, 4 months ago
Hello,

I am designing the object hierarchy for a small game I want to make in C++.
At a certain point I have a list with all game objects in it, like enemies, trees, the player, etcetera.
What would be a good way of accessing other game objects in one of these objects? For example if an enemy has to check wether another enemy is nearby.

My first idea is making multiple lists inside the list for every different game object, but this doesn't seem very practical.
I am sure you have better ideas.

Thanks,

Aart
Advertisement

My first idea is making multiple lists inside the list for every different game object, but this doesn't seem very practical.
I am sure you have better ideas.


There's nothing particularly impractical about having type-specific lists as well as a list of all entities, particularly for a small game. Simple, pragmatic approaches often a good idea, not least because they're easier to replace when you have a clear picture of where the bottlenecks are.

If you have a particular issue which has come up in profiling (e.g. iterating through all enemies is taking too much frame time) or that you know you're going to face (e.g. you're trying to push the envelope in terms of number of opponents), then that's the time to start looking for other, more complicated approaches, like spatial hashing for instance.
You probably need some event manager in your game.
Just consider there is global instance of Event Manager, which can send different sorts of events in your game. Every game object has corresponding listener for the events (all of them are inherited from some abstract EventListener class).

If your enemy moved for example you send event(this is how it looks in my engine):

g_EventManager::CallEvent( Event_Enemy_Moved(enemyId, new_position));


All listeners added to the manager will try to handle this event type. If some listener cares about the event, you can write your code to handle it. You can differentiate event types by unique static field in each event type.
The hierarchy manager could also have search functions. For example, I have frequently seen functions similar to this:

std::list<GameObjects*> manager::FindAllObjects( predicate testFunction );
std::list<GameObjects*> manager::FindAllObjectsInRadius(float range);
std::list<GameObjects*> manager::FindAllObjectsOfType(objectType type);
std::list<GameObjects*> manager::FindAllObjectsOfTypeInRadius(objectType type, float range);

and so on. The list of search functions tends to grow as the engine matures.
Thanks all.
I will try to implement functions similar to the ones frob mentioned, that seems like a good way of easily fixing what I need.

I have another related question now.
What would be a good way of accessing such a hierarchy manager?
All my objects currently have a variable in which their parent is stored, so by iterating through all these variables they will end up at the root, which will be the hierarchy manager. I don't like this way though, I think it's rather slow. Would it be better to pass the hierarchy manager to every objects contained in this specific hierarchy?
Iff the hierarchy manager owns the objects in question (or is otherwise guaranteed equal or longer life time), you can pass a HierarchyManager * to every object, like you do with their parents. A simple pointer is fast to dereference and does not waste much space. If the objects are not guaranteed to die before their manager (but be sure of the reasoning of this decision), you might want to look into a std::shared_ptr<HierarchyManager> (or boost::shared_ptr if your compiler is pre-C++0X/C++11) or another form of reference-counting.
Alright, that helps me.

I have run into another problem though.
Since the hierarchy manager will have functions that returns objects of a ceirtain requested type, would it be a good idea to keep a different list for each object type? Iterating through all objects and checking their type seems pretty slow to me.
Out of curiosity.. Why are you using a list? Why not a std::vector? A std::vector has better performance than a std::list, and I'm not only talking about memory usage, a std::vector is actually much faster than a std::list. (CPU caching and such)

A further explanation is here:
http://channel9.msdn...rup-Cpp11-Style

Skip to around 44 minutes, he gets into it at like 44:40. (Although I recommend watching all of it, if you haven't)

anax - An open source C++ entity system


Alright, that helps me.

I have run into another problem though.
Since the hierarchy manager will have functions that returns objects of a ceirtain requested type, would it be a good idea to keep a different list for each object type? Iterating through all objects and checking their type seems pretty slow to me.


Although it may be complicated, this can be achieved through mapping each type of GameObject to an index in an array.
I realise that sentence doesn't make all that much sense, but I'll try to explain it further.
Basically this may be a way of storing your GameObjects:

std::vector<std::vector<GameObject*> > gameObjects;


Where the first index is the GameObject's class type ID, and the second index is the GameObject's ID, or vise versia (1st = GAMEOBJECT_ID and 2nd = TYPE_ID)
i.e.

gameObjects[TYPE_ID][GAMEOBJECT_ID]
// or
gameObjects[GAMEOBJECT_ID][TYPE_ID] // this way may be more efficient, since there is more GameObject objects than there is GameObject classes


This means you will need to store an ID in the GameObject, and register the ID when you add the GameObject.

Now to store/get the TYPE_ID of a GameObject calss is possible without the use of RTTI you just need to think hard (you can do it with virtual functions and macros; using virtual functions since you need to know what type a GameObject is). Currently, I am doing something similiar (storing objects by type) by using macros that are defined WITHIN each class.

For example, here's what a class that derives a GameObject may look like:

class Player : public GameObject
{
// registers the class, adds an ID to the game object CLASS
// also defines virtual/staitc functions to retrieve class information (ID, name, etc.)
REGISTER_CLASS(Player)
public:
};

Then.. to access a game object it may be something like this:

template <typename Type>
Type* getGameObject(int id)
{
return gameObjects[typename Type::GetClass().getId()][id];
}
// or...
GameObject* getGameObject(const GameObject::Class& gameObjectClassType, int id)
{
return gameObject[gameObjectClassType.getId()][id];
}

Of course, I don't really want to give it away all that much, and there is probably another method of doing this (through the use of templates)

anax - An open source C++ entity system

Sorry, what I actually meant was an std::vector. Thanks for the link though, I'll check it out.
I'll see what I can do with the information I have now. Time to write everything down and create some structure graphs.

Thanks all.

Aart

This topic is closed to new replies.

Advertisement