Trouble trying to get my character in c++

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?

A couple of questions:

  1. From where does AIntPlayer derive?

  2. Where is this code?

  3. Do any of those calls (e.g. GetOwner(), Character->GetOwner(), etc…) return non-null before casting? What do they return?

Hey, AIntPlayer derives from ACharacter.

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.

  1. Every of those casts have returned null before casting.

Im thinking of making a function like this:


 AIntPlayer* GetPlayer(AIntPlayer* playerRef);


im also following this tutorial to implement this : Healthbars and Nameplate Widgets with UMG - Tom Looman

Hi @Matgarciaz

You could try:


#include "Kismet/GameplayStatics.h"

AIntPlayer* player = Cast<AIntPlayer>(UGameplayStatics::GetPlayerController(GetWorld(), 0));

Don’t forget to set your **PlayerController **in GameMode.

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


PS: don’t forget to protect your pointers.

both of you, thank you! I was one off i tried get player controller at one point but i never did get pawn! LOL thank you guys so much, it worked!