# Get Player Controller from inside Game Instance?

**URL:** <https://forums.unrealengine.com/t/get-player-controller-from-inside-game-instance/479913>\
**Category:** Character & Animation\
**Tags:** playercontroller, gameinstance, unreal-engine, cast, question\
**Created:** [March 8, 2021, 2:08pm UTC](https://forums.unrealengine.com/t/get-player-controller-from-inside-game-instance/479913 "2021-03-08T14:08:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmigel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@schmigel](https://forums.unrealengine.com/u/schmigel)\
**Post date:** [March 8, 2021, 2:08pm UTC](https://forums.unrealengine.com/t/get-player-controller-from-inside-game-instance/479913/1 "2021-03-08T14:08:40Z")

</div>

Hi everyone!  
I’m trying to cast to my Player Controller inside the Init() in the Game Instance.  
The thing it always fails. The PController is APCBase, and it’s already defined as the default controller.

Like this:  
APCBase\* const PC = Cast (UGameplayStatics::GetPlayerController(GetWorld(),0));

I tried using  
UGameplayStatics::GetPlayerController  
GetPrimaryPlayerController  
GetFirstLocalPlayerController

Is there something I’m missing?

---

<div class="post-metadata">

**Author:** ![Fraps2020](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fraps2020/32/460865_2.png) [@Fraps2020](https://forums.unrealengine.com/u/Fraps2020)\
**Post date:** [March 8, 2021, 2:27pm UTC](https://forums.unrealengine.com/t/get-player-controller-from-inside-game-instance/479913/2 "2021-03-08T14:27:38Z")

</div>

Your Game Instance is instantiated before the World and before the Player Controller, that’s probably the reason why – you’re calling something that doesn’t exist yet. You probably have to revisit your logic a bit!

---

<div class="post-metadata">

**Author:** ![schmigel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@schmigel](https://forums.unrealengine.com/u/schmigel)\
**Post date:** [March 8, 2021, 3:25pm UTC](https://forums.unrealengine.com/t/get-player-controller-from-inside-game-instance/479913/3 "2021-03-08T15:25:28Z")

</div>

I found the solution if anyone is trying the same.  
The thing is that World() dont exist in the game instance.

I used  
FCoreUObjectDelegates::PostLoadMapWithWorld.AddUObject(this, &UC0GameInstance::OnPostLoadMap);

This will call OnPostLoadMap after the map Loads. Then we’ll have the World.  
In the OnPostLoadMap function I use:  
APCBase\* const PC = Cast (UGameplayStatics::GetPlayerController(GetWorld(),0));

To cast to my specific Controller Type.

The thing is that it only works on the packaged game.

---

<div class="post-metadata">

**Author:** ![schmigel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@schmigel](https://forums.unrealengine.com/u/schmigel)\
**Post date:** [March 8, 2021, 3:26pm UTC](https://forums.unrealengine.com/t/get-player-controller-from-inside-game-instance/479913/4 "2021-03-08T15:26:25Z")

</div>

Thank you Fraps! I didnt know that before.
