# How to access custom player controller from character class?

**URL:** <https://forums.unrealengine.com/t/how-to-access-custom-player-controller-from-character-class/449106>\
**Category:** C++\
**Tags:** access, inheritance, unreal-engine, question\
**Created:** [May 17, 2019, 9:39am UTC](https://forums.unrealengine.com/t/how-to-access-custom-player-controller-from-character-class/449106 "2019-05-17T09:39:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_W\_Lewis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_W\_Lewis](https://forums.unrealengine.com/u/Jack_W_Lewis)\
**Post date:** [May 17, 2019, 9:39am UTC](https://forums.unrealengine.com/t/how-to-access-custom-player-controller-from-character-class/449106/1 "2019-05-17T09:39:53Z")

</div>

Hi,

How would I access my my custom player controller from my character class? In some peoples code I’ve seen people use something like this CPC-\>GetControlRotation(); for example. Where I assume CPC is the stored name for their CustomPlayerController. But without the rest of their code I can’t see how they actually access the controller.

Thanks,  
Jack

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [May 17, 2019, 10:09am UTC](https://forums.unrealengine.com/t/how-to-access-custom-player-controller-from-character-class/449106/2 "2019-05-17T10:09:03Z")

</div>

GetController() returns controller that currently possessing the pawn, either AI or player controller:

> **[GetController](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/GameFramework/APawn/GetController/)**
>
> Overload list

If pawn is not possesed it will return nullptr, so do a check before starting doing calls to player controller

---

<div class="post-metadata">

**Author:** ![DebashishPatra](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DebashishPatra](https://forums.unrealengine.com/u/DebashishPatra)\
**Post date:** [May 18, 2019, 6:07am UTC](https://forums.unrealengine.com/t/how-to-access-custom-player-controller-from-character-class/449106/3 "2019-05-18T06:07:22Z")

</div>

Here is some code to get you started.

You can simply declare variables in header if you do not want to repeatedly refer the variables.

 ![277197-zzref.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/8/7/d87c07c1d3a4fc0ca36148db43bc1d25cd4ba565.png)

You can initialize the variable like this. As @anonymous_user_f5a50610 mentioned, if your character is **not posessed** by an **AI /Player** , it will return null. As for the following code, the first posessed pawn/character is saved. Following does this in BeginPlay.

 ![277198-zziinits.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/1/d/81d356f069d4851dd48edeef11ab0eb79400b941.png)

The following code uses that reference to do some fancy headbobs. Camera transitions should work similarly. Commented out because, well, headbobs.

 ![277199-zzheadbobs.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/3/5/035616ff0605c4720eb97a5945f837418cbed325.png)

WW\_Player here, is a first person C++ only class, derives from character class and automatically generated by a custom GameModeBase.

---

<div class="post-metadata">

**Author:** ![Jack\_W\_Lewis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_W\_Lewis](https://forums.unrealengine.com/u/Jack_W_Lewis)\
**Post date:** [May 18, 2019, 11:55am UTC](https://forums.unrealengine.com/t/how-to-access-custom-player-controller-from-character-class/449106/4 "2019-05-18T11:55:31Z")

</div>

This seems to get my player controller but not my custom player control. I need to run a function from my custom one from the character class input component.

---

<div class="post-metadata">

**Author:** ![mightyenigma](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mightyenigma](https://forums.unrealengine.com/u/mightyenigma)\
**Post date:** [May 20, 2019, 6:21pm UTC](https://forums.unrealengine.com/t/how-to-access-custom-player-controller-from-character-class/449106/5 "2019-05-20T18:21:29Z")

</div>

Is the game starting up using a GameMode that assigns your custom Player Controller by default? If not, are you explicitly telling to create and assign your custom controller to each player?
