rameshvarun / netplayjs

Make P2P multiplayer browser games, no server hosting or synchronization code required. Powered by rollback netcode + WebRTC.
https://rameshvarun.github.io/netplayjs/
ISC License
496 stars 34 forks source link

Game Pauses When One Player Backgrounds a Tab #76

Open rameshvarun opened 1 year ago

rameshvarun commented 1 year ago

Right now, for both rollback and lockstep netcode, if one user backgrounds their tab, the game will totally freeze. Currently it’s not that big of an issue since we only support two-player games anyway, but as we move on to >2 players this problem must be solved.

Right now the main loops of both netcode implementations look something like this.

start() {
  setInterval(() => {
    this.tick();
  }, this.timestep);
}

The problem is that the browser will stop calling setInterval (and other callbacks like requestAnimationFrame) when the tab is backgrounded meaning that the game wont tick anymore.

The good news is that this is solvable. Even when backgrounded, the pages are responding to messages on the RTCDataChannel, meaning that event listeners are being run. We should be able to tick during these event listeners, though this will require some changes to the main loops to make this possible.