Enhanced Input: Action binding with bound parameters

Hey,

I’m looking to bind multiple input actions to a single handler, with a means of knowing the initiating action. With the legacy input component, this was straight forward. The generic BindAction method accepted a delegate, and one could simply bind some indicator of the initiating action as a parameter:


DECLARE_DELEGATE_TwoParams(FActionInputDelegate, int32, EInputEvent);

void ATestCharacter::SomeMultiHandler(const int32 ActionIndex, const EInputEvent InputEvent);

PlayerInputComponent->BindAction<FActionInputDelegate>(ActionName, IE_Pressed, this, &ATestCharacter::SomeMultiHandler, ActionIndex, IE_Pressed);

This doesn’t seem to work in the new “enhanced” system, and no action delegate signatures provide any means of resolving the initiating action. Has anyone had any luck achieving something similar without adding a ton of boilerplate to their code?

3 Likes

Same issue here. Any help would be much appreciated.

I’s appear pretty simple (after 10th attempt :wink: )
Just add any argument you need to the handler, but don’t forget to put const FInputActionValue &value first.

Here is an axample:
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked(player_input_component))
{
EnhancedInputComponent->BindAction(Stalker_Character_Actions.Action_Jump, ETriggerEvent::Triggered, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(Stalker_Character_Actions.Action_Jump, ETriggerEvent::Completed, this, &ACharacter::StopJumping);

	EnhancedInputComponent->BindAction(Stalker_Character_Actions.Action_Move, ETriggerEvent::Triggered, this, &AStalker_Character::On_Action_Move);
	EnhancedInputComponent->BindAction(Stalker_Character_Actions.Action_Move_Crouch, ETriggerEvent::Started, this, &AStalker_Character::On_Action_Move_Mode, ENPC_Movement_Mode::Crouch, true);
	EnhancedInputComponent->BindAction(Stalker_Character_Actions.Action_Move_Crouch, ETriggerEvent::Completed, this, &AStalker_Character::On_Action_Move_Mode, ENPC_Movement_Mode::Crouch, false);
	EnhancedInputComponent->BindAction(Stalker_Character_Actions.Action_Move_Walk, ETriggerEvent::Started, this, &AStalker_Character::On_Action_Move_Mode, ENPC_Movement_Mode::Walk, true);
	EnhancedInputComponent->BindAction(Stalker_Character_Actions.Action_Move_Walk, ETriggerEvent::Completed, this, &AStalker_Character::On_Action_Move_Mode, ENPC_Movement_Mode::Walk, false);
	EnhancedInputComponent->BindAction(Stalker_Character_Actions.Action_Move_Sprint, ETriggerEvent::Started, this, &AStalker_Character::On_Action_Move_Mode, ENPC_Movement_Mode::Sprint, true);
	EnhancedInputComponent->BindAction(Stalker_Character_Actions.Action_Move_Sprint, ETriggerEvent::Completed, this, &AStalker_Character::On_Action_Move_Mode, ENPC_Movement_Mode::Sprint, false);

void AStalker_Character::On_Action_Move_Mode(const FInputActionValue &value, ENPC_Movement_Mode movement_mode, bool is_on)
{
… ; // Here we’re using 1 method for 3 different actions with 2 events each (press and release)

2 Likes

I’m having a devil of a time getting this working myself! When I try the same thing, the compiler complains that I’m providing too many arguments:
|Error|C2660|'UEnhancedInputComponent::BindAction': function does not take 5 arguments
Is this something you ran into during your aforementioned 9 unsuccessful attempts? :sweat_smile: I just want one little payload, dangit!

Note: I am on 4.26, perhaps the feature is just newer than the version I’m using…

In case anyone stumbles across this problem, solution is pretty much simple (as Alexander Semeko described).
Just create a function, where 1st param ist const reference to FInputActionValue, and then add your arguments:

void XXX::FireRMB(const FInputActionValue& Value, const ETriggerEvent TriggerEvent, const bool bEnable, const int ExampleInt)

Then, just bind it:

EnhancedInputComponent->BindAction(FireRMBAction, ETriggerEvent::Completed, this, &ASevenPlayerController::FireRMB, ETriggerEvent::Completed, true, 43);

Watch out! You can mark these arguments as CONST but you CAN’T use references (const reference is used only for 1st mandatory arg).

Edit: Do not forget to use Cast in case your argument is for example int8 → so you do for example (int8) 7 in your binding

3 Likes

What version of the engine?

Does anyone have a clear answer for this ?
I’m trying to bind input actions to abilities using GAS by following this except the way they bind actions doesn’t work for me (they don’t specify which version of UE they are using)
And I’ve tried the way Alexander and tibioc mentionned but I still get an error that says there are no overloaded functions that take more than 4 parameters (although I didn’t quite understand what tibioc meant by “Do not forget to use Cast in case your argument is for example int8 → so you do for example (int8) 7 in your binding”)
I’m using Unreal 5.2.1 if that’s any help

Coming back months later to say that I was just stupid and let rider add consts on functions without thinking which ended up being the reason it didn’t work :sweat_smile:
So the link I shared in my previous reply should work, it worked for me at least.

I lost so much time with that (was trying non-working lambda solution provided by Bing Copilot)…

Your solution is clear, easy and working, huge thx!