UWorld* wworld = GEngine->GameViewport->GetWorld();
APawn* Pawn = UGameplayStatics::GetPlayerPawn(wworld, 0);
ACharacter* Character = Cast(Pawn ); //Doesn’t work!
How to cast from Pawn to Character in C++?
UWorld* wworld = GEngine->GameViewport->GetWorld();
APawn* Pawn = UGameplayStatics::GetPlayerPawn(wworld, 0);
ACharacter* Character = Cast(Pawn ); //Doesn’t work!
How to cast from Pawn to Character in C++?
I’m not sure in which Context this is but you should be able to get the Character directly from the PlayerController like so:
APlayerController *playerController = GetController<APlayerController>();
if (playerController)
{
ACharacter *myCharacter = playerController->GetCharacter();
}
If you need to obtain the PlayerController as well because you’re not in a class that already has a reference to it, you can do so using the following:
APlayerController* playerController = GetWorld()->GetFirstPlayerController();
If you’re running a Local Multiplayer, you may need another way obtain the correct PlayerController but that is outside the scope of the question. Hope that helps!
GetOwner() was in the context of my code. Throw whatever object you want in there; if the cast fails you will now have a nullptr. Note that OwnerChar was already declared to be a ACharacter* in the .h file.
Posted as a screenshot because the alligators aren’t showing on answerhub.