Jump to content

  • Log In with Google      Sign In   
  • Create Account


Accessing other game objects


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Aart   Members   -  Reputation: 218

Like
0Likes
Like

Posted 30 November 2012 - 10:00 AM

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

Sponsor:

#2 C0lumbo   Crossbones+   -  Reputation: 2204

Like
2Likes
Like

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 DgekGD   Members   -  Reputation: 374

Like
1Likes
Like

Posted 30 November 2012 - 02:22 PM

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.

#4 frob   Moderators   -  Reputation: 20363

Like
3Likes
Like

Posted 30 November 2012 - 02:27 PM

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, 30 November 2012 - 02:30 PM.

Check out my personal indie blog at bryanwagstaff.com.

#5 Aart   Members   -  Reputation: 218

Like
0Likes
Like

Posted 30 November 2012 - 04:53 PM

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?

#6 nife87   Members   -  Reputation: 516

Like
2Likes
Like

Posted 01 December 2012 - 03:30 AM

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, 01 December 2012 - 03:34 AM.


#7 Aart   Members   -  Reputation: 218

Like
0Likes
Like

Posted 01 December 2012 - 04:28 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.

#8 pinebanana   Members   -  Reputation: 475

Like
0Likes
Like

Posted 01 December 2012 - 07:24 AM

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)

Edited by pinebanana, 01 December 2012 - 07:25 AM.

anax - An open source C++ entity system


#9 pinebanana   Members   -  Reputation: 475

Like
0Likes
Like

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.

anax - An open source C++ entity system


#10 Aart   Members   -  Reputation: 218

Like
0Likes
Like

Posted 01 December 2012 - 07:42 AM

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

#11 __UFNHGGI_H__   Members   -  Reputation: 305

Like
0Likes
Like

Posted 02 December 2012 - 01:56 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;
}


#12 pinebanana   Members   -  Reputation: 475

Like
1Likes
Like

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.

anax - An open source C++ entity system





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS