Server game loop and syncronization questions

Hello! I’m studying how to make multiplayer games and I came across something I don’t understand how to do correctly: The game loop on the server side.
I’m thinking about a space top down shooter now, just to limit the scope. In my case, it is a kryonet application with LIBGDX. What I have planned for the multiplayer follows:
Client sends spawn message TCP.
Server handles the message, spawning the client ship.
Server responds the message with a snapshot TCP.

–> if there are more than two players
Server sends a start game message TCP (for UI) and starts the game (spawning of aliens).
During game:
Client streams player input through UDP.
Server process the input simulating all players.
Server sends snapshots TCP/UDP? to players each server tick.

–> if game over condition is met
Server sends end game message TCP.

I have several questions though…
Q1 - How will the server run by it self? I though of making a separate thread that calls the methods for sending stuff to the client (the snapshots).
Q2 - How much time a server tick should have? Wouldn’t this depend on the clients game loop delay?
Q3 - I currently sync the spawning of bullets to the ship firing animation… is that going to be a problem since the rendering is client side?
Q4 - Valve’s article says in source that object positions are processed both locally and server side, with interpolation to always use the server over the client. Can that be
applied to shooting?
Q5 - Should the player send a input message each game loop tick? Or should I sample the input with time intervals?
Q6 - Should the server snapshots be sent over UDP or TCP?
Q7 - What calls the server update loop? Won’t the server simulation run without sync with the client?

Thanks so much for taking your time to read all this!
If there is any good article about this, or a example game, even books, send me the names! =)

My suggestions below…

Q1 - How will the server run by it self? I though of making a separate thread that calls the methods for sending stuff to the client (the snapshots).

The server should be written in a manner that it can run entirely in a seperate thread or JVM than the client.
Its game loop is all about calculating updates and publishing data to the clients that will keep the client in sync.

Q2 - How much time a server tick should have? Wouldn’t this depend on the clients game loop delay?

The servers game loop should not be driven by the client but rather based on real world lapsed time.

Q3 - I currently sync the spawning of bullets to the ship firing animation… is that going to be a problem since the rendering is client side?

I would need more details to be this specific. Consider telling the server your firing and having the server tell the client back look theirs bullets here. When they bullet event from the server first arrives… your ship fires.

Q4 - Valve’s article says in source that object positions are processed both locally and server side, with interpolation to always use the server over the client. Can that be
applied to shooting?

yes. But the server deciding a hit has occured should always win over
any local calculation.

Q5 - Should the player send a input message each game loop tick? Or should I sample the input with time intervals?

The player should only send data to the server when it needs to push new information to it. No need to send needless network traffic.
Dending on how your doing your client data updates the client may poll the server for updates or the server will push updates to the client… there are pros and cons to both approaches.

Q6 - Should the server snapshots be sent over UDP or TCP?
if you want the server to be able to reside outside the users local network then TCP. UDP is great for service discovery on a local network.

Q7 - What calls the server update loop? Won’t the server simulation run without sync with the client?
see response to Q2

Many thanks!!

Let me see if I got it:
The server will have its own thread, that run endlesly.
The server will then have its own FPS.
The player should send messages like “mouseButton1Down” “mouseButton1Up” and only when the state changes.
All messaging will be TCP.

If that is good then I’m a happy dev and you deserve TONs of good coffe, with cookies.

The server snapshots have a timestamp?
The thing that stills bother me is that the server have its own fps…
imagine that the client is running at a framerate of 60, and the ship travels 100 pixels,
then the server sends an update, but there the fps is 200 and the ship travelled 350.2 pixels…
(I compute the position using RK4 integrations but still… this sync thing is hard to grasp)

Something like this?

https://dl.dropboxusercontent.com/u/1466740/isThis.png

I think your relating the client game loop too closely to the server one.

lets try a different approach to explain what i would recommend.

The game client is responsible for drawing and interacting with the user.

the game server is responsible for managing game assets that need to be kept in sync across game clients.

When we talk about the server’s game loop… loop may not be the best term.
I personally use a scheduled thread that runs every 0.x seconds (.3 for my current game)

Example from my current game:

The server starts…
A scheduled thread is created that runs every .3 seconds.
When this thread executes it will :
-run a routine to update any calculated modifications to the models its managing.

outside this loop the server provides the following operations that can be invoked by clients

getCoordsList(); - return a list of game coords for the assets being managed by the server

getModelData(id) - as my game is 3d if a model is in the coords list but the client doesnt have a local copy of this model it can ask for the data needed to make it.

addModelData(ModelData) - the client add a new model to the server to manage from this point forward. Other clients will find out about via the combination of the two other methods. (getCoordsList and getModelData)

Note: Nothing on the server is ever related to FPS.

The server threading is based on real world time.

So if your client is slow… and another is fast… the objects managed by the server dont react differently.

hope this helps :slight_smile:

in your diagram… when a user presses a button… dont make the server interpret what to do… thats the clients apps job. But do tell the server about what it needs to track.

so if LeftClick = fire gun… then yes there is likely a call to the server to fire weapon… but the server should have no idea this was caused because the user left clicked. :slight_smile:

I see now! ;D
So in your game the client pull server updates… I think I’m going for the server pushing snapshots.
I also understood that the messages of input should be “fireShip” instead of “mouseButton1”.
nice.

I’ll try to sync a throw-away prototype that I have…

an alternative that matches what you want is each time the server runs its update thread it ends with broadcasting out to all clients the updated data.

If the message is lost in the network so be it… youll get it next update. :slight_smile:

this work perfectly fine.

one tip in this approach… bake a timestamp in the packet before sending it to clients.
the client will only accept new data that is newer than previously processed.

Otherwise you WILL get a situation where the sever sends packets A B C
and your client will receive them as A C B and youll get a stutter effect in your game.

ORGASM.

oZai73uc4g8

Success! Now I can proceed to implement a framework XD.

Huh… That’s an interesting game idea :smiley:

lol looks like your multiple cock textures need some good ol alpha blending

My ‘artist’ friend doesn’t know the meaning of .png

Well, your artist has obviously never done anything with computer graphics before then.

Yeah, that or he just want to stick with paint tool sai forever, and will never touch photoshop =P

Well, thanks for the help. I’ll code a framework for this kind of sync and if everything goes ok, I’ll post and teach about it XD

unfortunately im at work and thus cant see your video/image right now… but gratz on getting it working!

j.

I’m back here just to say thanks again. Seeing this working is a total dream of mine.
Multiplayer means much to me…
I’ve added the rest of the game to the server now and it is just amazing seeing that I’ve coded
that and it is not crashing nor flickering nor nothing actually. Is just amazing.
I’ll study now how to generalize this architecture a bit and try to provide either a tutorial or
library (on top of kryonet) to make that super easy for people starting into games. Just like the libs I’m using
made that easy for me.
Something like ‘extends LiveEntity’ with a few ‘@ID and @Sync’ annotations, a ‘client.processLastSnapshot()’…

pA_sJwbLeoA

Lol you should make it so that they are shooting something more appropriate … like unfertilized eggs and instead of having them blow up they turn into a fetus and fly away lol