Having one static big thread running through the whole game


I am currently trying to create some sort of a “lobby” logic for a future game. As Unreal can’t do it “naturally”, I have decided to use the FSocket system coupled with an external server.

But here I am having a threading issue ; the game is freezing when I am doing things on the socket. I achieved to create a dedicated thread using the FRunnable and Thread systems. The only way I found to actually retrieve data from the thread is to set a timer which every second looks if a new packet has been received, and processes it. It’s currently working, but I would like to have this timer constantly active (in a static way), because I need to be able to send and recieve packets during the whole game… and I didn’t find how to do this.

Could you help me ? The ideal would be having one big class which contains static functions, each one corresponding to one packet (like OnPacketXX), and one static function to send data to the server, everything thread safe obviously. I know how to do this in Java but here, whenever I try to execute code (for example to change an element of the UI) from the socket thread I have an error saying that the thread is not safe to render Slate widgets…

Thanks ! And sorry for my english, it’s not my native language :stuck_out_tongue:

Why wouldn’t Unreal be able to do it “naturally”?

The error message tells you what’s wrong; you can’t render slate widgets from another thread. You’re not doing this in a thread-safe manner at all. How do you ensure that nothing is modifying internal Slate structures while you try to render?
You could decouple the rendering from the socket code, let the socket polling happen asynchrously and render on the thread which is running Slate. Or you could push rendering updates to a queue (from the socket thread) and process these updates on the thread running Slate. Just make sure you lock the queue when you push to it. Or you could avoid using threads altogether and just poll the socket asynchrously.

Thanks for your answer :slight_smile:

Because Unreal is made for games, not for networking and databases stuff. The thing is that I would need to have several levels loaded at once (one level per match between two teams). So I found a workaround, I need to have several Unreal servers running at once on one single server, and a “lobby manager” to manage them. The lobby creates the Unreal servers, launches it and says to the clients to connect to it while keeping a connection to the lobby manager. I didn’t find out how to connect to a Unreal server from the game without changing the map. I don’t know if I’m clear ^^’

I know that, I am trying to find a solution to this ^^

This is actually the solution I considered using, but in order to do this I need a timer which every seconds (or whatever interval) checks if there is new data from the socket and processes it. But as I said, the socket needs to be running continuously, and I didn’t find how to create a “global” timer which is not depending on the current level or an actor or whatever UThing.

Sound interesting, how can I do this ? Do you have any link to some tutorials ?

Thanks again ^^