Find nearest Actor. Why this doesnt work?

Hi all,

Im trying to find an easy way to search for the nearest actor to the player.

I thought this would work but it make the editor crash and I want to know why.

The idea is to pick an array of actors and compare their positions with the objetive actor, build a map (key: float distance, value: actor), ask for the smallest value of the distances array and then pick that actor as a value of the map. Im calling this in the begin overlap event.

The problem is this makes the engine crash, any idea why, and how to make this easy way?

Actually the solution of BRGCooper and mine work!

Hello, I have a question. Do you have the log file or know what crash exception you are getting? It’s possible we could see if something is happening with the loop or a null exception and go from there.

If the keymap is giving issues, then you could simply just get the current object in the array’s distance, save it and it’s current location to a local variable in that function if the distance value is lower (Let’s name them ClosestActor and ClosestDistance). Once the for loop is done, you then return those variable’s values. For the first case we can just check if out array index is 0 to set the ClosestDistance.

I have an example of what that may look like as well.

Hope that helps!

2 Likes

Thank you for answering!

I cant show here the crash report cause now its working just fine, no crashing, expected behaviuor.

I cant belive it.

Your way was actually the same but without fancy data structures, I will keep that in mind for next steps!

Thank you again.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.