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
updateGameState();
sendGameStateToClients();
}
if (clientConnect) {
registerClient();
}
if (clientDisconnect) {
unregisterClient();
}
if (clientActionReceived) {
validateClientAction();
applyClientActionToGameState();
}
}
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.
GetTickCount()
). \$\endgroup\$ – Romeo Jun 21 '13 at 15:18