How do I correctly implement delegates to call in lambda functions with websockets

Hello All.

I have a websocket implementation on one of my projects. so ive got the socketgameinstance.h and and socketgameinstance.cpp

in socketgameinstance.cpp I create all the custom websocket events as I need to.
(header definitions)

(cpp on init())

Then I have a global variable I want to set to that messagestring so I can anlayse that json payload and update my variables accordingly based on the message.

so all i want to do is call the playermatched function in the onmessage event from the websocket. so everytime a message is received. That function gets called and the parementer gets passed.

I feel like im going in circles at this point, I am still learning. Any guidance would be greatly apreciated. thank you in advance!

So I need to bind the function in the constructor, But I fear im not grasping exactly how delegates work because im stuck on the binding part

1 Like

Capture this in the lambda capture thing :

WebSocket->OnMessage().AddLambda([this](const FString& MessageString)
{
    this->PlayerMatched(MessageString);
}

When using lambdas with UObjects, prefer using WeakLambdas, so you won’t run into crashes due to a delegate being called on an object that doesn’t exist anymore.
GameInstance isn’t likely to be destroyed, but still I feel like there’s a decently high chance of your game crashing on exit. It works almost the same way you just pass the object as parameter first :

AddWeakLambda(this, [this](const FString& Msg) {
    //...
});
1 Like