Movement in Player Controller

I recently started translating my Blueprints to C++ code to help with performance, and I’ve been stuck on this problem for a while: I used my Blueprint Player Controller to manage input, as I wanted input to be common to all player characters, but I didn’t want to bog down the character itself with the input management as the character could also be possessed by an AI controller. I tried to port that over in C++ using the SetupInputComponent, like this:

void APlayerUnitController::SetupInputComponent()
{
    if (InputComponent)
 {
	InputComponent->BindAxis("MovementY", this, &APlayerUnitController::MoveY);
	InputComponent->BindAxis("MovementX", this, &APlayerUnitController::MoveX);
 }
}

void APlayerUnitController::MoveY(float AxisValue)
{
    if (PlayerRef == NULL)
    {
	PlayerRef = Cast<APlayerUnit>(GetPawn());
    }
    else if((PlayerRef->bIsMovementEnabled) && (AxisValue != 0.0f))
    {
	const FRotator Rotation = ControlRotation;
	const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
	const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	//Stops AxisValue from going above 1 in either direction, which would speed up our character
	AxisValue = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
	PlayerRef->AddMovementInput(Direction, AxisValue);
    }
}

void APlayerUnitController::MoveX(float AxisValue)
{
    if (PlayerRef == NULL)
    {
	PlayerRef = Cast<APlayerUnit>(GetPawn());
    }
    else if ((PlayerRef->bIsMovementEnabled) && (AxisValue != 0.0f))
    {
	const FRotator Rotation = ControlRotation;
	const FRotator YawRotation(0.0f, Rotation.Yaw, 0.0f);
	const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
	//Stops AxisValue from going above 1 in either direction, which would speed up our character
	AxisValue = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
	PlayerRef->AddMovementInput(Direction, AxisValue);
        }
}

But so far nothing works, and I’m not sure what I’m doing wrong, seeing as most of it is code from the engine’s Third Person C++ template, adapted to a Player Controller. If anybody can tell me or provide some other way to make it work, I would be very grateful.

Add a call to Super::SetupInputComponent before your binding actions in SetupInputComponent; or pull the functionality from APlayerController::SetupInputComponent into your overriding method. Creation of the InputComponent occurs there, so your InputComponent likely is never created, thus the if(InputComponent) check never passes.

See: Runtime/Engine/Private/PlayerController.h
lines 2242 - 2256

The input component is created at line 2247

Oh…wow, I feel dumb! I can’t believe I forgot to do that, that made it work. Thanks a lot!