Thread communication

Hello,

Right now, I have a Runnable for connecting to and receiving from a FSocket. I’m pretty new to UE, so please bare with me. If the TCP connection can not connect to the server, I need to basically create a widget and add it the viewport. Creating the widget from code is no problem but I know you’re not supposed to make objects from other threads, so, what I’m asking is the best way to go about getting “events” or something like that from the runnable.

I’m pretty bad at explaining things so please let me know if something is unclear.

Thanks.

I’m interested by an answer too…

Hello,

When you create the FRunnable thread, also pass a TFunction pointer to it, which will be called if the TCP connection fails. Then fire that function, through an AsyncTask on the GameThread. Move the widget creation logic in that function. This will make sure, that the widgets are created on the Main thread, while keeping your logic intact on your worker thread.

You can use a AsyncTask to execute code on the GameThread from an other Thread.

Here a sample i use:

FFunctionGraphTask::CreateAndDispatchWhenReady([this]() {
			UE_LOG(LogTemp, Warning, TEXT("on gameThread"));
			// your code on gamethread
		}
		, TStatId(), nullptr, ENamedThreads::GameThread);

if you want to test use a sleep in the code and your game should freeze.