UE Enhanced Inputs
Setup Enhanced Inputs using C++
- 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
}
} - Close Visual Studio Code
- Open Unreal Editor -> Choose “Tools” -> Click “Refresh Visual Studio Project” (This step is crucial! Otherwise it still cannot detect the module update.)
- Reopen Visual Studio Code, and then you are ready to use the module “EnhancedInput”
In header file1
2
3
4
5
6
7
8protected:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
UInputMappingContext* BirdMappingContext;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input)
UInputAction* MoveAction;
void Move(const FInputActionValue& Value);
- Set your inputs in BluePrint
- Select “BP_[YourCharacter]”
- In Details Panel:
- Set “Bird Mapping Context” to your mapping context
- 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
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);
}
}
- Compile and run project, when you triggers your mapping context, the warning log “IA_Move triggered” should show.