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.

  • \$\begingroup\$ Related: gamedev.stackexchange.com/questions/46023/… \$\endgroup\$ – MichaelHouse Jun 21 '13 at 14:39
  • \$\begingroup\$ I don't know if i understood this right but i think he uses dynamic time-step there. \$\endgroup\$ – Romeo Jun 21 '13 at 14:43
  • \$\begingroup\$ The OP is using a variable time-step. However, the answers recommend a fixed or semi-fixed time-step. See also this question: gamedev.stackexchange.com/questions/12347/… \$\endgroup\$ – MichaelHouse Jun 21 '13 at 15:10
  • \$\begingroup\$ Let's say i use deWiTTERS game loop. How can i get the tick count in Java ( GetTickCount() ). \$\endgroup\$ – Romeo Jun 21 '13 at 15:18
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Patrick Hughes Jun 21 '13 at 17:23

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)

| 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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