# How to use mouse click to change current pawn under control?

**URL:** <https://forums.unrealengine.com/t/how-to-use-mouse-click-to-change-current-pawn-under-control/26607>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [April 6, 2015, 2:25pm UTC](https://forums.unrealengine.com/t/how-to-use-mouse-click-to-change-current-pawn-under-control/26607 "2015-04-06T14:25:38Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![anonymous\_user\_6d3a5d00](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_6d3a5d00](https://forums.unrealengine.com/u/anonymous_user_6d3a5d00)\
**Post date:** [April 6, 2015, 4:35pm UTC](https://forums.unrealengine.com/t/how-to-use-mouse-click-to-change-current-pawn-under-control/26607/5 "2015-04-06T16:35:30Z")

</div>

Actor iterating is very expensive, you only really want to do it during a BeginPlay() or similar. This is because most things you place in the world are based off AActor, so a typical map has thousands of actors.

In the case of an RTS, where most units are spawned dynamically, I would create a pointer TArray\<APawn\> per player of their units at map start, and then every time you spawn a unit, add it to this list of pointers. This way you have your dynamic list of pawns of only units, and never have to iterate through the world. TArray also has some very nice features for iterating through the array quickly.

This would leave you with a TArray of 100 units, and one TArray per player. Since each of the indexes of the array is a pointer to a pawn, you should have all base functionality as if each one was the pawn, so you can do all of your AI logic, Movement, and other things using a base PlayerUnits[index]-\> call.

More info on TArrays and Dynamic Arrays in general is here:

> **[TArray: Arrays in Unreal Engine](https://docs.unrealengine.com/5.1/en-US/array-containers-in-unreal-engine/)**

> **[UE4 Libraries You Should Know About](https://www.unrealengine.com/de/blog/ue4-libraries-you-should-know-about)**
>
> UE4 has plenty of great C++ libraries to use for game development, but it’s not always obvious in a large code project where to find them. In this post, I just want to call out a few that are especially useful and worth checking out. For more...

> **[TArray](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Core/Containers/TArray/)**
>
> Templated dynamic array

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

---

_[View the full topic](https://forums.unrealengine.com/t/how-to-use-mouse-click-to-change-current-pawn-under-control/26607)._
