Get nearest actor to AIPawn

Hello,

I’ve been struggling with for a couple of days now. I think I have the logic set up for it (below), but I’m not sure how to implement a couple of steps with blueprint.
I am trying to implement a cover system. The AI searches for the nearest CoverNode blueprint (within a radius, preferably) and returns the location of that cover node to Blackboard. Simple right…?

/*Logic for how to get the nearest CoverNode and return it’s location to Blackboard (no radius implemented yet.)

Get an array of all actors of CoverNode class
For each, get location,

Get the location of the AIPawn
Compare the location of the AIPawn with each CoverNode location
Find the smallest value (i.e the nearest)
Store the index of that CoverNode as a variable (BestNode)

Cast to the Node array with the index of BestNode and get location of that node
Set blackboard key
*/

I’ve not used arrays much before, so I’m very unsure how to get the index of a specific variable in an array, or how to search through them.
is what I have so far,


I am not sure if is the correct way of going about what I want to achieve. is a Task blueprint, in case anyone is wondering.

Any help is much appreciated.

You would use the Get Distance To and CompareFloat nodes to do that.

2 Likes

Thanks Mikesdav!
That’s a great way to sort through and find the distance.
How can I get the index of that actor in the array?

Looking at the array tools I can see one called ‘find item’ but I’m not sure if that is useful in circumstance.

You can use Array Index in the ForEachLoop for that.

Thanks for the help.
The array index in the ForEachLoop did indeed give me what I wanted. But strangely, it didn’t work the first time I tried it. I might have missed something, or it may have been a bug (always stored index 0 as the shortest distance). I closed the editor and rebuilt the blueprint and it started working correctly.

Now onto implementing a radius. A the moment it searches all cover nodes in the entire level. That doesn’t seem too efficient to me.
Any ideas???

Multi Sphere Trace for Objects is how I’d do that…I’d just adjust the Radius to a reasonable size…

How would I go about getting the actors I need?
I see there is an ‘Actors to Ignore’ input, but not an ‘Actors to Include’ (ignoring every other type would end up as a massive list). The ‘object types’ input of course doesn’t allow actors.
Sorry to ask, I can’t find any documentation on this.

I’d extract the actors I need from the Array that was made from the Sphere Trace…
use the “For EachLoop” after the Trace and “Break Hit” from the ArrayElement on it…
Create an Object array for and add the “Hit Actor” to it from there “Filter Array” by whichever Blueprint Class you’d like…
From there continue on and Check the distance of the objects in the new Filtered array…
I’m no pro so I won’t be surprised if there’s a better way but should get the job done…

Wow, you came to a lot of the same conclusions I did. I have a working BT Service for what you are describing:

Is similar to what you are making? I can help you out if you have any questions about it.

I was wondering if maybe you could help me. I’m trying to implement a similar feature but as soon as I add in more than one NPC, the distance search fails. Here’s my network:


When there’s one NPC in the scene it works fine, but if there’s two, then it always returns a fail state, as if it’s getting the distance of all the NPCs from the player and not just finding the nearest NPC.

Hi, I realize is a old thread but for anyone stumbling across the issue with **TorQueMoD**´s implementation is that the for loop ends with a finish execute. means that the RecieveExecute will only ever be able to loop through one iteration of the ForEachLoop. Instead he should en with the FinishExecute from the *Completed *node of the ForEachLoop.

As for **mikesdav**´s distance calculation loop; It works and works well. However, for those that get the issue of no returning actors it is important to set the inital value of “LowestFloat” variable to something pretty high since will be the first comparison. If it is set to default 0 nothing will ever match as lower distance. The initial value of “LowestFloat” can therefore be interpereted as a maximum distance check. Anything further away than it´s initial value will never be accepted.

Wow, thanks Artsie! That’s a really simple solution :stuck_out_tongue: