Managing multiple references of the same game entity in different places using IDs

Posted by vargonian on Game Development See other posts from Game Development or by vargonian
Published on 2010-11-02T07:02:14Z Indexed on 2011/01/17 19:59 UTC
Read the original article Hit count: 259

Filed under:
|
|

I've seen great questions on similar topics, but none that addressed this particular method:

Given that I have multiple collections of game entities in my [XNA Game Studio] game, with many entities belonging to multiple lists, I'm considering ways I could keep track of whenever an entity is destroyed and remove it from the lists it belongs to.

A lot of potential methods seem sloppy/convoluted, but I'm reminded of a way I've seen before in which, instead of having multiple collections of game entities, you have collections of game entity IDs instead. These IDs map to game entities via a central "database" (perhaps just a hash table).

So, whenever any bit of code wants to access a game entity's members, it first checks to see if it's even in the database still. If not, it can react accordingly.

Is this a sound approach? It seems that it would eliminate many of the risks/hassles of storing multiple lists, with the tradeoff being the cost of the lookup every time you want to access an object.

© Game Development or respective owner

Related posts about XNA

Related posts about architecture