Get Enhanced Input Action Value in C++

Hello

I am looking for a c++ equivalent for this node
image

Hi,
I was facing the same problem too for about 4 hours. Here is the solution that worked for me so hope it helps.

Example:

To get the value for any input, you can bind to it using the “BindActionValue” function in the EnhancedInputComponent class. The function returns a reference to FEnhancedInputActionValueBinding.

// .h
public:
    UPROPERTY(Category="Input",  EditDefaultsOnly)
    UInputAction* IAMoveAction; //An FVector2D
private:
    struct FEnhancedInputActionValueBinding* MoveActionBinding;

That’s why the “&” in = &EnhancedInputComponent is important to store the reference to the binding and prevent always returning 0.

// .cpp
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"

EnhancedInputComponent->BindAction(IAMoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::Move);

MoveActionBinding = &EnhancedInputComponent->BindActionValue(MoveAction);

You can then use the function GetValue() to get the CurrentValue of the input which returns an FInputActionValue.

Finally, to get the actual value, use the Get(). For example, if it’s a bool, then Get<bool>(), for FVector2D, we use Get<FVector2D>(), etc…
example:

const FVector2D InputValue = MoveActionBinding->GetValue().Get<FVector2D>();
GEngine->AddOnScreenDebugMessage(-1, 0.f, FColor::Yellow, InputValue .ToString());
11 Likes

I haven’t tried it yet, but I found something like this :

if (UEnhancedInputComponent* PlayerEnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
{
    if (_moveInputAction)
    {
        auto moveInput = PlayerEnhancedInputComponent->BindActionValue(_moveInputAction.Get()).GetValue().Get<FVector2D>(); 
    }
}

So first setup the binding

MoveActionBinding = EnhancedInputComponent->BindActionValue(MoveAction);

where

FEnhancedInputActionValueBinding MoveActionBinding;

is declared in the header file
Then you can get the value by doing :

UEnhancedInputComponent* PlayerInputComponent = Cast(InputComponent);
if(PlayerInputComponent)
{
return PlayerInputComponent->GetBoundActionValue(MoveAction);
}

here is what you want:

FEnhancedInputActionValueBinding MoveActionBinding = EnhancedInputComponent->BindActionValue(IA_Move);
	const FVector2D MoveInputVector = MoveActionBinding.GetValue().Get<FVector2D>();
	
	float MoveInputLength = UKismetMathLibrary::VSize2D(MoveInputVector);

	
	FEnhancedInputActionValueBinding WorldSpaceMoveActionBinding = EnhancedInputComponent->BindActionValue(IA_Move_WorldSpace);
	const FVector2D WorldSpaceMoveInput = WorldSpaceMoveActionBinding.GetValue().Get<FVector2D>();