0

I am building a game server using Nodejs. But I have problems running setTimeout maybe because I don't quite understand the concept of the game loop.

Here is my code:

let tickLengthMS = 1000 / 60;
let previousTick = Date.now();

export function gameLoop() {
    const now = Date.now();

    if (previousTick + tickLengthMS <= now) {
        const delta = (now - previousTick) / 1000;
        previousTick = now;

        update(delta);

    }

    if (Date.now() - previousTick < tickLengthMS - 16) {
        setTimeout(gameLoop);
    } else {
        setImmediate(gameLoop);
    }
}

So this is the game loop that updates my game. And I need to run another loop to send messages to the clients, in a different frequency / timestep.

export function updateNetwork() {
   pack = ...
   socket.emit('update', pack);
   setTimeout(updateNetwork, 1000 / 20);
}

Then I just run:

gameLoop();
updateNetwork();

But it never gets to run the second setTimeout function. I wonder if the first gameLoop function is blocking the second setTimeout. But isn't setTimeout a non-blocking method? Is the gameLoop function equal to while(true) {} in other languages such as java and C++?

Updated the question to make two loops.

newguy
  • 5,668
  • 12
  • 55
  • 95
  • I'm not a NodeJS guy, but looking at this as Javascript, wouldn't you want to use setInterval, not setTimeout, to run the updateNetwork function more than once? Gameloop is calling itself back once after a timeout, which then sets a new timeout, ad infinitum. You're putting a separate setTimeout outside that loop, which would only get called once... right? So that should be a setInterval... – joshstrike Dec 31 '16 at 06:09
  • @joshstrike setTimeout and setInterval do the same thing but a bit subtle difference. You can read the highest vote answer here http://stackoverflow.com/questions/729921/settimeout-or-setinterval. – newguy Dec 31 '16 at 06:17
  • No what I mean is, setTimeout only runs once. setInterval keeps running forever (until you clearInterval on the id of the interval it creates). In your gameLoop, you have setTimeout calling gameLoop, which calls setTimeout which calls gameLoop, etc. However, your updateNetwork function does not call a new setTimeout. You either need to call setTimeout again each iteration, or create a single setInterval that repeatedly calls updateNetwork. – joshstrike Dec 31 '16 at 06:22
  • @joshstrike Thanks I get what you mean. I guess I will need to put the second one in a loop. But The problem is now it never reaches the second setTimeout and I wonder if I should put it inside the update function of `gameLoop`. – newguy Dec 31 '16 at 06:26
  • Personally, I like having only one loop in any program, and chaining everything from that. In fact, that is exactly why people (not me) like NodeJS. But yes, if the update function fails you won't reach the second setTimeout, which is why if you are okay with having two timers/loops, you would be better off using setInterval for your update loop. Then you don't need to call it except once at the beginning of the program. – joshstrike Dec 31 '16 at 06:28
  • @joshstrike Thanks my mind is blown after reading tons of articles that use either setTimeout, setInterval or setImmediate in the game loop. Now I am just more confused. – newguy Dec 31 '16 at 06:31
  • Just think of setTimeout as a one-time delay, whereas setInterval creates a repeating timer. setImmediate is some NodeJS thing that puts the function at the top of its list of tasks, I think. – joshstrike Dec 31 '16 at 06:34
  • @joshstrike Putting the second setTimeout in a loop actually I can see it firing the events. I guess if it is not in a loop it might just skip the setTimeout if it doesn't get the allocated time to execute the command. – newguy Dec 31 '16 at 12:07

0 Answers0