Sign in to follow this  
Surgura

Accessing other game objects

Recommended Posts

Surgura    219
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

Share this post


Link to post
Share on other sites
C0lumbo    4411
[quote name='Surgura' timestamp='1354291206' post='5005721']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
DgekGD    374
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):
[CODE]
g_EventManager::CallEvent( Event_Enemy_Moved(enemyId, new_position));
[/CODE]

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.

Share this post


Link to post
Share on other sites
frob    44904
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. Edited by frob

Share this post


Link to post
Share on other sites
Surgura    219
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?

Share this post


Link to post
Share on other sites
nife87    520
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. Edited by nife87

Share this post


Link to post
Share on other sites
Surgura    219
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.

Share this post


Link to post
Share on other sites
pinebanana    479
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:
[url="http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Style"]http://channel9.msdn...rup-Cpp11-Style[/url]

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

Share this post


Link to post
Share on other sites
pinebanana    479
[quote name='Aart' timestamp='1354357712' post='5005996']
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.
[/quote]

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:
[CODE]
std::vector<std::vector<GameObject*> > gameObjects;
[/CODE]

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.
[CODE]
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
[/CODE]

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:
[CODE]
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:
};
[/CODE]
Then.. to access a game object it may be something like this:
[CODE]
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];
}
[/CODE]
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) Edited by pinebanana

Share this post


Link to post
Share on other sites
Surgura    219
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

Share this post


Link to post
Share on other sites
__UFNHGGI_H__    305
u can use template && typeid() :-)
sample ::

[CODE]
#include <vector>
#include <Windows.h>
#include <iostream>

class GameObject
{
public:
float x,y; //position :-)
virtual void onUpdate(){};
};
class Scene
{
std::vector<GameObject*> objects; //all objects in scene :)

public:
void update(); //update all objects :D
//Add New Object To Scene :->
template < class T> T* addObject(float _x, float _y)
{
T* l_maintype = new T;
GameObject* l_obj = (GameObject*)l_maintype;
l_obj->x = _x;
l_obj->y = _y;
objects.push_back(l_obj);
return l_maintype;
}
template < class T> T* getObjectFromFirst()
{
int l_size = objects.size();
int i = 0;
const type_info& l_type = typeid(T);
while( i < l_size)
{
if(typeid(*objects[i]) == l_type)
return (T*)objects[i];
i++;
}
return NULL;
}
template < class T> T* getObjectFromLast()
{
int l_size = objects.size()-1;
const type_info& l_type = typeid(T);
while(l_size > -1)
{
if(typeid(*objects[l_size]) == l_type)
return (T*)objects[l_size];
l_size--;
}
return NULL;
}
template < class T> std::vector<T*> getObjects()
{
std::vector<T*> l_vec;
int l_size = objects.size();
int i = 0;
const type_info& l_type = typeid(T);
while( i < l_size)
{
if(typeid(*objects[i]) == l_type)
l_vec.push_back( (T*)objects[i] );
i++;
}
return l_vec;
}
};
void Scene::update()
{
int l_size = objects.size();
int i = 0;
while(i < l_size)
{
objects[i]->onUpdate();
i++;
}
}
//----------------------------
class Player : public GameObject
{
public:
float m_speed;
void onUpdate();
};
void Player::onUpdate()
{
x += m_speed;
y += m_speed;
}

int main(int argc, char* argv[])
{
Scene l_scene;
l_scene.addObject<Player>(0, 0)->m_speed = 2;
l_scene.addObject<Player>(100, 100)->m_speed = 4;
l_scene.addObject<Player>(256, 256)->m_speed = 8;
while(1)
{
l_scene.update();
Sleep(1000);
std::cout << l_scene.getObjectFromFirst<Player>()->x << '\n';
}
std::cin.get();
return 0;
}
[/CODE]

Share this post


Link to post
Share on other sites
pinebanana    479
[quote name='__UFNHGGI_H__' timestamp='1354434990' post='5006231']
u can use template && typeid() :-)
sample ::

[CODE]
#include <vector>
#include <Windows.h>
#include <iostream>

class GameObject
{
public:
float x,y; //position :-)
virtual void onUpdate(){};
};
class Scene
{
std::vector<GameObject*> objects; //all objects in scene :)

public:
void update(); //update all objects :D
//Add New Object To Scene :->
template < class T> T* addObject(float _x, float _y)
{
T* l_maintype = new T;
GameObject* l_obj = (GameObject*)l_maintype;
l_obj->x = _x;
l_obj->y = _y;
objects.push_back(l_obj);
return l_maintype;
}
template < class T> T* getObjectFromFirst()
{
int l_size = objects.size();
int i = 0;
const type_info& l_type = typeid(T);
while( i < l_size)
{
if(typeid(*objects[i]) == l_type)
return (T*)objects[i];
i++;
}
return NULL;
}
template < class T> T* getObjectFromLast()
{
int l_size = objects.size()-1;
const type_info& l_type = typeid(T);
while(l_size > -1)
{
if(typeid(*objects[l_size]) == l_type)
return (T*)objects[l_size];
l_size--;
}
return NULL;
}
template < class T> std::vector<T*> getObjects()
{
std::vector<T*> l_vec;
int l_size = objects.size();
int i = 0;
const type_info& l_type = typeid(T);
while( i < l_size)
{
if(typeid(*objects[i]) == l_type)
l_vec.push_back( (T*)objects[i] );
i++;
}
return l_vec;
}
};
void Scene::update()
{
int l_size = objects.size();
int i = 0;
while(i < l_size)
{
objects[i]->onUpdate();
i++;
}
}
//----------------------------
class Player : public GameObject
{
public:
float m_speed;
void onUpdate();
};
void Player::onUpdate()
{
x += m_speed;
y += m_speed;
}

int main(int argc, char* argv[])
{
Scene l_scene;
l_scene.addObject<Player>(0, 0)->m_speed = 2;
l_scene.addObject<Player>(100, 100)->m_speed = 4;
l_scene.addObject<Player>(256, 256)->m_speed = 8;
while(1)
{
l_scene.update();
Sleep(1000);
std::cout << l_scene.getObjectFromFirst<Player>()->x << '\n';
}
std::cin.get();
return 0;
}
[/CODE]
[/quote]

It's possible to not use type_id, or that for loop checking the type of the object. Plus I heard type_id was slow, and not supported on all platforms.

As I suggested in my previous posts, if you sort the objects added by type before-hand, it will be faster to access each GameObject. Edited by pinebanana

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this