Cast PlayerController c++

Hi, here is the problem :

I have a personal PlayerController (MyPC ) derived from APlayerController. I use it in my map, and it works great.

The question is : how do I do to get it from an other class ?
I tried :

MyPC * pMyPlayerController = Cast<MyPC >(UGameplayStatics::GetPlayerController(GetWorld(), 0));

By the way, pMyPlayerController is NULL.

Any idea ? Thank you.

Hello Daestra,

When are you trying to make this call and how are you going about checking it? Also, are you sure that the PlayerController is being set correctly in your GameMode?

I tried testing by making a custom playercontroller, a custom actor, and a custom gamemode. In the gamemode I set the default playercontroller to the custom one. In the custom actor I made the cast to the playercontroller in beginplay and then used an if check to see if I should print a log statement based on if the casted variable was null or not. This was the syntax for that.

AMyPlayerController * MyPc = Cast<AMyPlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
UE_LOG(LogTemp, Warning, TEXT("It's populated"));

Found the solution, I was calling UGameplayStatics::GetPlayerController(GetWorld(), 0) in an UBject, where GetWrold() is undifined.
It must be called from an actor.

So, Excuse me. How to set the default playercontroller to the custom one ?

Hello microface,

To see an example, create a new C++ project with the Third Person Game template and take a look at the projectnameGameMode.cpp class generated by the template. It’ll show here, on line 13, how to set the default pawn class. You can do a similar thing to set the default playercontroller by using PlayerContollerClass instead of DefaultPawnClass.

PlayerControllerClass = ASWP_WorldPlayerController::StaticClass();

write this in the constructor of your GameMode class. “ASWP_WorldPlayerController” is my custom c++ player controller class