Can I use BindAction with lambda?

Why it’s not possible wirte this:

InputComponent->BindAction( "Grow", IE_Pressed, [ this ]()
{
    isGrowing = true;
} );

I’ve to write something like:

InputComponent->BindAction( "Grow", IE_Pressed, this, &AMyPawn::OnStartGrowing );

Thanks in advice!

1 Like

Too my knowledge, and I could be wrong; you are only able to use lambda on certain delegates, and only as a static function pointer. I think it would be good for BindAction, but the devs would need to sort that out, or create a class inheriting from UInputComponent and overriding BindAction.

Not using BindAction per se, but if you look at how BindAction is implemented, you can use the same tools to do what you’re asking:

FInputActionBinding GrowPressedAB( "Grow", IE_Pressed );

GrowPressedAB.ActionDelegate.GetDelegateForManualSet().BindLambda( [this]()
{
	isGrowing = true;
});

InputComponent->AddActionBinding( GrowPressedAB );
3 Likes

Been a while since you left this but I just needed to say thank you because this helped solve my exact problem. Gratitude.

This doesn’t seem to be possible for the Enhanced Input, anymore.

FEnhancedInputActionEventDelegateBinding<void()> Binding(Action, ETriggerEvent::Triggered); // doesn't accept FName
Binding.Delegate. ??? // doesn't have `GetDelegateForManualSet`
Cast<UEnhancedInputComponent>(InputComponent)-> ??? // doesn't have a way to add `FEnhancedInputActionEventDelegateBinding`
1 Like

i switched to templated functions now, i.e. non-type template parameters, to a similar effect.

1 Like

this would work, but doesnt account for the InputActionValue that is passed from the InputComponent

EDIT: im dumb. this does work perfectly. to get the FInputActionValue passed, all you have to do is ensure the template function is inside the CPP file AND have it as a parameter within the function. the BindAction will automatically pass the FInputActionValue if present

1 Like

@rubm123 Thank you!

1 Like