Just after some rough advice on getting distance between enemies and players, currently i have it so on perception update set the target actor as the one to move towards, my game is going to be local multiplayer with upto 4 players at a time, i am trying to get the ai to get all actors that use a specified interface, and then querying whether they are dead, im just after checking to find the closest actor to set as my focused actor
I did try it like this a little while back but, as you see its overly complicated, and i had a hard time finding out whether the closest player was dead
below is a basic example of how you could find the closest character. in the example i used perceived actors as the potential targets but you could use whatever method you like to create the array of targets. i also did not add in the check for being dead since that could have many meanings / definitions, to check for death you would just add in another check before setting current closest.
on to how it works. the basic idea here is to loop through the list and check to see if the index we are testing is closer to the ai character (self) than the reference stored in the currentTarget variable. ok so first thing we need is a list of potential targets, used ai perception and got percieved actors to do this se we can only target things we see or hear. i then promoted the array to a variable to ensure it remains static while we run our script. next we need to set an initial value for the current closest so we get index 0 and set that as the closest, we do this so we have something valid to compare to in the next step otherwise it will try to get the distance to nothing which will always be the shortest. we are up to the loop now which is the main functionality of the script. the loop takes every index and gets its distance to the aicharacter then does the same with the current target, if the distance to the current index is less then or equal we set the current index actor to be the closest target and move on. there is also a check to ensure that the indexes being tested are not equal to 0, this is since we already set index 0 as the closest so theres no need to test it though this check may not actually be necessary. the last step is to output the closest target when the loop has completed.
I created a custom blueprint node that does just that if you want to see how to do it yourself here is the link to the tutorial:
Basically you feed it any array of actors and it will sort them closest to farthest or vice versa (boolean variable you can select on the node). It outputs a sorted array with the actors and their distance away from the target actor. You can then grab the 0 index of the sorted array and that will be the closest enemy to your player.
First, I never asked anyone to write C++, just copy/paste. Second, many things can be done using “a little BP code”. Doesn’t mean C++ isn’t also a viable and perfectly good solution. Me and @ThompsonN13 frequently respond to the same questions, sometimes we have identical solutions, sometimes slightly different takes, sometimes one of us has a better solution. In this case, I was simply adding an alternative because people do not always post everything they have in mind. The C++ node does offer more functionality that maybe the OP would like to have, maybe not. Regardless your comment stating that the above solution is “correct” is really uncalled for. It presumes the one I offered was not which if you bothered to look at the C++ code you would find that it in fact does the exact same thing (and then some) just in C++ which is most certainly more efficient compared to the same thing attempted using blueprints. That being said, I have no issue with the above answer, it does exactly what the OP asked for. Down votes not really necessary.
I would have to agree with what Nebula says here overall. The person may or may not need the extra bits but having multiple solutions is always a good thing. C++ code is a more performant way to go so it will run quicker and be better optimized and you have more options when coding. I would go with C++ if i knew the language. In the future others may have the same issue as the original poster but be working in c++ so this solution would help them. If we can solve issues for more than just the OP then thats a win in my book.