How do I bind a function pointer's function to a delegate?

So I have a dynamic multicast delegate, and a function pointer:

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FEventPublisherEvent);
FEventPublisherEvent myDelegate;

typedef void (UEventPublisher::*FunctionPtrType)(void);
 FunctionPtrType callbackPt = r;

callbackPtr is currently pointing to a function, and I would like to Add() that function to my delegate:

myDelegate.Add(callbackPtr);

That produces an error, "cannot convert argument 1 from ‘UEventPublisher::FunctionPtrType’ to ‘const TScriptDelegate &’ "

I know that means that myDelegate is expecting a weak pointer, and it’s getting my custom type, but where I’ve been stuck for almost two days now is what needs to change for me to hand myDelegate a reference to callbackPtr’s function that it can Add() to itself.

When you try to pass a member function pointer to a dynamic delegate you need to provide it with the class object and use AddDynamic. Try this:

myDelegate.AddDynamic(this, callbackPtr);

I’m assuming you’re trying to add from within UEventPublisher, if not replace this with a pointer to the UEventPublisher you want the function to be called on.

AddDynamic won’t show up with autocomplete as a member function of your delegate. It’s actually a macro which ends up calling __Internal__AddDynamic.

I am 90% sure you’re a mad genius that just solved all of my problems in one fell swoop, thank you!!!

I can make this work entirely within UEventPublisher, but I’m having a bit of trouble allowing external classes to use this method: I have a class UEventSubscriber, whose header has an #include to UEventPublisher, and I’m trying to repeat your logic remotely:

void UEventSubscriber::SubscrEventTest()
{
UEventPublisher::FunctionPtrType callbackPtr;
ACodeProjectCharacter* tempChar = Cast(this->GetOwner());
callbackPtr = &this->TestEvent;
tempChar->EventPublisher.SubscribeEvent(“TestEvent”, callbackPtr);
}

This should work, as it’s using the same syntax, the only difference is that I have to manually scope FunctionPtrType to the class that owns it, but I get an error on the line &this->TestEvent;, " ‘&’ : illegal operation on bound member function expression". Is there any chance that’s super-easy to resolve?

Hmm, that seems to produce its own error,

callbackPtr = &UEventSubscriber::TestEvent;

results in "a value type of "void(UEventSubscriber::*)() cannot be assigned to an entity of type “UvenetPublisher::FunctionPtrType”

That error is because &this->TestEvent isn’t a proper way to get a pointer to a member function. You have to do &UEventSubscriber::TestEvent instead.

Yea, you can’t assign a member function pointer of different types. Think of the class type as an argument which is used for this. UEventSubscriber::TestEvent is a member function of UEventSubscriber while callbackPtr requires a member function pointer of UEventPublisher.

Ooh okay, that makes sense. Hmm… in that case, how would you recommend passing a function reference between classes? Wrap it in a delegate inside EventSubscriber, and pass that to EventPublisher instead of the pointer?

Oh, sure thing! Sorry about that :slight_smile:

Hey! I just thought to let you know that if you question has been answered; Please “Accept” it as answer; It saves time for us moderators and makes sure the questions that need attention are seen to as soon as possible! Thanks!