Difference between revisions of "Game initialization"

From Valve Developer Community
Jump to: navigation, search
m (clarified)
m (linked the german translation)
Line 1: Line 1:

Revision as of 11:19, 26 April 2013


The 'ignition circuit' of every Source game and mod is the file traditonally named <game>_client.cpp. It contains several scopeless functions (i.e. not in a class) that are called when server.dll is loaded, when the world spawns, when a player connects, and so on. One of its main purposes is to create a player entity for all connecting clients. For obvious reasons there can only be one game_client.cpp in a project!

Client.cpp functions

Various console commands etc. that are processed on the server but only affect one player. Changing player model, saying things, god mode...

The code is shared between all games and mods, so don't put anything game-specific in here!

Game_client.cpp functions

The functions in _client.cpp are apparently called from within the engine, and as such must always be the same.

void ClientPutInServer()
void ClientActive()
Called when a player connects and activates respectively. These functions define the C++ class used for players.
void respawn()
Called by ClientKill() and DeadThink() whenever a player dies. Spawns ragdolls in multiplayer, reloads the last saved game in singleplayer.
const char* GetGameDescription()
Returns the descriptive name of the .dll (e.g. Half-Life or Team Fortress 2).
CBaseEntity* FindEntity()
Called when the !picker targetname is invoked.
void ClientGamePrecache()
Precache function for the player. To do: Why not use CBasePlayer::Precache()?
void GameStartFrame()
Entry point for game-specific code that needs to run once per frame. Use with caution.
void InstallGameRules()
Decides which GameRules to use. Use CreateGameRulesObject( string ClassName ) to do this.

Game loop

(Adapted from this report by James Gadbury)

  • CServerGameDLL::GameFrame()
    • GameStartFrame()
    • Physics_RunThinkFunctions() - Runs the main physics simulation loop against all entities except players
      • Physics_SimulateEntity(player)
        • CBasePlayer::PhysicsSimulate()
        • CBasePlayer::PlayerRunCommand() - To do: This appears to actually be called from somewhere within the engine.
          • CPlayerMove::RunCommand()
            • CPlayerMove::StartCommand()
            • CBasePlayer::PreThink()
            • CBasePlayer::Think()
            • CPlayerMove::SetupMove() - applies a usercmd
              • CGameMovement::PlayerMove()
              • CGameMovement::FullWalkMove()
            • CPlayerMove::FinishMove()
            • CBasePlayer::PostThink()
            • CPlayerMove::FinishCommand()
    • IGameSystem::FrameUpdatePostEntityThinkAllSystems() - To do: call Think() on all non-player entities?
    • g_pGameRules->EndGameFrame()