How can I get a reference for the default pawn inside the player controller that controls that pawn?

Hi!

I’m trying to get a reference for my default pawn inside my custom player controller class:

include “MyCameraController.h”
include “MyGameModeBase.h”
include “CameraPawn.h”

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

// Input controller
InputComponent->BindAction(“ZoomIn”, IE_Pressed, this, &AMyCameraController::ZoomIn);
InputComponent->BindAction(“ZoomOut”, IE_Pressed, this, &AMyCameraController::ZoomOut);
InputComponent->BindAxis(“MoveSideways”, this, &AMyCameraController::MoveSideways);
}

void AMyCameraController::ZoomIn()
{
AMyGameModeBase* MyGameMode = GetWorld()->GetAuthGameMode();
ACameraPawn* MyPawn = myGameMode->DefaultPawnClass.GetDefaultObject();
}

void AMyCameraController::ZoomOut()
{
}

void AMyCameraController::MoveSideways(float AxisValue)
{
}

But I don’t know how to do it because the above code doesn’t compile and I don’t know if there is an Unreal’s function to get it.

Thanks.

What errors are you getting?
You’ll need to cast your Pawn:

MyPawn=Cast<ACameraPawn>(myGameMode->DefaultPawnClass.GetDefaultObject())

@RecourseDesign These errors:

Cannot use a value of type “AGameModeBase *” to initialize an entity of type “AMyGameModeBase *”

Cannot use a value of type “APawn *” to initialize an entity of type “ACameraPawn *”

Yes, change the ZoomIn code to:

void AMyCameraController::ZoomIn()
{
  AMyGameModeBase* MyGameMode = Cast<AMyGameModeBase>( GetWorld()->GetAuthGameMode());
  ACameraPawn* MyPawn = Cast<ACameraPawn>(myGameMode->DefaultPawnClass.GetDefaultObject());
}

1 Like