How to get location of closest enemy pawn?

Hey, so what do If I have multiple classes? Like more than one type of BP_Enemy?

How would one go about getting the location of the closest pawn within a radius? I need the location to plug into a teleport for a special attack in my game. If someone could give me an example of the nodes to use, that would be great. I imagine I would need an array, but how I would get the location information is a little confusing to me. Also, when I “Get” from my purple array filled with actors, there is an empty plug with a number next to it. I do not know what to plug in there.

you need to make an array that contains all the actors in your radius. there are many ways to accomplish this such as overlaps or get all of class for example. next you need to run a test on each enemy to get its distance from the player. as you do this you also record the closest one as you go along and replace it as needed. by the time you done comparing them all you will have the closest.

below is an example of how this is done. basically you begin with an array of the enemies. then you set the first index as the closest so you have something to compare to later. then you use a for each loop to compare the distance to each enemy and if that enemy is closer than the current closest enemy you set the one being tested as the current closest.

ok now for your last question, on the get node the number you put in represents the index that you want to get from the array. basically if you have a list of things and you number each entry the index is that number. also you mentioned that you have a purple color, that means you are getting a class and not a reference which is something you dont want in this case. for what your looking to do you need a reference to the actors that exist in the scene so your variable should be of type actor reference which will be blue.

2 Likes

if they all inherit from a parent class then then you would use the parent class. if not then you just need a way to populate the array with all the actors you want. it really depends on your setup / circumstance. worst case you populate your array with every character in range then do a check to see if the one being tested is a enemy type.

personally when i create enemies i make a base enemy class then make the different enemy types children of the base. this process makes it so you dont need to make multiple instances of things like health systems and it makes it easier when trying to get any kind of enemy.

How would I attach multiple actors to the array? It only allows one selection.

what do you mean? “attach to array” do you mean inserting actors into the array? or are you talking about the get overlapping actors node?

if your talking about the overlapping actors node then you just need to be looking for a class higher in hierarchy so instead of looking for a specific enemy type you would be looking for character. then you will need to have a check to see if the character at each index is a enemy. you could also just use multiple get overlapping actor nodes then combine the arrays into one array before proceeding.

No I mean, what is the best way to scan through all the actors I have for the shortest distance OR gather all the actors in a collision sphere? You mentioned all my enemies being the child of one class that can be referenced as BP_Enemy. I didn’t do that prior, is there a way to assign them together under one class type after the fact? And what node would I attach to the GET so that it runs through every entry in the array? If I understand correctly, the array is only grabbing the entry at 0.

there is a way to reparent the enemies to inherit from one class but its not really necessary since they all inherit from character class already. if you really want to reparent then the option to do so is in the class settings.

you dont need to attach anything to the get node. the way i have it set up is i use the get to get the actor at index 0 and set that as the closest just as a initialization. basically you need the variable to have a value to start with so when you try to compare the distance from the player to the current closest you dont get errors. imagine you were doing this in real life and you have a list of people and their distance from you. you would start at the begging of the list right. so you would look at the first on the list and go ok they are the closest since they are the only one ive looked at thus far. then you would move on to the next one and ask is this closer then the first one. how can you know if something is closer if you dont have anything to compare to. thats why we use this get and set prior to the loop to give a base value to the variable.

your other issue about going through every index in the array is already take care of via the loop. the for each loop takes each index and does the comparison. well actually the way i set it up it does the comparison for every index except index 0 since we already set that as the current closest beforehand.