UE 5, Enhanced Input C++ problems

Okay, so!
I am currently learning UE5, and as a C++ programmer, I am only using C++.
After creating both the Character and the Controller, I checked with my senior that the code was right, yet, when I looked for this section

in the BP_MyCharacter I created, I could not find it (as a matter of fact, I only see the voices

  • Block Input

  • Auto Receive Input

*Input Priority

Does anyone know why it doesn’t show up?

Thank you to those who might want to help me out!

Are the missing UInputAction parameters marked as

UPROPERTY(EditAnywhere,BlueprintReadWrite)
UInputAction * testInputAction;  // (example input action)

Are the all below public?

public:

UPROPERTY(EditAnywhere,BlueprintReadWrite)
UInputAction * testInputAction;  // (example input action)

Did you initialize them in the CDO?
example:

AMyClass::AMyclass()
{
testInputAction = CreateDefaultSubobject<UInputAction> (TEXT("Test input action"));  

};

1 Like

Yes, everything is there. I texted the code and imported it on my coworker’s PC and it works just fine, on mine, however, some things aren’t showing up :confused:

I can share the code, if you prefer?

Sure I can compile it and see what shows up.

Apology for my late reply and thank you for your patience.
Okay, so: my task was to create basic WASD actions for my character using only the PlayerController.

  1. First task was to create them with a basic Input, the deprecated system. Using that one, all commands work just fine. My Character can move without any problem. This is the main code in my Controller.
void AMyCharacterController::BeginPlay()
{
	Super::BeginPlay();

	// get the enhanced input subsystem
	if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
	{
		// add the mapping context so we get controls
		Subsystem->AddMappingContext(InputMappingContext, 0);

		UE_LOG(LogTemp, Warning, TEXT("BeginPlay"));
	}
}

void AMyCharacterController::SetupInputComponent(UInputComponent* InputComponent)
{
	Super::SetupInputComponent();
	
	InputComponent->BindAxis("MoveForward", this, &AMyCharacterController::MoveForward);
}

void AMyCharacterController::MoveForward(float Value)
{
	APawn* MyPawn = GetPawn();

	if (MyPawn)
	{
		const FVector ForwardVector = GetControlRotation().Vector();
		// Add movement in the forward direction
		MyPawn->AddMovementInput(ForwardVector, Value);
	}
}
  1. Second task was to create all the commands all over again using the Enhanced Input System. Alas, I really can’t seem to understand the logic behind it to implement it the same way.

My Controller.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyCharacterController.h"

#include "Components/InputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "InputActionValue.h"
#include "Engine/LocalPlayer.h"
#include "EnhancedInputComponent.h"






void AMyCharacterController::BeginPlay()
{
	Super::BeginPlay();

	// get the enhanced input subsystem
	if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
	{
		// add the mapping context so we get controls
		Subsystem->AddMappingContext(InputMappingContext, 0);

		UE_LOG(LogTemp, Warning, TEXT("BeginPlay"));
	}
}

void AMyCharacterController::SetupInputComponent(UInputComponent* InputComponent)
{
	
	// Bind functions to input
	if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent)) 
	{
		EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Started, this, &AMyCharacterController::MoveForward);
	}

	//InputComponent->BindAxis("MoveForward", this, &AMyCharacterController::MoveForward);
}

void AMyCharacterController::MoveForward(const FInputActionValue& Value)
{
	const FVector2D MovementVector = Value.Get<FVector2D>();

	const FVector Forward = GetActorForwardVector();
	AddMovementInput(Forward, MovementVector.Y);
	const FVector Right = GetActorForwardVector();
	AddMovementInput(Right, MovementVector.X);

	//APawn* MyPawn = GetPawn();

	//if (MyPawn)
	//{
	//	const FVector ForwardVector = GetControlRotation().Vector();
	//	// Add movement in the forward direction
	//	MyPawn->AddMovementInput(ForwardVector, Value);
	//}
}

And My Controller.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MyCharacterController.generated.h"


class UInputMappingContext;
class UInputAction;
struct FInputActionValue;

/**
 * 
 */
UCLASS()
class MYPROJECT2_API AMyCharacterController : public APlayerController
{
	GENERATED_BODY()


	UPROPERTY(EditAnywhere, Category = "EnhancedInput")
	UInputMappingContext* InputMappingContext;

	UPROPERTY(EditAnywhere, Category = "EnhancedInput", meta = (AllowPrivateAccess = "true"))
	UInputAction* MovementAction;

protected:

	/** Input Mapping Context to be used for player input */


	virtual void SetupInputComponent(UInputComponent* InputComponent);

	virtual void BeginPlay() override;

	// Input functions
	void MoveForward(const FInputActionValue& Value);

	// End Actor interface
	
};

All the tutorial I followed don’t work . I am probably missing something, but I don’t understand what…

Why not just create a third person example with c++? It basically already has all of the needed functions with enhanced input (movement, camera movement) input mapping setup etc.

No need to reinvent the wheel at this point :wink:

I know, but the task they gave me was to do just that, as a learning part.

I think I will try to look further into the documentation.

Okay, so.
The solution to this is easier than what I expected.

If I want to to the binding in my Custom Controller (which you migth want if you wish to use the controller for different things), first, in Begin Play, you must cast a LocalPlayer controller, check for the input system. If the Input System exists, add the mapping contest.

Next, do the binding in the SetupInputController (mind the override, be sure to put the Super).

Now, fo the part I was missing:

In the movement function, since you’re not in the Character class, you must tell the function to apply the movements to the current Pawn.

It was easier that expected.

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