0

I'll try to explain this as easily as I can.

I basically have a game that hosts multiple matches. All the matches on the server are all processed by the server, no players host a match on their computers via port forwarding, it's all done by the server so they don't have to.

When a player requests to make a match, a match object with a thread is made on the server. This works fine. Each match object has it's own list of players. However, the game client runs at 30FPS and it needs to be in sync with the server, so just updating all the players in the thread loop will not do since it's not run at 30FPS.

What I'm doing right now is I use a game engine: SFML, which in its window loop, goes through all the players in the server and runs their update code at 30FPS. This is fine however, when there comes a time where they may be a huge chunk of people, it will be better that the players are updated via the match threads so as not to slow down the processing speed by having it all done in one render loop.

What I want to know is, how would I simulate 30FPS in a match's thread loop? Basically have it so each player's update() function is called in the timeframe of 30FPS, without having to use a rendering engine such as SFML to limit how fast the code is to be run? The code is run in the background and output is shown on a console, no rendering is needed on the server.

Or put simply, how do I limit a while loop code to run in 30FPS without a game engine?

Joe Bid
  • 465
  • 8
  • 24

1 Answers1

2

This seems a bit like a X Y problem : You try to sync players via server "FPS" limit. Server side doesn't really work like that.

Servers sync with clients in terms of time by passing to the client the server's time on each package or via a specific package (in other words, all clients have only the server's time).

But regarding server side implementations for gaming :

The problem is a bit more broad than what you mentioned. I'll post some guidelines which hopefully will help your research.

First of all, on server you don't require rendering, so FPS is not relevant (30 fps is required in order to give our eyes the sensation of fluidity). Server usually handles logic, like for example various events (for example someone fires a rocket, or a new enemy has spawned). As you can see events don't require FPS, and they are randomly triggered.

Something else that is done on the server is the Physics (or other player-player or player-environment interactions ). Collisions are done using a fixed update step. Physics are usually calculated at 20 FPS for example. Moving objects get capsule colliders in order to properly simulate interaction. In other words, severs ,while not rendering anything, are responsible for movement/collisions (meaning that if you don't have a connection to the server you won't move / go through walls , etc - implementation dependent).

Modern games also have prediction in order to reduce lagging (after all, after you give any input to your character, that input needs to get to the server first, get processed and be received back in order to have any effect on the client). This means that when you have an input on a client (let's take moving for example) , the client starts making the action in anticipation , and when the server response comes (for our example the new position) it will be considered as a correction. That's why sometimes in a game when you have lag you perceive that you are moving in a certain direction then all of a sudden you are somewhere completely different.

Regarding your question :

Inside your while loop, make a deltaT , and if that deltaT is lesser than 33 miliseconds use sleep(33-deltaT) .

As you requested, I'm posting a sample of deltaT Code :

 while (gameIsRunning)
 {
      double time = GetTickCount();
      UpdateGame();
      double deltaT = GetTickCount()-time;
      if (deltaT < 33 )
      {
          sleep(33- deltaT);
      }
 }

Where gameIsRunning is a global boolean, and UpdateGame is your game update function.

Please note that the code above works on Windows. For linux, you will require other functions instead of GetTicksCount and sleep.

MichaelCMS
  • 4,703
  • 2
  • 23
  • 29