I am trying to cast an APlayerController to my derived custom PlayerController in the event AGameModeBase::PostLogin(). It’s OK to use traditional c++ type casting, but when I use function Cast(), it returns a null pointer.
void AMyGameMode::PostLogin(APlayerController * NewPlayer) {
auto player1 = (AMyPlayerController*) (NewPlayer);
auto player2 = Cast<AMyPlayerController>(NewPlayer);
}
Did you try to Call any AMyPlayerController specific Function? Because C++ type Casting will not return you a nullptr if it fails and will crash on you if you try todo something with it.
You most likely dont get a AMyPlayerController there. Can you do a quick Log like that:
UE_LOG(LogTemp, Warning, TEXT("MyClass is %s : "), *NewPlayer->GetPathName());
I have solved the problem my self. It seems like I cannot cast a blueprint type player controller to a c++ type player controller. A inheritance tree may be APlayerController ← AMyPlayerController ← MyPlayerController_BP. My Gamemode default is set to MyPlayerController_BP. But as I am trying to cast to AMyPlayerController, I got a nullptr.