How can I get the nearest actor and store it's array index?

Hello,

I’ve been struggling with this 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.
This is what I have so far,

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

Any help is much appreciated.

Try using a sphere overlap that covers the area you want the AI to search. Then set the Actor Class Filter to make sure the overlap node only looks for objects of the class you are looking for.

The sphere overlap will output an array of actors, so feed that into an Add node with your Array Variable attached.

Then, do a ForEachLoop on the array and pull the distance from the AI to each cover. Create a Distance Variable and set it to a very high number. As the loop looks at each cover’s distance, compare the distance to the Distance Variable. If the new distance is shorter, set the Distance Variable to be whatever that new distance is.

Here is an example from my project:

In your case, you may also want a second Variable that sets to the Index of the For Each Loop and only does so when a new shortest distance is found. That way you can reference that variable against your array and get the index of the shortest cover.

That should get you started. I haven’t touched blackboard yet so I am no help there, but it should just be a matter of inserting somewhere in the mix.

Hi !

I want to do the same thing and i have the same blueprint than you but… How it’s possible to get the array index of the actor ?

You might want to post your own question.