Selecting a pawn to be a target (BP)

I’m making a turret fir a tower defense game, How do i get a reference to a pawn so as to put it inside a pawn variable?

this is the code so far:


what needs to go before the SET (in the bottom left) in order to get the turret to select a pawn inside the level?

images would be really helpful as I’m still new to UE4, Thanks!

if you right click your blueprint area, start typing ‘get all actors of class’ and use the pawn class (or your target class). this will output a list of pawns in the map. if this is single player game with only one pawn, drag out the pin with the array symbol, and type ‘get’ to grab the pawn at index zero. you will need to do this at ‘beginplay’ so it initializes when the game starts. from there you can reference that pawn. if you have more than one you need to detect, there are alternative strategies to accomplish this.

I’ve set it so that an array will be created on begin play which also sets the target variable to the pawn at index 0. but won’t the array need to be updated at some point? Whats the best way of handling this?

if you want to work with multiple object detection, there are better ways, and my first suggestion only partially works. here’s an example of a better approach:

if you create a function ‘DetectEnemy’, and in that function use the ‘get all actors of class node’, and then drag out the array, and call ‘foreeachloop’, that way you can iterate through the results and do a range check to establish priority. so for each pawn in that list, get the array object location, subtract the turret location, and get the vector length, if the length is less than your turret range, set your target to that array object (and potentially break the loop if you desire…there’s a foreachloopwithbreak node as well). the problem with this function, is that if you put it in tick, you’re iterating in tick, which is never a good thing. so on beginplay, start a new timer, and call your ‘DetectEnemy’ function to loop every second or half second or maybe even faster depending on performance requirements. if this doesn’t make sense, i’ll try and put a screenshot together.

I got most of that except what comes after the foreachloopwithbreak what nodes do I need the loop to link to and how? thanks for your answer by the way!

See image

Eventually, you’ll want to actually build a list of all valid candidates within range and then apply some additional logic to determine which one to pick…either the closest, or most threatening, or however you want your turret to choose.

Cheers this is a great help!

great! glad it worked out. be sure to mark this thread as answered so others can see that it has been resolved for their reference as well (and for some karma :stuck_out_tongue: )