EQS: How to find the AI that is the closest to the player

Who’s running this behavior tree, all AI right ?

yes :slight_smile: worth mentioning that the AIs get disabled at a certain radius too. I’m using the “Smart AI” asset, but this part is my own code added to it.

Why do you need an EQS to check all AI, shouldn’t the AI run a single check against the player and not all AI ?

Its just the first thing that came to mind. I’ve been making different checks through the behavior tree for, for example, if the player is holding food, or is crouched, etc. I’m making sure the checks arent all running though at once, by checking the distance between the player and the AIs too.

I’m open to anything, but I’m a little limited too with my knowledge. I’m terrible at communication between blueprints which is why I’d prefer to do it in the behavior tree if theres a way.

For quick example, nothing gets past the first circle if the AI isnt within a certain radius:

You can use a task or a service.
For the task example, create a BTTask_BlueprintBase and use it like this


and use it in your behavior tree, you should have a bool that the task can set, and you can use it later

You can replace the GetPlayerPawn if you’re dealing with multiplayer for example and need to get the target player from the blackboard.

Thats actually what I’m doing for the distance checks! :smiley: But my issue is that all AIs within that radius, run true on the condition and then all of the AIs approach. I’d like for the closest of them to do it, not all.

Or, its a little different, but isnt it kind of the same too? Or do yours do different than this?

Yours does seem more performance friendly though since it doesnt include tick.

The issue is that there is a way, but it’s ugly, a bad practice and not performant, which is why I don’t want to propose it

Like I said before, you could add a component to your character, or even do it in your character’s blueprint itself:

Then in your Blackboard add a key like so:
image

Then add this decorator to your “closest AI” subtree root:

This won’t work well in multiplayer though

Oooh I think I’m starting to understand now. I’ll give it a go :smiley: Sorry, I sometimes need to get things very well explained before I get it.

And this is a single player game, so if it works for that I’ll take it

Should I be running the custom event off of something like begin play then or do you suggest something else?

Up to you, begin play should work

You should probably also set the decorator to abort self on result change

I’ll give it a go!

I think you need to clear the object for all others that failed the test in case they were previously selected.

It’s instance synced so it will be the same for all blackboards, but you are right, you do need to clear the object when the EQS returns nothing

Do I do that off of the “is valid index” → branch → false ?

Oh so this is running in AI class ? wouldn’t it be better in the player class and the player can set the blackboard for all, you can run the EQS once too.

This is on the player, or a component on the player

Oh I just read the description of instance synced, you’re right.

I didn’t test it but I think it should work

I created the blueprint like you showed. But what do I do more? When I run it like this, it just gets a red mark on it. Come to think of, I havent used “compare blackboard entries” yet before either so I’m trying to read up on it.

image

edit:

it seems that during runtime, the “selfactor” key is the fox. Is it not supposed to be the player?

During play, can you check the value of ClosestAI in your blackboard? SelfActor is supposed to be the AI running the BT