Sign in to follow this  
UrKrll

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

Recommended Posts

UrKrll    130

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.

Share this post


Link to post
Share on other sites
frob    44962

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.

Edited by frob
Typos, need to slow down a bit.

Share this post


Link to post
Share on other sites
wodinoneeye    1689

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.

Share this post


Link to post
Share on other sites
hplus0603    11356
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.

Share this post


Link to post
Share on other sites
UrKrll    130

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this