Enhanced Input System

Setup

  • Be sure “Enhanced Input” plugins are enabled (Edit->Plugins). If it isn’t activate it and reset editor.
  • Add “Enhanced Input” module to .uproject in VS
"Plugins": [
	{
		"Name": "EnhancedInput",
		"Enabled": true
	}
]
  • Add dependency to .buildcs
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" });
  • Edit→ project settings → Input → Default Classes
    Default Player Input Class: EnhancedPlayerInput
    Default Input Component Class: EnhancedInputComponent
  • Navigate to your project in file explorer
  • Delete the following folders: Saved, Intermediate, and Binaries
  • Right click your uproject file and select “Generate Visual Studio Project Files” (you might have to click “Show more options” before that option is visible)

Input Action & Input Actions Mapping

  • Add Input Action in editor.
  • Add Input Mapping Context and define/add Input Actions to it.

Code & Blueprint

  • Create pointer for Input Actions and mapping
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhanced Input") UInputAction* IA_Interact{};
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhanced Input") UInputMappingContext* IAM_Anna{};
  • Create functions for binding
    void E_Interact(const FInputActionValue& Value);
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "Anna.generated.h"

class UInputAction;
class UInputMappingContext;

UCLASS()
class IRONGATES_API AAnna : public ACharacter{
	GENERATED_BODY()

public:
	AAnna();
	virtual void Tick(float DeltaTime) override;
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

private:
	void E_Move(const FInputActionValue& Value);
	void E_Jump(const FInputActionValue& Value);
	void E_Look(const FInputActionValue& Value);
	void E_Interact(const FInputActionValue& Value);

	//class UCameraComponent* Camera;

protected:
	virtual void BeginPlay() override;

	// Input Actions 
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhanced Input")
	UInputAction* IA_Move{};
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhanced Input")
	UInputAction* IA_Jump{};
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhanced Input")
	UInputAction* IA_Look{};
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhanced Input")
	UInputAction* IA_Interact{};

	// Input Action Mapping 
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhanced Input")
	UInputMappingContext* IAM_Anna{};


	////Spring Arm Component to follow the camera behind the player
	//UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	//class USpringArmComponent* SpringArmComp;

	//Player follow camera
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	class UCameraComponent* CameraComp{};


};
  • Set mapping context at BeginPlay()
  • Bind Input Actions to functions at SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
#include "Anna.h"

#include "InputMappingContext.h"
#include "EnhancedInputSubsystems.h"
#include <EnhancedInputComponent.h>

#include "Camera/CameraComponent.h"

AAnna::AAnna(){
	PrimaryActorTick.bCanEverTick = true;

	CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	CameraComp->SetRelativeLocation(FVector(0, 0, 90));
	CameraComp->SetupAttachment(GetRootComponent());
}

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

	// Get the player controller
	APlayerController* PC = Cast<APlayerController>(GetController());

	// Get the local player subsystem
	UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer());
	
	// Clear out existing mapping, and add our mapping
	Subsystem->ClearAllMappings();
	Subsystem->AddMappingContext(IAM_Anna, 0);
	

	//Add Camera Component
}

void AAnna::Tick(float DeltaTime){
	Super::Tick(DeltaTime);

}


void AAnna::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	// Get the EnhancedInputComponent
	UEnhancedInputComponent* PEI = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);
	if (PEI == nullptr) return;

	// Bind the actions
	PEI->BindAction(IA_Move, ETriggerEvent::Triggered, this, &AAnna::E_Move);
	PEI->BindAction(IA_Jump, ETriggerEvent::Triggered, this, &AAnna::E_Jump);
	PEI->BindAction(IA_Look, ETriggerEvent::Triggered, this, &AAnna::E_Look);
	PEI->BindAction(IA_Interact, ETriggerEvent::Triggered, this, &AAnna::E_Interact);

}



void AAnna::E_Move(const FInputActionValue& Value){
	//if (GEngine) GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Green, FString("Move"));

	if (Controller == nullptr)
		return;
	
	const FVector2D MoveValue = Value.Get<FVector2D>();
	const FRotator MovementRotation(0, Controller->GetControlRotation().Yaw, 0);

	// Forward/Backward direction
	if (MoveValue.Y != 0.f){

		// Get forward vector
		const FVector Direction = GetActorForwardVector();

		AddMovementInput(Direction, MoveValue.Y);
	}

	// Right/Left direction
	if (MoveValue.X != 0.f)
	{
		// Get right vector
		const FVector Direction = MovementRotation.RotateVector(FVector::RightVector);

		AddMovementInput(Direction, MoveValue.X);
	}
	
}

void AAnna::E_Jump(const FInputActionValue& Value)
{
}

void AAnna::E_Look(const FInputActionValue& Value){
	//if (GEngine) GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Green, FString("Look"));

	double X = Value.Get<FVector2D>().X;
	double Y = Value.Get<FVector2D>().Y;

	AddControllerYawInput(X);

	FRotator new_rotation = CameraComp->GetRelativeRotation();
	new_rotation.Pitch = FMath::Clamp(new_rotation.Pitch + Y, -80.f, 80.f);
	CameraComp->SetRelativeRotation(new_rotation);//FRotator(Y,0,0));//
	

}

void AAnna::E_Interact(const FInputActionValue& Value)
{
}
  • Create a Blueprint which inherit C++ class.
  • Set default Input Actions and Mappings.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *