How to Bind Action with Enhanced Input in the Actor?

Hi,

I created a gizmo that works in the runtime in C++. Right now, I have a problem, the gizmo is inherited from the AActor class. I need a left mouse pressed button to work in the actor. So my question is how to bind the function in the actor? The online tutorial is all about the binding in the character or pawn but there is no such tutorial in actor.

Right now, I try to use the InputComponent in the AActor and cast it to the EnhancedInputComponent then bind the function with it, but it will not trigger the function. In a third-person template for testing and here is the code:

In EnhancedInputActor.h

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

	void Test(FInputActionValue& Value);

In EnhancedInputActor.cpp

void AEnhancedInputActor::BeginPlay()
{
	Super::BeginPlay();
	
	APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();

	if (PlayerController)
	{
		EnableInput(PlayerController);
	}

	if (InputComponent)
	{
		UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(InputComponent);
		EnhancedInputComponent->BindAction(TestAction, ETriggerEvent::Triggered, this, &AEnhancedInputActor::Test);
	}
}

void AEnhancedInputActor::Test(FInputActionValue& Value)
{
	UE_LOG(LogTemp, Warning, TEXT("In Test"));
}

In this code, if I write this way, the “BindAction” will report an error that there is no match function with these parameters. And I have to change this
EnhancedInputComponent->BindAction(TestAction, ETriggerEvent::Triggered, this, FName("Test"));

The action mapping uses the default mapping coming with the template and is added in the third-person C++ file. I create a new Input action in the blueprint and assign that action to the TestAction variable in the actor blueprint and add this action in the default mapping with a button T.

But this is not working when I press the button.

Thanks for your reply.

The error about no matching function is because the FInputActionValue should be const, as in

void AEnhancedInputActor::Test(const FInputActionValue& Value)

Thanks for your reply.

I try to add the const, but still report the error:

The Test function you updated is on the AEnhancedInputActor class, but the variable you are passing called ControlledCharacter is of the AMyProject3Character class. If you want to be calling AEnhancedInputActor::Test() then pass ‘this’ instead of ControlledCharacter

Thanks! I am so dumb for missing those small details. It finally works.

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