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"

Bind delegate with one parameter

I am trying to bind a delegate with a predefined parameter (payload). I am having a hard time with the syntax, so I'd appreciate some help as I have tried to look up a lot of examples but to no avail. Something along the lines of:

 //Global scope in .h 
 DECLARE_DELEGATE_OneParam(RefreshOne, uint8);

 //Private member variable of the class
 RefreshOne one;
 
 //Inside MyClass constructor
 one.BindRaw(this, &MyClass::MyFunction, 1);

I have tried it with binduobject and others(create*) with no avail. It spits out various errors the different ways I write it. Some help with binding this delegate with a payload so I can use it later (in a timer for example) would be much appreciated.

Product Version: Not Selected
Tags:
more ▼

asked Oct 07 '14 at 03:32 PM in C++ Programming

avatar image

BaderThanBad
664 21 18 177

avatar image BaderThanBad Oct 07 '14 at 04:06 PM

Fiddled around a bit more, this line apparently works for a timerdelegate:

 FTimerDelegate TempDelegate(FTimerDelegate::CreateUObject<MyClass, uint8>(this, &MyClass::MyFunction, 1));
avatar image BaderThanBad Oct 07 '14 at 04:09 PM

I still would like to know the proper syntax for a normal delegate though.

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

2 answers: sort voted first

Hi,

A few problems here. First of all, a delegate definition describes how you want to call the delegate, not the signature of the functions it's bound to. If you want to bind 1 to a function 'void MyFunction(uint8)' (I'm guessing - you didn't specify the definition of your function), it means you want the delegate to always pass 1 as that argument, which means that you pass zero arguments to the call.

Second, the bound variables needs to match the types of the delegate parameters precisely. So even though 1 is convertible to uint8, you need to cast it instead, like (uint8)1, which forces the type to be uint8 and the binding to be recognised.

So, I'm thinking your class wants to look something like this:

 DECLARE_DELEGATE(RefreshOne);
 
 class MyClass
 {
 public:
     MyClass()
     {
         one.BindRaw(this, &MyClass::MyFunction, (uint8)1);
     }
 
     void MyFunction(uint8 Val)
     {
         // Whatever
     }
 
     void Invoke()
     {
         one.Execute(); // Will call this->MyFunction((uint8)1);
     }
 
 private:
     RefreshOne one;
 };

This is a simple example.

More generally, bound variables are applied to the end of delegated calls, so a more complex example might look something like this:

 DECLARE_DELEGATE_TwoParams(Delegate, const TCHAR*, float);
 
 struct MyClass
 {
     MyClass()
     {
         del.BindRaw(this, &MyClass::MyFunction, (uint8)1, 'x');
     }
 
     void MyFunction(const TCHAR* a, float b, uint8 c, char d)
     {
         // Whatever
     }
 
      void Invoke()
      {
          del.Execute(TEXT("Hello"), 3.14f); // Will call this->MyFunction(TEXT("Hello"), 3.14f, (uint8)1, 'x');
      }
 
     Delegate del;
 };

Hope this helps.

Steve

more ▼

answered Oct 15 '14 at 12:07 PM

avatar image

Steve Robb STAFF
2.8k 58 31 101

avatar image BaderThanBad Oct 15 '14 at 01:16 PM

That makes a lot more sense, I mistakenly thought the declaration was the function signature instead of how the delegate is called. Thank you.

avatar image IanBreeg Apr 17 '15 at 09:37 AM

Thanks, very helpful! I wonder why the delegates Wiki page doesn't contain these details. They should really be there.

avatar image jjuszcza Jan 28 '19 at 02:15 PM

The official documentation for Delegates says the exact opposite to this post, and this post is correct while the official documentation is wrong.

Official Unreal Engine Delegates Documentation:

The macro used is determined by the signature of the function(s) to be bound to the delegate

Your post:

First of all, a delegate definition describes how you want to call the delegate, not the signature of the functions it's bound to

Just to be absolutely clear:

The macro used is determined by the signature of the function A delegate definition is not described by the signature of the function

3 hours. 3 hours trying to pass payload data to a template. To a "one parameter delegate".

I get that the macro used is somewhat relevant to the signature of the function, but it's how you want to call the delegate, not the signature of the function it's bound to.

I thought I was going insane.

Also, the documentation on docs.unrealengine incorrectly shows the example

MyDelegate.BindRaw( &MyFunction, true, 20 );

in the Payload Data section. You shouldn't use BindRaw there, you should use BindStatic, which I found was correctly documented in the comments of the Delegate.h class in the actual engine source code.

I hate whoever wrote that page of documentation.

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

It should be like this, DECLARE_DELEGATE will generate RefreshOne variable for you, thats what this macro is for:

 DECLARE_DELEGATE_OneParam(FRefreshOne, uint8)

Now you bind it by doing this:

 RefreshOne.Bind(this, &SomeClass::SomeFunction);

And you trigger the event using

 RefreshOne.Brodcast (1); //2nd paremeter is that uint8 parameter you declered

The delegate you using is monocast delegate, you can set only bind one function, when you bind again it will be replaced, you can declere multicast delegate where you can bind multiple functions using this

 DECLARE_MULTICAST_DELEGATE_OneParam(FRefreshOne, uint8)

and then you bind functions using

 RefreshOne.AddDynamic(this, &SomeClass::SomeFunction);
more ▼

answered Oct 07 '14 at 04:57 PM

avatar image

Shadowriver
37.2k 936 172 1116

avatar image BaderThanBad Oct 07 '14 at 05:04 PM

Im not sure this is the proper usage. Why would you need a delegate if in your broadcast call you specify the function and its parameters? If so why not call it directly.

avatar image Shadowriver Oct 07 '14 at 05:14 PM

Sry another mistake ^^' sry messed you just need parameter

avatar image BaderThanBad Oct 07 '14 at 05:29 PM

Not to wail on you or anything but the RefreshOne isnt an identifier is what my compiler is telling me. As I understand it becomes like a variable type, so you need to make an instance from it is my understanding. Im doing this on 4.4 so im not sure if its different in an update. (Also, there is no Bind function, multiple types but no bind: error C2039: 'Bind' : is not a member of 'TBaseDelegate_OneParam')

avatar image Shadowriver Oct 07 '14 at 05:34 PM

Hmm after looking deeper, try to F prefix to RefreshOne, macro should create varable without F

DECLARE_DELEGATE_OneParam(FRefreshOne, uint8)

Sry that i'm messing up i rarely use delegates in my classes

avatar image BaderThanBad Oct 07 '14 at 05:07 PM

I want to bind a specific instance of a member function, not a static call. I dont see where you specify the instance of the class function too.

avatar image Shadowriver Oct 07 '14 at 05:12 PM

Ah yes forgot that you need to specify a object sorry i will correct that

(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