Game initialization

From Valve Developer Community
Jump to: navigation, search
Deutsch 简体中文

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!

Custom <game>_client.cpp

Rather than modify hl2_client.cpp or hl2mp_client.cpp it is advised to create your own. The functions defined in this file are global functions so to create your own <game>_client.cpp simply copy the existing one and rename it and then exclude the old one for the build. (right click->properties->Exclude from build -- yes),

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()