Help! Enhanced Input from Player Controller

Has anyone used Enhanced Input in C++ from the player controller to the player? If so, would you mind showing your setup?

I have no problem using standard input but I keep getting nullptrs when setting up the new one from the player controller and all the examples I’ve seen, even the ones in the templates do it directly in the player character, but that doesn’t help for easily swapping characters later.

Thanks :pray:

1 Like

So now it compiles, but I get this when showing the debug info:

It says “No enhanced player input action mappings have been applied to this input”, and the player can’t move.

But I do have these:

image

and then in the player controller bp, they are set.

My code for the player controller is here:

#include "TGCNP_PlayerController.h"
#include "EnhancedInput/Public/EnhancedInputComponent.h"

ATGCNP_PlayerController::ATGCNP_PlayerController()
{

}

void ATGCNP_PlayerController::BeginPlay()
{
	while (Player == nullptr)
		Player = Cast<ATGCNP_Seldin>(GetPawn());

	Possess(Player);
}

void ATGCNP_PlayerController::EnhancedMove(const FInputActionValue& value)
{
	if (Player != nullptr)
		Player->Move(value);
}

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

	// standard inputs
	InputComponent->BindAction("InputUp", IE_Pressed, this, &ATGCNP_PlayerController::InputUp_Pressed);
	InputComponent->BindAction("InputDown", IE_Pressed, this, &ATGCNP_PlayerController::InputDown_Pressed);
	InputComponent->BindAction("InputRight", IE_Pressed, this, &ATGCNP_PlayerController::InputRight_Pressed);
	InputComponent->BindAction("InputLeft", IE_Pressed, this, &ATGCNP_PlayerController::InputLeft_Pressed);
	InputComponent->BindAction("InputUp", IE_Released, this, &ATGCNP_PlayerController::InputUp_Released);
	InputComponent->BindAction("InputDown", IE_Released, this, &ATGCNP_PlayerController::InputDown_Released);
	InputComponent->BindAction("InputRight", IE_Released, this, &ATGCNP_PlayerController::InputRight_Released);
	InputComponent->BindAction("InputLeft", IE_Released, this, &ATGCNP_PlayerController::InputLeft_Released);

	// enhanced inputs
	if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(InputComponent))
	{
		EnhancedInputComponent->BindAction(IAJump, ETriggerEvent::Triggered, this, &ATGCNP_PlayerController::EnhancedJump);
		EnhancedInputComponent->BindAction(IAMove, ETriggerEvent::Triggered, this, &ATGCNP_PlayerController::EnhancedMove);
	}
}

For anyone looking for the solution here’s mine. Don’t forget to add the EhnancedInput module to your build.cs file. For me, I was missing the EnhancedInputSubsystem which I added to the BeginPlay() function.

They key difference between using a pawn/character and the player controller is…

In the character you are using PlayerInputComponent and in the player controller it’s just called InputComponent. Everything else is basically the same. For me, I prefer to have the actions on the character but I pass the input values in from the player controller. This way I can swap out the possessed pawn and everything will still work using one standardized input setup.

I’ll add more to this as I go.

#include "TGCNP_PlayerController.h"
#include "EnhancedInput/Public/EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"

ATGCNP_PlayerController::ATGCNP_PlayerController()
{

}

void ATGCNP_PlayerController::BeginPlay()
{
	while (Player == nullptr)
		Player = Cast<ATGCNP_Seldin>(GetPawn());

	Possess(Player);

	if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
		Subsystem->AddMappingContext(InputMappingContext, 0);
}

void ATGCNP_PlayerController::EnhancedMove(const FInputActionValue& value)
{
	if (Player != nullptr)
		Player->Move(value);
}

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

	// standard inputs
	InputComponent->BindAction("InputUp", IE_Pressed, this, &ATGCNP_PlayerController::InputUp_Pressed);
	InputComponent->BindAction("InputDown", IE_Pressed, this, &ATGCNP_PlayerController::InputDown_Pressed);
	InputComponent->BindAction("InputRight", IE_Pressed, this, &ATGCNP_PlayerController::InputRight_Pressed);
	InputComponent->BindAction("InputLeft", IE_Pressed, this, &ATGCNP_PlayerController::InputLeft_Pressed);
	InputComponent->BindAction("InputUp", IE_Released, this, &ATGCNP_PlayerController::InputUp_Released);
	InputComponent->BindAction("InputDown", IE_Released, this, &ATGCNP_PlayerController::InputDown_Released);
	InputComponent->BindAction("InputRight", IE_Released, this, &ATGCNP_PlayerController::InputRight_Released);
	InputComponent->BindAction("InputLeft", IE_Released, this, &ATGCNP_PlayerController::InputLeft_Released);

	// enhanced inputs
	if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(InputComponent))
	{
		EnhancedInputComponent->BindAction(IAJump, ETriggerEvent::Triggered, this, &ATGCNP_PlayerController::EnhancedJump);
		EnhancedInputComponent->BindAction(IAMove, ETriggerEvent::Triggered, this, &ATGCNP_PlayerController::EnhancedMove);
	}
}

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.