If you want to bind a function to another object then the only thing you can do is use delegates.
Here’s an example.
// .h of UNetworkObject
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FHTTPOnResponseRecievedDelegate, const FString&, HttpData);
class UNetworkObject
{
FHTTPOnResponseRecievedDelegate HTTPOnResponseRecievedBinder; // custom name
}
// .cpp of UNetworkObject
void UNetworkObject::FunctionThatSendsTheResponse
{
// calls all binded function
HTTPOnResponseRecievedBinder.Broadcast("Response");
}
//------------------------------------------------
// .h of UGameAccountObject
class UGameAccountObject
{
UFUNCTION() // necessary
AccountLoginComplete(const FString& HttpData);
}
// .cpp of UGameAccountObject
void BeginPlay() // or Constructor
{
UNetworkObject* Obj;
Obj->HTTPOnResponseRecievedBinder.AddDynamic(this, &UGameAccountObject::AccountLoginComplete); // binded function must have the same signature as the binder declaration
}
Thank you so much for the response. I edited my response a few minutes ago with the delegate implementation. I am now getting ‘expression must have pointer type’.
Now this is the final (and very dumb) question. On compile, I receive
uninitialized local variable 'netObj' used
How do I initialize it properly? I swear I am going to spend the night reading C++ pointer tutorials after this. I haven’t done much work with pointers since high school.
Thank you so much!!! I was able to compile with ‘new UNetworkObject’ but its really good to know to use NewObject. I’ve made the changes and I actually understand delegates now. I appreciate the help more than you can imagine
This is great example, thank you! I have a question since I kinda have modified version of this setup. In my case call to Broadcast method is not happening in implementation file of class where callback is defined as property (UNetworkObject.cpp in your case). I was making API play nicely with Blueprint objects and moment in which I am supposed to ping callbacks is in some other class’ .cpp file. How I ping all callbacks is like this:
And this actually works pretty well from Blueprint. However, when I do what you did (create UNetworkObject-like object from C++, code from above is not pinging delegate inside of that object.
I assume that if I manage to find a way to drag reference of object which contains the delegate to place where I wanna call Broadcast method on it will do the job, but that would right now make my code really ugly. So wanted to check if you maybe know if there’s a way for me to locate all instances of class which contains delegates (made in C++) and to ping them all?