Page 1 of 1

Handling creation/deletion of units within game

PostPosted: Apr 13, 2004 @ 8:02am
by j.edwards
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?

PostPosted: Apr 13, 2004 @ 10:28am
by Peter Balogh
I don't see how that would fix your problems. Sure, it'd get rid of access violations, but you'd have a rather high chance of confusion in the game. E.g. the index of the unit your rocket is targeting would be freed after the target is destroyed and would be shortly reused for another rocket objects, etc... The way to do it is either keep a reverse list of objects referencing each unit, so when something happens to the unit it can call a HandleEvent function in all the objects referencing it, so they can all handle the situation in their own fashion. (Like a target-locked rocket could self-destroy or find a new target, etc)

Cheers,
Peter of PDAmill

PostPosted: Apr 13, 2004 @ 11:15am
by j.edwards
Peter: I was trying to save on lists per object, but you are right. I will give the suggestion a go. Thanks.

PostPosted: Apr 13, 2004 @ 6:37pm
by Dan East

PostPosted: Apr 13, 2004 @ 9:08pm
by warmi

PostPosted: Apr 14, 2004 @ 3:06am
by j.edwards

PostPosted: Apr 14, 2004 @ 4:05am
by warmi

PostPosted: Apr 14, 2004 @ 7:24am
by Peter Balogh

PostPosted: Apr 14, 2004 @ 9:04pm
by kaboomie