Download

C++ Delegates, call a blueprint defined user function.

Hello,

Here’s my current issue. I have implemented A Character Class, which has-a AActor. This AActor component communicates with a 3rd party External Chat Server, that I created. Now I can send and receive messages from the server, using the CHAT BOX which i have designed and implemented in Blueprint Widget. Now my problem is how to get the chat to update with the message.

I have the message received but this is in C++. I have created a delegate that works and calls a C++ function, but i need to be able to define a function for the UI blueprint and have it be broadcast-ed to from the C++ delegate, with a FString being passed through. I am just having so many issues getting this to work. The delegate doesn’t pop up in the unreal engine editor.

Thanks in advance for your time.

Images of class code.


Your delegate declaration looks good, if you get a pointer to your AAethorMessage instance actor you should be able to bind the delegate to a blueprint event. The automatically generated function should be called “bind delegate onMessageReceived” or something of that sort, if you just search for the name of the delegate you should find it.

Yeah I’m seeing that now. In my ACharacter I have the AAethorMessager* aethor_messenger, object, and in blueprint I got the event binding finally. However, I need to fill in self, and I’m having a hard time getting an instance to that object variable, source code follows. I apologize if i seem noobish, I have not really used Unreal Engine too much, and im trying to build a big game, for my first project to get experience.

Capture5.PNG
This is what i have now, but I don’t think that customevent is firing:(

Getting an Accessed None Error on Aethor Messenger that may be, because the socket hasn’t completely initialized yet?

Depending on when your UMG widget is created, it may fire the Construct event before your character is initialized. I suggest trying to add a delay node right after the Event Construct node to check if that fixes it. Maybe you just forgot to set the AethorMessanger variable after spawning the actor in C++? Also I would recommend you to implement the chat backend somewhere in a more global scope like the game instance or even the player controller. Because characters can frequently be spawned and destroyed, if you need chat while you are dead and there is not character for example it wouldn’t work :slight_smile:

Oh I got it working. Also in a player controller class? I will keep that noted. Thank you so much.

FString packet should be declared outside the while loop and Broadcast() should be called from outside it as well.

I can definitely move the FString packet outside of the while loop, as that would take a little less computation time. However the broadcast needs to remain in the while loop, as this is a Chat Client, so every time it receives a message it needs to broadcast to Blueprint, to add the message to the chat box. I don’t want to call this function, and broadcast every time the function is called that’s a waste of computation time, and resources.