Game Engine Architecture

(Ben Green) #1

334 7. The Game Loop and Real-Time Simulation


7.7.1. Client-Server

In the client-server model, the vast majority of the game’s logic runs on a single
server machine. Hence the server’s code closely resembles that of a non-net-
worked single-player game. Multiple client machines can connect to the server
in order to take part in the online game. The client is basically a “dumb” ren-
dering engine that also reads human interface devices and controls the local
player character, but otherwise simply renders whatever the server tells it to
render. Great pains are taken in the client code to ensure that the inputs of the
local human player are immediately translated into the actions of the player’s
character on-screen. This avoids what would otherwise be an extremely an-
noying sense of delayed reaction on the part of the player character. But other
than this so-called player prediction code, the client is usually not much more
than a rendering and audio engine, combined with some networking code.
The server may be running on a dedicated machine, in which case we say
it is running in dedicated server mode. However, the client and server needn’t
be on separate machines, and in fact it is quite typical for one of the client ma-
chines to also be running the server. In fact, in many client-server multiplayer
games, the single-player game mode is really just a degenerate multiplayer
game, in which there is only one client, and both the client and server are run-
ning on the same machine. This is known as client-on-top-of-server mode.
The game loop of a client-server multiplayer game can be implemented
in a number of diff erent ways. Since the client and server are conceptually
separate entities, they could be implemented as entirely separate processes
(i.e., separate applications). They could also be implemented as two separate
threads of execution, within a single process. However, both of these ap-
proaches require quite a lot of overhead to permit the client and server to
communicate locally, when being run in client-on-top-of-server mode. As a
result, a lot of multiplayer games run both client and server in a single thread,
serviced by a single game loop.
It’s important to realize that the client and server code can be updated
at diff erent rates. For example, in Quake, the server runs at 20 FPS (50 ms per
frame), while the client typically runs at 60 FPS (16.6 ms per frame). This is
implemented by running the main game loop at the faster of the two rates
(60 FPS) and then servicing the server code once roughly every three frames.
In reality, the amount of time that has elapsed since the last server update is
tracked, and when it reaches or exceeds 50 ms, a server frame is run and the
timer is reset. Such a game loop might look something like this:
F32 dtReal = 1.0f/30.0f; // the real frame delta time
F32 dtServer = 0.0f; // the server’s delta time
Free download pdf