When writing a game, should you make objects/enemies/etc. have unique ID numbers?

Posted by SLC on Stack Overflow See other posts from Stack Overflow or by SLC
Published on 2010-04-11T13:12:05Z Indexed on 2010/04/11 13:23 UTC
Read the original article Hit count: 485

I have recently encountered some issues with merely passing references to objects/enemies in a game I am making, and am wondering if I am using the wrong approach.

The main issue I have is disposing of enemies and objects, when other enemies or players may still have links to them.

For example, if you have a Rabbit, and a Wolf, the Wolf may have selected the Rabbit to be its target. What I am doing, is the wolf has a GameObject Target = null; and when it decides it is hungry, the Target becomes the Rabbit. If the Rabbit then dies, such as another wolf killing it, it cannot be removed from the game properly because this wolf still has a reference to it.

In addition, if you are using a decoupled approach, the rabbit could hit by lightning, reducing its health to below zero. When it next updates itself, it realises it has died, and is removed from the game... but there is no way to update everything that is interested in it.

If you gave every enemy a unique ID, you could simply use references to that instead, and use a central lookup class that handled it. If the monster died, the lookup class could remove it from its own index, and subsequently anything trying to access it would be informed that it's dead, and then they could act accordingly.

Any thoughts on this?

© Stack Overflow or respective owner

Related posts about game-development

Related posts about object-oriented-design