Accessing other game objects
#1 Members - Reputation: 137
Posted 30 November 2012 - 10:00 AM
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
#2 Members - Reputation: 934
Posted 30 November 2012 - 10:27 AM
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.
#3 Members - Reputation: 359
Posted 30 November 2012 - 02:22 PM
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.
#4 Moderators - Reputation: 7796
Posted 30 November 2012 - 02:27 PM
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, 30 November 2012 - 02:30 PM.
#5 Members - Reputation: 137
Posted 30 November 2012 - 04:53 PM
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?
#6 GDNet+ - Reputation: 516
Posted 01 December 2012 - 03:30 AM
Edited by nife87, 01 December 2012 - 03:34 AM.
#7 Members - Reputation: 137
Posted 01 December 2012 - 04:28 AM
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.
#8 Members - Reputation: 329
Posted 01 December 2012 - 07:24 AM
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)
Edited by pinebanana, 01 December 2012 - 07:25 AM.
#9 Members - Reputation: 329
Posted 01 December 2012 - 07:34 AM
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)
Edited by pinebanana, 01 December 2012 - 07:41 AM.
#11 Members - Reputation: 191
Posted 02 December 2012 - 01:56 AM
sample ::
#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 <img src='http://public.gamedev.net//public/style_emoticons/default/smile.png' class='bbc_emoticon' alt=':)' />
public:
void update(); //update all objects <img src='http://public.gamedev.net//public/style_emoticons/default/biggrin.png' class='bbc_emoticon' alt=':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;
}
#12 Members - Reputation: 329
Posted 02 December 2012 - 05:02 AM
u can use template && typeid() :-)
sample ::#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 <img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' /> public: void update(); //update all objects <img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/biggrin.png' class='bbc_emoticon' alt=':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; }
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, 02 December 2012 - 05:05 AM.






