I have the same question than this guy: [Question] BIND_ACTION: passing parameters? - Programming & Scripting - Unreal Engine Forums
is there any update about this?
I have the same question than this guy: [Question] BIND_ACTION: passing parameters? - Programming & Scripting - Unreal Engine Forums
is there any update about this?
link text, Maybe this will help?
To clarify, this is what I actually have:
InputComponent->BindAction( "Ability1", IE_Pressed, this, &AConceptCharacter::AbilityOneInUse );
InputComponent->BindAction( "Ability2", IE_Pressed, this, &AConceptCharacter::AbilityTwoInUse );
InputComponent->BindAction( "Ability3", IE_Pressed, this, &AConceptCharacter::AbilityThreeInUse );
InputComponent->BindAction( "Ability4", IE_Pressed, this, &AConceptCharacter::AbilityFourInUse );
And I got 4 functions which have the same implementation, but with different parameters, so I want to pass parameters to use one generic function like this:
void AConceptCharacter::AbilityInUse( int32 NumberOfAbility ) {
//implementation...
}
From what I understand I think I might have an easier solution. Most likely you will do some sort of switch-case or if checks based on the number you are passing to determine which button is pressed. Instead, bind multiple buttons to the same Bind action function, and inside your function do your checking to see which button was pressed.
(I’m not for certain since I’ll be away for a few days, but that post does seem old and i’m certain that if you create an actual delegate like in the link you’d be able to bind it to the inputcomponent, if push comes to shove just have functions that call the generic function with the desired parameters bound to the different buttons.)
Hey -
Generally, to create a delegate with a parameter you would include the parameter in the declaration as such: DECLARE_DELEGATE_OneParam( DelegateName, Param1Type )
And create a variable for the delegate normally: DelegateName MyDelegate;
At this point you can bind the function to call when the delegate is triggered: MyDelegate.Bind(this, &Class::FunctionToBind);
Assuming the function ReturnType FunctionToBind(Param1Type){//some code}
you can make this call from the delegate with MyDelegate.Execute(ParamValue);
The link in BaderThanBad’s answer provides an example of setting up a delegate with a parameter. You can also check the documentation for delegates, Delegates and Lamba Functions in Unreal Engine | Unreal Engine 5.1 Documentation , for more info.
Cheers
Yeah… but I couldn’t use that solution with the “BindAction” function from the input component.
Instead, what I did was use template parameters like this:
InputComponent->BindAction("Ability1", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<1>);
InputComponent->BindAction("Ability2", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<2>);
InputComponent->BindAction("Ability3", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<3>);
InputComponent->BindAction("Ability4", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<4>);
InputComponent->BindAction("Ability5", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<5>);
and then:
void AConceptCharacter::AbilityTestInUse(int32 index) {
// do stuff
}
template<int32 Index>
void AConceptCharacter::AbilityTestInUse() {
AbilityTestInUse(Index);
}
Just curious, with the template approach, what did your headers look like.
Cant seem to get this to compile.
From what i can see template and UFUNCTION() are incompatible.
Hi Lordmatics,
I had the same problem and that’s what i did to resolve it:
.h
void AbilityTestInUse(int32 index);
template<int32 Index>
void AbilityTestInUse()
{
AbilityTestInUse(Index);
}
.cpp
InputComponent->BindAction("Ability1", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<1>);
InputComponent->BindAction("Ability2", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<2>);
InputComponent->BindAction("Ability3", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<3>);
InputComponent->BindAction("Ability4", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<4>);
InputComponent->BindAction("Ability5", IE_Pressed, MyCharacter, &AConceptCharacter::AbilityTestInUse<5>);
void AConceptCharacter::AbilityTestInUse(int32 index) {
// do stuff
}
It’s because templates are always declared in header (if not, you cannot compile)
I just went through this, including temporarily using an integer template argument wrapper function that was uncannily similar to your comments on the other answer. However, I eventually arrived at this, which I think is pretty elegant:
for ( int i = 0; i < kMaxNumAbilities; ++i ) {
// Concatenate the "Ability[0...N]" name.
std::string ActionName( "Ability" );
ActionName += std::to_string( i );
// Bind the press event.
FInputActionBinding PressedAB( ActionName.c_str(), IE_Pressed );
PressedAB.ActionDelegate.GetDelegateForManualSet().BindLambda(
[this, i]() { AbilityPressed( i ); }
);
InInputComponent->AddActionBinding( PressedAB );
// Bind the release event.
FInputActionBinding ReleasedAB( ActionName.c_str(), IE_Released );
ReleasedAB.ActionDelegate.GetDelegateForManualSet().BindLambda(
[this, i]() { AbilityReleased( i ); }
);
InInputComponent->AddActionBinding( ReleasedAB );
}
As of Unreal 4.18 an templated BindAction<>() was added.
To use it:
class AMyCharacter : public ACharacter
{
// ...
void Foo(int32 idx);
DECLARE_DELEGATE_OneParam(FFooDelegate, int32);
};
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInput)
{
// ...
PlayerInput->BindAction<FFooDelegate>("Foo", IE_Pressed, this, &AMyCharacter::Foo, 42);
}