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.

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

FTimerDelegate TempDelegate(FTimerDelegate::CreateUObject<MyClass, uint8>(this, &MyClass::MyFunction, 1));

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

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.

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.

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);
1 Like

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

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

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

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

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

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

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

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.

1 Like