[C++] How do you get APlayerController from ACharacter?

I have been looking for resources on obtaining APlayerController from ACharacter. So far, I see a few questions about enabling input, and answers to those questions saying to override SetupInputComponent method.

But I don’t see how to get APlayerController from ACharacter, even though ACharacter should be able to retrieve APlayerController in some way.

Does anyone know how to do this, and if possible, explain it in detail? Thanks in advance.

I haven’t tried this with the default APlayerController, I’m using my own APlayerController(named ATBPlayerController)
and get it like this in the ACharacter class(named ATBCharacter)

.h

ATBPlayerController* player_controller_;

.cpp

void ATBCharacter::BeginPlay()
{
	Super::BeginPlay();
        player_controller_ = Cast<ATBPlayerController>(Controller);
}

this works for me(with a custom PlayerController)

But,
ACharacter also has an Controller variable, this variable should be the controller/playercontroller which possesses the ACharacter, so this should work if u are using the default one.

The controller may change during the game. Instead of adding a new member variable I would add a method like this and call it when needed

ATBPlayerController* GetATBPlayerController() const
{
  return Cast<ATBPlayerController>(Controller);
}

I never thought of casting it to APlayerController. Thanks to both of you.

APlayerController* player_controller = static_cast<APlayerController*>(this->GetController());

//you might want to use dynamic_cast if you are not sure if it's a APlayerController.