x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

BindAction delegate with parameters

I have the same question than this guy: https://answers.unrealengine.com/questions/4332/question-bind-action-passing-parameters.html

is there any update about this?

Product Version: Not Selected
Tags:
more ▼

asked Dec 09 '14 at 04:45 PM in C++ Programming

avatar image

Davixe
184 18 20 34

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

4 answers: sort voted first

link text, Maybe this will help?

more ▼

answered Dec 11 '14 at 04:49 PM

avatar image

BaderThanBad
664 21 18 177

avatar image Davixe Dec 11 '14 at 05:26 PM

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...
 
 }

avatar image BaderThanBad Dec 12 '14 at 02:48 AM

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.)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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);
 }
more ▼

answered Jul 08 '18 at 10:57 AM

avatar image

Unknown1987
31 2 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hey Davixe-

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, https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Delegates/index.html , for more info.

Cheers

Doug Wilson

more ▼

answered Oct 27 '15 at 06:12 PM

avatar image Davixe Oct 27 '15 at 07:36 PM

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);
 }
avatar image Lordmatics Nov 24 '16 at 07:38 PM

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.

avatar image aelroth Nov 30 '16 at 10:14 AM

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)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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 );
 }
more ▼

answered Jun 24 '17 at 06:00 AM

avatar image

Neverender
51 1 7 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question