Should i run the client logic in separated thread or in update () ?

Started by
3 comments, last by UrKrll 8 years, 6 months ago

Hello,

so i'm working on a basic multiplayer game using lidgren.network and Unity, nothing big and just to learn it. But there's one question i have since a few days, should i run the networking part of the client in a separate thread or should i look for new messages in the update function ?

I have the fear that, when using update(), it will miss messages coming from the server.

Advertisement

It does not necessarily need to be in either a separate thread or in any specific function. Those are implementation details.

What matters is that you regularly check for and process incoming messages. The frequency of the task depends on the game.

If your implementation needs a separate thread for your program to check it regularly, then do what you need.

If your implementation needs it to be in a particular function to check it regularly, then do what you need.

If your game's implementation requires something else so you can check for network traffic regularly, then do what you need.

An issue might be can your playing logic handle having its situational data change WHILE its doing a decision making process (strategy/tactical or even a render cycle) ?

If not (real AI can take alot of resources sometimes requiring processing across the update period to come to a conclusion) then you have to lock down the internal world representation (no allowing changes while the AI processing is done) and in the meantime accumulating the situational changes to be applied to the next world representation for the next AI cycle.

If you have a separation of this kind, then a seperate thread is probably a good idea.

--------------------------------------------[size="1"]Ratings are Opinion, not Fact
The kernel buffers incoming messages. You don't have to "be there" right when the packet comes in to listen for it. You will not lose messages if you poll for messages from the main thread.

Also, polling from the main thread will not increase latency, because the time when that message affects the physics and game logic on the client is determined by the main thread anyway.

Thus, it's simpler, and just as robust, to poll for messages in the main thread; a separate thread for receicing network messages from the Lidgren library is not necessary or particularly helpful.
enum Bool { True, False, FileNotFound };

ok, i checked both possibilities eg: build 2 projects, one has the clientlogic in the update() function the other one runs a separated thread and both are spammed with messages.

no major problems with either of them.

But i'll stick to the update() function, it's easier to process the messages there.

Thanks for all the anwsers and helpfull comments.

This topic is closed to new replies.

Advertisement