Cast() not working

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);
}

in this case, player 2 goes to a null pointer.

auto player1 = Cast(NewPlayer);

Thats the right way =)

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());

Edit: Sry used the Protected Function :smiley: fixed now

[2017.05.24-02.39.22:407][ 88]LogTemp:Warning: MyClass is /Game/UEDPIE_1_BP_Main.BP_Main:PersistentLevel.PlayerController_1 :

It seems like the original new player is successfully added.

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.

Nope your wrong you do something else there. Works perfectly on my side: