• Advertisement
Sign in to follow this  

checking a "dead pointer"

This topic is 4619 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement