Trying to get reference to my player character and I keep recieving null. Im making a targeting system and inside of my widget component, im trying to get a value I made for my character to see if im currently targeting. I keep getting null. I’ve tried
AIntPlayer* player = Cast<AIntPlayer>(Character->GetController());
Cast<AIntPlayer>(GetOwner());
Cast<AIntPlayer>(Character->GetOwner());
Cast<AIntPlayer>(GetOwner());
Cast<AIntPlayer>(GetWorld()->GetFirstPlayerController()->GetPawn());
running out of ideas here. What else can I do? What am i not understanding?
the code is inside of my widget, I wanted to check if the character was targeting before I activate the “Target hud”.
So i came up with this code here:
//if player target isnt nullptr and its not this actor and we're targeting. Move on.
if (playerTarget && playerTarget != this && bTargeting) {
//AddElement(playerTarget);
TargetHUD = Cast<ATargetHUD>(GetHUD());
if (TargetHUD) {
TargetHUD->TargetWidget->DrawComponent(playerTarget);
}
else {
UE_LOG(LogTemp, Warning, TEXT("TargetHUD is null"));
}
}
to pass the other actor to my widget. However, if I place an AIntPlayer * playerRef parameter, it still returns nullptr.
Every of those casts have returned null before casting.
Small add to the @SolidSk answer: since you want the character, once you have the controller, you still need the GetPawn(), otherwise you will be casting a controller to a character, which should fail:
AIntPlayer* player = Cast<AIntPlayer>(UGameplayStatics::GetPlayerController(GetWorld(), 0)**->GetPawn()**);