Get all actor of class, sort by distance

Hello. I have “get all actor of class” and “simple move to location”. I have a few actors. How to sort “get all actor of class” by distnace (actor-me)?

I first went to the nearest enemy, then to continue, and so on

How to approach this would change depending on exactly what you’re interested in doing. Do you only want to move to the nearest actor? If so, you can just find the closest actor each time, rather than sorting all of the actors. It also depends on how many actors there are. If there are only a couple, sorting won’t be too bad, and if they all already exist when BeginPlay is called you can sort them then and won’t have to worry about adding more later. So, basically, 1) do they have to be sorted, or is only the nearest one important? and 2) how many are there?

This is sandbox. Player can add any number of actors. Then, it will be, for example, 10 monsters that must go to these actors (by distance, first to the nearest).
As already be at a distance, for example, “200” then the monster destroy actor, and go to next actor.

I wouldn’t worry about sorting the actors. Instead just find the nearest one each time it you want to move to the next. Create a function that returns just the nearest actor of the type you are interested in. With just a few actors to move to it won’t be too bad to iterate through all of them once, particularly since you only need to call the function when it is time to move.

For example, put this function in the actor you want to move:

Once you get all the actors of the specific class, use a foreach loop to go through them. The Get Distance To node will tell you how far away the other actor is. Just save the nearest actor in a local variable and return it after the loop is done. In my example, it is important that you set Closest Distance to a large number at the start of the function (otherwise it will be 0, and none of the actors will be closer than that). MIN_DISTANCE is also optional, but prevents the function from returning an actor that is too close. From there you can use the Move To on the output of this function.

Hope that helps!

In my example Nearest was of type Actor. For Closest Distance I would set the default to something large, 10,000 or 100,000, just so that you can be certain it will be larger than your nearest actor’s distance.

Hello. Thanks for help me. You are awesome.

Which has a default value closet distance?

Thanks! so much

It looks like you have min and closest distance backwards. Right now it will check if the value is less than min (500) and greater than closest (100000), which will always be false. Check for < Closest and > Min.

Thank you!!

Hey, I used this to make my character rotate to aim at an actor on mouse click, however, it only rotates towards the same target all the time, regardless of not being the closest to the character. Here’s my setup, the first screen is the array of the target actors in the level (in level BP), the second is the rotate setup in me character BP.

Make sure you plug “self” into the other pin on the “Get Distance To” node. That might fix your problem. Let me know if it doesn’t. Right now it looks like it’s trying to find the distance between the target and nothing, which could be a problem.

Hey ZombieKemist,
In the example setup for your answer, what happens if more than one actor is less than the “Closest Distance”? From what I understand of the logic here, you’re telling the loop to get the Distance to the player character (assuming this is where the function is) then you’re checking to see which of the actors of specified class has a Distance To value of less than your “Closest Distance” and then setting that as the “Nearest”. But I’d imagine that this won’t always return the nearest NPC, just the first one that is less than whatever the value you put inside the “Closest Distance” float. Would it not? So if two actors are the same distance away from the player then it might not return the correct one. In my case I’m trying to use this to find the closest NPC and I’m worried if they’re moving around the screen then there might be a chance of two of them being the same distance from the player at one point.

“So if two actors are the same distance away from the player then it might not return the correct one.” If 2 actors are at the same distance → which would be the closest ? if the are at the SAME distance → how do you define which one is a correct actor ?

It has been a while since you asked, but I thought I would answer just in case you’re still wondering.

Most importantly, the chance that two actors are the EXACT same distance from the player character is negligible. In the case that two NPCs are perfectly equidistant, it will return the first one it comes across in the list, because the “Distance < Closest” will return false if Distance == Closest. You could change this to be the last one simply by changing the distance check from a < to a <=, since it would then update for equal values. Again, the chances of two float values being identical is very slim.

If you wanted to be able to separate equidistant actors, you could set up a second layer of comparison (however you wanted). You could check if they are equal, if they are, use the second layer of comparison to determine which you want to pick.

Hope that helps!

I noticed there’s a problem with this setup if you want to resuse it multiple times. If you do, then you need to set the Closest distance back to a large value before you run the function again. Otherwise even if you’re closer to one of the actors, it will return false if you were previously closer to the last one. So make sure you set the closest distance to 100,000 or whatever large value you chose before the function runs.