# \[C++\] How do you get APlayerController from ACharacter?

**URL:** <https://forums.unrealengine.com/t/c-how-do-you-get-aplayercontroller-from-acharacter/312092>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 22, 2015, 5:26am UTC](https://forums.unrealengine.com/t/c-how-do-you-get-aplayercontroller-from-acharacter/312092 "2015-03-22T05:26:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asperatology](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@asperatology](https://forums.unrealengine.com/u/asperatology)\
**Post date:** [March 22, 2015, 5:26am UTC](https://forums.unrealengine.com/t/c-how-do-you-get-aplayercontroller-from-acharacter/312092/1 "2015-03-22T05:26:15Z")

</div>

I have been looking for resources on obtaining APlayerController from ACharacter. So far, I see a few questions about enabling input, and answers to those questions saying to override SetupInputComponent method.

But I don’t see how to get APlayerController from ACharacter, even though ACharacter should be able to retrieve APlayerController in some way.

Does anyone know how to do this, and if possible, explain it in detail? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Sleicreider](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sleicreider](https://forums.unrealengine.com/u/Sleicreider)\
**Post date:** [March 22, 2015, 9:47am UTC](https://forums.unrealengine.com/t/c-how-do-you-get-aplayercontroller-from-acharacter/312092/2 "2015-03-22T09:47:17Z")

</div>

I haven’t tried this with the default APlayerController, I’m using my own APlayerController(named ATBPlayerController)  
and get it like this in the ACharacter class(named ATBCharacter)

.h

```
ATBPlayerController* player_controller_;

```

.cpp

```
void ATBCharacter::BeginPlay()
{
	Super::BeginPlay();
        player_controller_ = Cast<ATBPlayerController>(Controller);
}

```

this works for me(with a custom PlayerController)

But,  
ACharacter also has an Controller variable, this variable should be the controller/playercontroller which possesses the ACharacter, so this should work if u are using the default one.

---

<div class="post-metadata">

**Author:** ![marotili](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@marotili](https://forums.unrealengine.com/u/marotili)\
**Post date:** [March 22, 2015, 9:54am UTC](https://forums.unrealengine.com/t/c-how-do-you-get-aplayercontroller-from-acharacter/312092/3 "2015-03-22T09:54:14Z")

</div>

The controller may change during the game. Instead of adding a new member variable I would add a method like this and call it when needed

```
ATBPlayerController* GetATBPlayerController() const
{
  return Cast<ATBPlayerController>(Controller);
}

```

---

<div class="post-metadata">

**Author:** ![asperatology](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@asperatology](https://forums.unrealengine.com/u/asperatology)\
**Post date:** [March 22, 2015, 3:21pm UTC](https://forums.unrealengine.com/t/c-how-do-you-get-aplayercontroller-from-acharacter/312092/4 "2015-03-22T15:21:13Z")

</div>

I never thought of casting it to APlayerController. Thanks to both of you.

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [April 8, 2018, 10:40pm UTC](https://forums.unrealengine.com/t/c-how-do-you-get-aplayercontroller-from-acharacter/312092/5 "2018-04-08T22:40:48Z")

</div>

APlayerController\* player\_controller = static\_cast\<APlayerController\*\>(this-\>GetController());

```
//you might want to use dynamic_cast if you are not sure if it's a APlayerController.
```
