Jump to content
  • Advertisement
Sign in to follow this  

best /most common way of handling in-game object list

This topic is 4679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone ! I'm currently working on a small game with relatively large lists of ingame objects : the way i'm currently handling this is with a linked list class in which every node has a "void*" member for data storage aswell as a "dataType" member (in order to be able to do some specific operations which require casting). I found this type of list to be quite easy to work with as it handles any type of objects , buti was wondering which is actually the most commonly used methode for storing game objects (besides STL lists or vectors, which i wont be using for training purposes) ? Should i separate my object list into multiple ones, based on object types,(template based) which seems kinda clumsy even with its advantages, or is the current method just fine ? Sorry for the long post , and thanks in advance for the answer!

Share this post

Link to post
Share on other sites
Hmm, I don't know if I completely understand your first method. Your second method is a definite no, however.

A very simple and clean way of going about this is to derive all game entities off of an abstract interface, say, IEntity. Store entities by maintaining a container of IEntity pointers.

IEntity would need certain pure virtual methods which all entities should implement. These might be update(), render(), and perhaps some others.

If, for example, you stored your IEntity pointers in a vector (or, in fact, any other STL container), you could do the following:

#include <vector>
#include <algorithm>
#include <boost/bind.hpp>

typedef std::vector<IEntity*> EntityVec;
EntityVec entities;

struct UpdateEntityFunctor {
void operator ()(const float dt, IEntity* Ent) { Ent->update(dt); };

// ...

void updateGame( const float dt )
// ...

std::for_each(entities.begin(), entities.end(), boost::bind<void>(UpdateEntityFunctor(), dt, _1));

Share this post

Link to post
Share on other sites
Original post by JonatanHedborg
Some method to check if you want to remove a certain entity may be helpful as well. or just make update return a bool.

If you want to continue on in a fashion similarly to std::for_each, then the following might suffice:

struct RemoveEntityIfFunctor {
const bool operator ()(IEntity* Ent) { return !Ent->isAlive(); }

// ... later on in updateGame() ...

entities.erase(std::remove_if(entities.begin(), entities.end(), RemoveEntityIfFunctor()), entities.end());

Edit: I somehow managed to forget all about boost::mem_fn. You may use this to automatically generate the above functors in place. So:

entities.erase(std::remove_if(entities.begin(), entities.end(), boost::mem_fn<const bool>(&IEntity::isDead)), entities.end());

[Edited by - nilkn on January 22, 2006 6:03:15 PM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!