checking a "dead pointer"

Started by
13 comments, last by Zahlman 18 years, 7 months ago
hmm i'll use the easiest method that i know of.

since units still are going to be corpses for a while after death, I keep the unit, removes all iterators in "alive-lists" and puts it in a corpse-list.

When the attacker checks next time, it'll see that it's victim is in the corpse list and can just set it's attackUnit pointer to 0, allowing it to pick a new target that isn't already dead.

The units will not be really removed before the corpses are decayed. Works fine for my rts...

Thanks for your help
Suliman
Advertisement
Since you might want to eventually have a necromancer unit (ala warcraft3)...

I would have two functions-- killUnit and removeUnit. killUnit , would be the same as if the unit was killed in battle (sets its Alive bool to false). RemoveUnit would actually free the memory, setting its pointer to null.
Insufficent Information: we need more infromationhttp://staff.samods.org/aiursrage2k/
boost's weak_ptr<> is a good way of acheiving this.
You can have an array of smart_ptr<>s as the master list and only keep weak_ptr<>s to thiem in other places. That way you can use the .lock() method of the weak_ptr to find out if it still exists.
In my RTS I used a UniqueID for every unit... so if the UniqueId wasn't the same as their target, they assumed it was gone.

Worked flawless... :D
"Game Maker For Life, probably never professional thou." =)
Off the top of my head, one way to handle uniqueIds might be as follows:

class IdKeyedThingy {  static map<int, IdKeyedThingy*> registry;  static int nextId;  IdKeyedThingy();  public:  static int create() {    int currentId = nextId;    registry[currentId] = new IdKeyedThingy;    nextId++; // ensure we continue to get unique IDs    return currentId;  }  static void kill(int which) {    delete registry[which];    registry[which] = null;    // if 'which' was invalid, this will insert a "default-constructed    // IdKeyedThingy*" - i.e. null pointer - with that key into the map, then    // delete the null pointer (which is safe), then return a reference to the    // null value and re-write it as null (which all basically has no effect    // but is safe). The net effect is that you build up bogus keys and waste    // some space but it should at least work.    // If that bugs you, "do it properly" with .find(), .insert() etc.  }  static IdKeyedThingy* get(int which) {    return registry[which];    // if invalid or previously killed, a null pointer is returned - but now    // at least it's a *valid* pointer.  }}// You could also put the management stuff outside the class, e.g. as globals in // a namespace, or something.// Usage example:Enemy e, e2;int target = IdKeyedThingy::create();e.target = target;e2.target = target;e.fire(); // where the implementation will ::get() the thingy// and check for a null pointer.if (IdKeyedThingy::get(target) && IdKeyedThingy::get(target)->hp <= 0 ) {  IdKeyedThingy::kill(target);}e2.fire(); // no problem; can easily determine that it's already dead.


Can be further improved by use of the Null Object pattern, functors for removal (in general, making the class aware of the conditions that trigger a kill() and letting it do the work), and so on.

This topic is closed to new replies.

Advertisement