I'm developing a game that has units which are continually created and deleted as units are built and destroyed and am running into problems with objects referencing units.
For instance, a smart missile has a lock target of type Unit. It's locked target may actually get destroyed before the missile gets to it etc causing access violations. I tried a quick fix by adding an "Active" property which helped a bit but doesn't catch all cases and is messy to work with.
Units are currently stored in the player class, ie. Player -> Units.
So I am thinking about having a "Unit Register" which is basically an array of all units. As new units are created they are added to the register, and as destroyed they are deleted, and instead of referrencing the unit directly it referrences the element in the unit register array.
I thought I would post here before implementing just to see if anyone knows of a better way ... any suggestions?