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:
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?
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?
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!