Download

Need help with binding function pointer to C++ delegate (non-dynamic non-multicast)

I’m having trouble with binding a pointer to a function with a specific signature to a delegate with the same signature.

There is an existing delegate, declared like DECLARE_DELEGATE_TwoParams(DelegateName, VarType1, VarType2). It is already correctly called when necessary, I just can’t bind a function to it.

I need to bind a function declared like void Function(VarType1, VarType2) to it. The function is not a member of the class that is has the delegate and is doing the binding, it comes from another class. So I cannot access it directly in a &Class::Function way, like most examples I’ve been able to find.

Rather, the function has to be a variable, which I assume is a function pointer like void(*CallbackFunction)(VarType1, VarType2), or a TFunction like TFunction<void(VarType1, VarType2)> CallbackFunction.

What should the input variable type be for the function I’m trying to bind, and what is the syntax and the correct bind function ( BindSP / BindRaw / BindUObject etc) to make this work? I assume the function should look roughly like:

 static void GenericBinder(UObject* CallingObject, TFunction<void(VarType1, VarType2)> CallbackFunction)
 {
  // get delegate, store in var called DelegateToBindTo
  DelegateToBindTo.BindRaw(CallingObject, &CallbackFunction);
 }

Anything similar that I’ve tried gives me massive compilation errors. What am I doing wrong? Many thanks in advance.

I mean not sure if this is something

void RegisterTestDelegate(FTestDelegate Delegate);

RegisterTestDelegate(FTestDelegate::CreateUObject(this, &ThisClass::FunctionToCall));

Thank you for the reply, @TheKaosSpectrum! In this case, if I understand correctly, the caller class needs to create a new delegate of the correct type when calling. I’m not sure I need an extra one, as one already exists in the class that does the binding.

To clarify, in my case, there are two separate classes. The first one is CallerClass, which has a Callback member function. The second is AsyncClass, which has the delegate already, which fires when needed. That part works.

CallerClass simply asks the AsyncClass to start some process, and to bind its (CallerClass’s) CallbackFunction to the delegate that fires when the process finishes. So the Caller should be able to tell the Async, “here’s my member function, I want it to fire when your delegate is fired”.