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

Started by
2 comments, last by nilkn 18 years, 2 months ago
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!
Advertisement
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));}
Some method to check if you want to remove a certain entity may be helpful as well. or just make update return a bool.
=======================Game project(s):www.fiend.cjb.net
Quote: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]

This topic is closed to new replies.

Advertisement