So I have a dynamic multicast delegate, and a function pointer:
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:
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:
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
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:
ACodeProjectCharacter* tempChar = Cast(this->GetOwner());
callbackPtr = &this->TestEvent;
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
Yea, you can’t assign a member function pointer of different types. Think of the class type as an argument which is used for
UEventSubscriber::TestEvent is a member function of
UEventSubscriber while callbackPtr requires a member function pointer of
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
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!