how to iterate through a array to get the actor that is closes

been trying to pick up a object that has replicated objects in the world.thing is i have simulate physics sleeping until i trigger a attempt to pickup this object. then on press it wakes the object and i can grab it…and throw it… but i have a destroy actor on the end of the function using a inherited bp class it is also destroying all the other actors . i am lost on the three type variables i should make i am assuming 2 floats furthest distance and closest distance… also a actor type variable but pretty much i am lost.i need to return the closest actor out of a for loop…please help
i am not useing a player controller but instead a pawn that moves around with my camera to pickup objects…some sort of examples or tutorials would be awesome… i am only a few months into unreal i have taken some courses so i have a pretty good understanding thanks

1 Like

so create 2 variables, 1. smallest distance (float) and 2. closest actor.
in loop get your pawn location minus object location and use node “vector length” this gives you float distance. Save actor to reference as closest actor.
if distance of second actor is longer than previous, check next actor else rewrite reference to closest actor.

anyway you could give me a example… i understand what your saying but not sure how to wire it up i got something like this going so far… i am a noob i know lol… i cant seem t be able to Set closest actor from where i am i think i got it totaly wrong anyway lol

it should be 2 floats right because what would i have to compare with if only 1 variable of type float and 1 of type actor ? so i need 2 floats and 1 actor variable ?

Follow this flow:
Use a “sphere overlap actors” node set to scan within some radius and save that array as a variable
Loop through the array, and calculate/store distances in a float array so that the two arrays have “linked” indices like 3:3, 5:5, etc
After that’s complete, use a “min of float array” node on the float array and it would output the index of the closest actor
Use that index number to pull from the actors array and you’re good to go.

EDIT: Oh and after you get the array index you’re looking for, use a GET on the actor array, and save that actor to your closest actor variable(single, not array).

I’ve mocked up a quick function to get the nearest actor from given array of actors. The function is made in character blueprint.
Important! Remember to set the default value of DistanceToNearestActorLOCAL float to 10000 or even higher if you will gather objects from bigger distance.

Optionally, you could move this function to Custom Function Library to access it from anywhere in the blueprints. But if you are planning to use it only inside character blueprint it should be fine.

Get all actors of class is expensive and wasteful. Use the sphere overlap setup like I described earlier:
EDIT: Oh and make sure to set the class filter if you need it. If you have a master parent “enemy npc” class, it will pick up all child actors as well. And if you set it to a child, it won’t pick up the parent. Allows for some specificity if you need it.

Well there isn’t anything wrong with what exadi did. It’s just that depending on how many actors you’ve got, it can bog down a lot. If your level only has 20 actors of a given class, then it’s no big deal. If it had 2000 actors, then it’s a big deal.

You might want to make sure you’ve got a high enough radius 100=1m=~3.3ft. Ironically, when I set up the example, I wasn’t getting any overlaps either until I remembered that I had a radius of zero lol… If you’re still having problems, you will need to make sure to correctly set the actor’s collision settings. The documentation does a decent job of explaining it all.

I wouldn’t suggest using GetActorsOfClass, either. I’ve just used the example micabreen gave to show how to use the function I’ve mocked up for him.
Unless it is needed for specific gameplay purposes there is really no need to get all actors.

Usually, you want to get actors on a specified range and then get the nearest one.

thanks for all the reply this is great… what i am having a issue with is that i want to use a line trace to pickup a rock… but there are hundreds of rocks in the world (that you can pick up) currently physics is set to true and sleeping… so when line trace hits i wish to set state to awake on only the rock i hit are the one in front of me… the only way i could think of to do this was to get all actors of class and set physics to awake i am really new to unreal so i am sure ther is a better way but my thinking was… i get all actors of class then get closest one to me then execute the awake? any suggestions would be awesome thanks
@IronicParadox i tried to use your example but when i get to the call to wake it is giving me error i tried making a ref to the rock class … since these functions are in player controller the rocks have to be reffrenced into this i thought here is what i tried to do… thanks

https://forums.unrealengine.com/filedata/fetch?photoid=1413844&type=thumb