Jump to content
  • Advertisement
Sign in to follow this  
dpadam450

(Super) Smart Pointer

This topic is 1469 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

Imagine an RTS game, you have 20 marines shooting at one building and it blows up. All the marines have AI pointers "targetedEnemy" pointing at the building. The building blows up and is removed from the world and deleted.

Is there any good design for this?----> Updating the 20 marines "targetedEnemy" pointers that is to be NULL so they can go looking for new enemies?

shared_ptr doesn't seem to work because that is reference counted. It WOULD work if I decide that the marine AI checked their enemies health and if health < 0, call reset on the pointer. After the 20 marines all call reset() then the object would officially be deleted.

The other option I thought of was creating a new pointer type (unless one exists), where anytime I deconstruct a copy of the pointer, it has maintainted a list of people pointing to it and sets all pointers in the list to NULL.

Something like this:

 

template <class T>;
class SuperPointer

{

       T*  data;
       vector<T**> owners;
       ~SuperPointer(){ // loop through owners setting them to NULL };

};

Anyone can essentially delete the data, it will update all respective pointers who are pointing to the data. Maybe there is a better way to think about this or there is some other pointer that does this?

Edited by dpadam450

Share this post


Link to post
Share on other sites
Advertisement

All the marines have AI pointers "targetedEnemy" pointing at the building.

 

A good programming practice would be for the AI to check if the enemy exists (not just attributes of the enemy.) If it doesn't exist, the AI should handle that.

 

For each object that needs it, you can provide a pointer to a pointer to the "enemy." When the enemy is destroyed, set the pointer-to-enemy to NULL. Then the pointer-to-pointer will return NULL.

Enemy *enemy = new Enemy;
...
AI::Enemy **aiEnemy;

for each AI( AI.aiEnemyPtr = &enemy; )
...
AI::CheckEnemy() { if (*aiEnemyPtr==NULL) Change_AIBehavior(); }
...
when (enemy->killed ) { delete enemy; enemy = NULL; }
Edited by Buckeye

Share this post


Link to post
Share on other sites

To check if it exists, it would have to go into a list of objects in the world to see if it is in that list still.......?

If that is what you mean, I definitely don't want to iterate through all the objects in the world for each of the 20 marines to see if their enemy is still in some world list.

Share this post


Link to post
Share on other sites

As Buckeye says, make it the AI's job to work out if the target is still there.

 

You can also have fun with it when you work this way.

 

A berserker that keeps firing at his target until he runs out of ammo, regardless of the state of the target. "Dance MF ..DANCE!"

 

A target that plays dead, then comes back to life to attack when least expected. (Nice hammy animation, "Mutter ich bin Todd".... etc. Just think of the worst overacting you have every seen and animate it smile.png )

 

Switching targets when you lose line of sight.

 

Switching targets based on percieved threats

 

Loads of things you can play with.

Share this post


Link to post
Share on other sites

 

As Buckeye says, make it the AI's job to work out if the target is still there.

Right, but that's what I'm saying, I'd rather not iterate every object on the other team to determine if my target is dead or not. In that case, each 20 marines is pointing to say the last object on the enemy team. If the enemy team has 1,000 objects, then

//Do this 20 times?
marine.ComputeAI() { // loop through to see if enemy exists in list........has to loop 1,000 objects}

 

 

^^^ Was this the original solution to the problem? I didn't get what the solution being suggested was. I think it was this. I guess if I finally partition my world objects, then I wouldn't have to search through that many objects and that would be ok.

Edited by dpadam450

Share this post


Link to post
Share on other sites
Then loop through the list of marines and set enemyPtr to NULL if enemyPtr==building. That only has to be done when something goes out of existence. Edited by Buckeye

Share this post


Link to post
Share on other sites

I probably wouldn't have any of this information inside the soldiers or the enemy. I'd probbaly have the GamePlay class store maps of soldiers to targets and a reverse of that map and then each frame (or at some other more appropriate time) have the GamePlay class evaluate the situation and update the maps based on targets going down or so on.

 

Just do it the easiest way you can because when you come back to debug it in 6 months time you won't be stumped.

Share this post


Link to post
Share on other sites

Read Buckeye's original post again. If the AI has a pointer *to the pointer* to the enemy, they can all see whether the enemy object exists/NULL or not with a single if-statement. No looping. The AI just sees a lack of enemy next time they get updated.

Share this post


Link to post
Share on other sites

We call this a "Weak Reference".

I don't have examples at hand, but that's the term you want to search for smile.png

 

Edit: What BitMaster said. std::weak_ptr

Edited by Daivuk

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!