AI pointing to dead character

Hey guys,

So I’m making a game in which spawning minions are attacking the ones on the other team (like in a moba). My problem comes when a single minion dies while other minions are moving toward him/attacking him. It obviously makes the AI go nuts since it cannot find the character. My question is pretty simple then : How can I avoid this kind of problem? How can I make sure that when a minion dies, everyone else in the game will stop pointing towards it?

Thanks alot!

If the object that is destroyed is a UObject, then if you use TSharedPtr to point to the object, IsValid() will check if the reference is still valid.
This will work if the object has been destroyed, though the pointers may prevent the object from being garbage collected.

So perhaps in combination with this you could define a function bool MinionClass::IsDead(), which could be called by the actors holding the pointers after checking IsValid() on the pointer, and would return true if the minion has been killed (in which case you select a new target etc) and false otherwise.

Does that solve it?

Mmmm worth the shot! But here is what I was thinking… tell me if it could work.

Instead of having the other minions checking if the target is dead, would’nt it be more easier to make the dying minion remove itself from all pointers? But for that I would need to access the blackboard of all minions to check what is the ID stored and compare it to the dying minion’s one. If it matches, reset the pointer. And that, is the problem. I don’t know if I can access the blackboard from a character class… What do you think?

Thanks for your help. I’ll try your idea right away

The problem with that sort of approach is that it isn’t thread or exception safe, and will be slow. As you go through the blackboard, all the minions you haven’t checked are still trying to get to you, so if something goes wrong with the dying minion you’ll get a bug, if any of the minions above where you are on the list copy their pointer value from any of the ones from below you on the list, you’ll get a bug. And what value would you be resetting to? If you change it to another minions pointer, what if that minion dies whilst you’re halfway through? If you’re changing them to NULL, you’re already performing checks with the askers, and IsValid will simply behave better.

The trick is to remember to check IsValid() any time the target could have died since you last asked.

Makes total sens. So if I understand right, I have to change my “Target” blackboard key to a TSharedPtr one? Or do I keep the same Key but give it a TSharedPtr value? I’m sorry there is just this part that I’m not sure to understand.

Thanks alot for your help

Ah, so that’s where your pointers are. So on your blackboard you have keys that are currently bare pointers to the minions. All you have to do is, when you use that pointer, instead of just using it bare, create a TSharedPtr object from it thus TSharedPtr minionPtr(BlackboardPointerValue) then call IsValid() etc on that (minionPtr.IsValid()) as above. That should work if I’ve read the API right.

I hope this will be my last question,

So I did as you said :

TSharedPtr minionPtr(OwnerComp.GetBlackboardComponent()->GetValue<UBlackboardKeyType_Object>(MinionPC->EnemyKeyID));

But now I get an error C2955 : ‘TSharedPtr’: use of class template requires template arguments list.

so I went to take a look at the declaration and here is what I found:

mutable class TSharedPtr<struct FKeyDetails> KeyDetails;

I’m really trying to make sens of all this but this is basically chinese to me.

My bad, it should be

TWeakObjectPtr<MinionClass>

TSharedPtr are for non-UObject types, I misremembered. The text with the < > is a template. You say what type of thing the template is of inside the template marker things. So if it were pointing to an actor, you’d put

TWeakObjectPtr<AActor>

, whilst if it pointed at a static mesh you’d put

TWeakObjectPtr<UStaticMeshComponent>

. I suggest looking up C++ templates if you’re still confused (or maybe doing the project with blueprints if you can - they’re really powerful and may be less confusing)

Hey,

Very sorry for the delay, I got caught up in the end of my semester.So yeah, I realized that I knew what is a template, just didn’t know the name for it.

I got to thank you, your solution made the game a lot more stable even my behavior tree is still doing very weird stuff. I will mark this question as answered.

Thanks again!