How to get Character in PlayerController

I have a class ATTSCharacter with some functions in it I need to call in my PlayerController.

So I have the following code, it compiles but doesn’t work:

void ATTSPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();

	ATTSCharacter* Character = Cast<ATTSCharacter>(GetControlledPawn());

	GEngine->AddOnScreenDebugMessage(1, 1.0f, FColor::Red, FString::SanitizeFloat(Character->Health));
}

When the code executes it says: Access violation reading location 0x00000000000004F0.

Any ideas?

It mean that GetControlledPawn(), or return diffrent type then cast or returns nulll pointer as controller not possessing pawn at this point. I belive it’s mopt likely 2nd option as you do it in function which name it sounds like it’s executed in componant initiation stage where controller possess nothing, try diffrent function, most safes will be BeginPlay()… or even better Possess():

1 Like

I don’t understand how to implement this Possess() can you help me?