In the previous post, we created an event listener which will allow us to select the target by clicking the target actor. https://unreal-mmo-dev.com/2025/04/25/ue5-devlog-81-how-to-add-left-right-event-click-listener-on-actor/
Now, I want to create a new feature, which will allow me to toggle my selected target by simply clicking tab, or any other button of my choice.
I will add an additional feature which will process only the targets visible on my screen.
By default, it can process any target which is spawned.
Let’s get started.
Defining input action
First, we want to define the input action for the next target toggle.
I created a new input action called IA_NextTarget.
You then need to create the Input Mapping Context and mine is called ActionBarActions as you can see from the screenshot.
In that mapping context, this is where you map the NextTarget action to the Tab button – or whatever you like.
Finally, you will need to register this action mapping in your blueprints, you can do this in multiple places or directly from inside the player controller, where the player controller reference will simply be ‘self’.
Add it to your event begin play.
Handling the actor toggle functionality
Now in your PlayerController class, you will be able to start handling the newly registered event for toggling the next target:
The overall function will look like this:
But lets go through the first and perhaps the most important function there, which is GetAllActorsOrdered.
This function requires to Get All Actors Of Class which returns the array of actors. I use the parent class which is used by my players and mobs so that I can toggle between them.
I could for example get actors of player class or mob class instead, if that’s what I wish to toggle instead.
Filtering array of actors to be ones on the screen
Unreal actually provides a really simple function to help with this:
I simply use the return value boolean for this, but you will find that it will include some actors which are slightly off screen. If you want to exclude those too, simply check the screen position co-ordinates to make sure they’re positive.
Sorting the array of actors by their distance
The trouble is, the returned array is not sorted! And UE5 doesn’t provide a simple way for us to sort it either. Doing it manually will be inefficient and ugly.
In order to get a sorted array, I actually leverage Map feature and numeric array sort.
So, first thing I do is start adding all of these actors into a map, where the key is the distance from my player. You can see this happen here, from the screenshot referenced above:
It’s very unlikely for 2 actors to have the exact same distance, but if that’s a possibility, this technique may not be right for you, you’d have to alter the map value to be an array which you’d also need to iterate against, which makes this not a good solution.
After placing all the items to the map where the keys are the locations, we basically want to sort those locations and we can do this more trivially now:
This provides us the Sorted Distance Keys – now we can simply use those to get the items in the sorted order.
That’s what you can see happen right at the end, before the return node.
Now you can iterate over the targets
I will add the function again for Toggle Next Target
So the first thing we did is got the ordered actors list, so now all you need to do is have a target reference (mine kept in actor actions component, current selected target).
If you have this reference, simply go to the next array index. To make the index loop, you simply need to use the mod (%) function. i.e. (current index + 1) % array length = looping index.
If you don’t have a current target, simply select the index 0. In my case, the index 0 will always be current player as that’s included in the find actors by class and the distance will be 0.