checking a "dead pointer"

Started by
13 comments, last by Zahlman 18 years, 7 months ago
hi in my rts, lets say 50 good guys find a bad guy and all start shooting at it. Their "unit * targetUnit" is set to this bad guy. Eventually he dies and he is removed. But all these good guys will then target something that doesn't exist, causing a crash or other unpleasant results. How to check if the pointer no longer points to a instance of the type its supposed to point at? Can I do that at all? To have a victim store a pointer to every one that targets him and at the moment of death clear all those attacker's target seems like overkill. Where do I go? Thanks Suliman
Advertisement
I dont think there is a way to test if a pointer is valid or not.
What you could do is some sort of reference counting system. Everytime an unit targets another one, increase a counter for the enemy unit and when they target something else decrease it, so you end up with a counter holding how many units have targeted. Now instead of deleting the unit outright mark it as dead and let the other units find other targets, once the counter is at 0 then delete the unit.

There are other ways you can approach the problem as well :)

Hope that gives some help.
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
"smart pointers"?

Hey,

My personal preference is to have a messaging system so that when the object 'dies', it broadcasts a 'OBJECT_DEAD' message with some attached info, so that the next time that the good guys are about to update, they check and realise that the target's dead. Basically, the same as grekster had, except my system would actively inform every other game class. There's a bunch of other cool stuff that you can use general messages for too.

Just my thoughts,

--CJM
Hi,

I would go the messaging route two. And I would not store pointers but ID's. That way you could reuse classes and just assign them a new ID.
“Always programm as if the person who will be maintaining your program is a violent psychopath that knows where you live”
I think there is another way , instead of just removing the dying enemy from the list , just remove it , but don't delete it, add it to temp list , and set some flag to dead = true.

Now once the player updates its AI , he checks if the target is dead , if it is the selects other enemy or do something else.

The idea to use temp list , is because you allocate memory , and use it all the time , just by adding removing it to current enemy list. This might be faster then just allocating and deleting memory all the time.
Essentially, what you want is some kind of "handle" to the targetted unit. If each object has a unique "object id", and this object id can be used in some kind of table (perhaps a std::map if you're using C++) to get a pointer to the object, then you're good to go. When an object is created, it gets inserted into the table - when it's destroyed, it goes away. When the unit fires, look up the object id it's targetting. If that object doesn't exist, you can use some logic there - either auto-target something else, or simply lose its target.
Quote:Original post by DMINATOR
I think there is another way , instead of just removing the dying enemy from the list , just remove it , but don't delete it, add it to temp list , and set some flag to dead = true.


DMINATOR, a problem I see with this is how long do you keep objects around in the 'temp' list before freeing the memory? If you have thousands of units being created and dying, you will want to release the memory from long-dead units. It could be done, but I think something like an ID that you can look up each tick is a more stable way to go.
Quote:Original post by RDragon1
DMINATOR, a problem I see with this is how long do you keep objects around in the 'temp' list before freeing the memory? If you have thousands of units being created and dying, you will want to release the memory from long-dead units. It could be done, but I think something like an ID that you can look up each tick is a more stable way to go.


I don't release If I want to add new unit to the scene , I just select one from the temp list , override its params (set new health , position , animation) . And put it to the processing list. It is easy fast and the best way I have seen. I am using something similar for my particle engine.
This can easily be achieved by storing a pointer-to-a-pointer in your "good guys". Then you simply check for *target being NULL rather than target itself being NULL.
Of course your master pointers will need to be stored somewhere where they wont move, like a plain old array.
It'll work, and it's perhaps more efficient than broadcasting messages.

It doesn't really have to be pointers either. any kind of system using a double-lookup of some kind will work, if you'd rather use indexes.
I'm sure you'll come up with something good enough.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement