• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Surgura

Accessing other game objects

11 posts in this topic

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
0

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
[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
0

Share this post


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

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
[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
1

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  
Followers 0