Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The server needs to handle the physics, because all the movement will be handled by it. So for that I need a game loop. The thing is that the server doesn't have the render part, it's just a console.

All the posts I found on the internet were involving the render part. I'm not stupid, I know I should not take in consideration that part but then which type of loop should I use? (fixedtimestep, semi-fixed). My game functionality would resemble League Of Legends or Dota 2 functionality.

Actual questions:

1.What is the server functionality for this type of game?

2.What does the game loop look like?

Additional info: I use libGDX for the client part, and JBox2D on the server part, KryoNet for Networking and thinking of using lwjgl (only the System class) for Native timers.

share|improve this question
Related:… –  Byte56 Jun 21 '13 at 14:39
I don't know if i understood this right but i think he uses dynamic time-step there. –  Romeo Jun 21 '13 at 14:43
The OP is using a variable time-step. However, the answers recommend a fixed or semi-fixed time-step. See also this question:… –  Byte56 Jun 21 '13 at 15:10
Let's say i use deWiTTERS game loop. How can i get the tick count in Java ( GetTickCount() ). –  Romeo Jun 21 '13 at 15:18
Just a side note: anything involving physics is almost always more stable using a fixed time step, especially with inexpensive integrators and double-especially if any springs are involved. –  Patrick Hughes Jun 21 '13 at 17:23

2 Answers 2

When handling games with a fixed time step, which is what you would need to simulate a physics system, it is best not to go with the system Timer as it is not as precise as other methods (even though the difference is small). Here is an example of how you might go about running a game loop that iterates 60 times a second.

public void run() {
    double ns = 1000000000.0 / 60.0;
    double delta = 0;

    long lastTime = System.nanoTime();
    long timer = System.currentTimeMillis();

    while (running) {
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;

        while (delta >= 1) {

The running boolean would be set to true at the start of a program and is better practice than simply putting true, especially if there are multiple threads (You would need to have another thread listening for input from clients).

What makes this method better than using a Timer is if there is a hiccup or delay in one of the game ticks it is able to compensate. (ie. if the game for whatever reason only iterates 59 times in a second, the next second it will run 61 times) This is important considering the server may fall behind occasionally for whatever reason.

The tick() method would consist of all of the code to run the game. (The parts that include movement and such)

share|improve this answer

The server loop depends on the functionality required by the game. Generally the server is going to be responsible for maintaining an accurate game state because it is usually the authoritative party.

So what does this mean for our server loop? The server loop handles new connections from clients, client transmissions, and automated functionality.

Let's take a look at what the League of Legends in-game server loop might look like at a very high level.

void lolServerLoop() {
    if (elapsedtime >= fixedtimestep) {
        // spawn minions
        // update projectile positions
        // update health pools
        // update mana pools


    if (clientConnect) {

    if (clientDisconnect) {

    if (clientActionReceived) {

Now this is of course very simple, but the idea here is that you have some actions that happen on intervals (or every tic) such as updating player health or updating projectile positioning, and there are actions that are "triggered" such as updating the game state based on player input.

It might help to first map out the expected responsibilities of the server. For instance, if the server is only responsible each players position, then the loop might look like this.

void serverLoop() { if (clientActionReceived) { validateClientAction(); updateGameState(); } }

See how in this case the server only changes the game state when the client sends an action.

It will be worth your time to sit down and figure out all the required functionality before writing code. Experiment a lot using your desired network library starting by implementing simple systems and progressing towards the more complicated ones.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.