I want a reference to my playerclass for my hud. But the player reference keeps to be null.
world = GetWorld();//getting the world
if (world){ con = Cast<APlayerController>(world->GetFirstPlayerController());//getting the controller
UE_LOG(LogTemp, Warning,TEXT("WORLD")); }//This works fine
if (con){ player = Cast<APlayerCharacter>(con->GetCharacter());//planing to get the playerclass
UE_LOG(LogTemp, Warning, TEXT("CON")); }//this works fine too
if (player){ UE_LOG(LogTemp, Warning, TEXT("Player")); }//but this didnt got called...why ?
Where is this code being executed?
AOvergrownHUD::AOvergrownHUD(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
init();
}
void AOvergrownHUD::init()
{
world = GetWorld();
if (world){ con = Cast<APlayerController>(world->GetFirstPlayerController()); UE_LOG(LogTemp, Warning, TEXT("WORLD")); }
if (con){ player = Cast<APlayerCharacter>(con->GetCharacter()); UE_LOG(LogTemp, Warning, TEXT("CON")); }
if (player){ UE_LOG(LogTemp, Warning, TEXT("Player")); }
}
Override BeginPlay or PostInitComponents and call your unit function inside of that rather than the constructor.
he still dosent get the player class but thanks for awnsering
It should. Did you remove the init from the constructor?
i put everything in BeginPlay
GetCharacter isn’t a member of APlayerController, is it something you’ve added? What is GetCharacter returning? Positive it’s been set?
no its from APlayerController. And yes its returning true
You’re tryng to cast whatever GetCharacter is to a player controller. I would imagine GetCharacter returns ACharacter and not APlayerController which is why player is always false.
i tried : con->GetPawn(),con->GetCharacter(),con->GetControlledPawn() and con. Nothing works
Right, because none of those are APlayerController, they’re all APawn or derivative of it
Is APlayerCharacter something you’ve defined? What is the super class of it?
Yes this is my main player class. Its parent is Character. Thats why i tried GetCharacter().
GetPawn should do it then assuming the APawn has been possessed.
If not try PostInitComponents rather than BeginPlay.
Positive that what you’re spawning is of that type? That the game mode is spawning your player character type?
If you cast to APawn or ACharacter what happens?