Setup Enhanced Inputs using C++

  1. Add “EnhancedInput” module to project build file (.Build.cs):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    // Fill out your copyright notice in the Description page of Project Settings.

    using UnrealBuildTool;

    public class SwordSlash : ModuleRules
    {
    public SwordSlash(ReadOnlyTargetRules Target) : base(Target)
    {
    PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

    PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" });

    PrivateDependencyModuleNames.AddRange(new string[] { });

    // Uncomment if you are using Slate UI
    // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

    // Uncomment if you are using online features
    // PrivateDependencyModuleNames.Add("OnlineSubsystem");

    // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
    }
    }

  2. Close Visual Studio Code
  3. Open Unreal Editor -> Choose “Tools” -> Click “Refresh Visual Studio Project” (This step is crucial! Otherwise it still cannot detect the module update.)
  4. Reopen Visual Studio Code, and then you are ready to use the module “EnhancedInput”

In header file

1
2
3
4
5
6
7
8
protected:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
UInputMappingContext* BirdMappingContext;

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
UInputAction* MoveAction;

void Move(const FInputActionValue& Value);

  1. Set your inputs in BluePrint
    1. Select “BP_[YourCharacter]”
    2. In Details Panel:
      1. Set “Bird Mapping Context” to your mapping context
      2. Set “Move Action” to your move action

In c++ file, under BeginPlay()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// If you find any link error, please refer to step 3
#include "Components/InputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"

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

if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->ClearAllMappings();
Subsystem->AddMappingContext(BirdMappingContext, 0);
}
}
}

void ABird::Move(const FInputActionValue& Value)
{
const bool CurrentValue = Value.Get<bool>();
if (CurrentValue)
{
UE_LOG(LogTemp, Warning, TEXT("IA_Move triggered"));
}
}

// Called to bind function to input
void ABird::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ABird::Move);
}
}

  1. Compile and run project, when you triggers your mapping context, the warning log “IA_Move triggered” should show.