From Valve Developer Community
Revision as of 03:36, 3 February 2019 by Loudslappingsounds (talk | contribs) (Networking: they just dont need to be networked... just like with any logic entity)
Jump to: navigation, search

The GameRules object defines the abstract rules of a game. It conventionally handles all event processing that is not innate to players, NPCs, the world, or other such tangible entities.

In a mod that supported deathmatch and capture the flag game modes, the modes would each have their own GameRules class containing the appropriate logic for when to award points, where to spawn players, and so on.

Minimum implementation

In shared\<mod>\<mod>_gamerules.cpp:

#include "cbase.h"
#include "multiplay_gamerules.h"

	#define CSkeletonGameRules C_SkeletonGameRules

// Valve's base classes: CSingleplayRules, CMultiplayRules, CTeamplayRules, CTeamplayRoundBasedRules
class CSkeletonGameRules : public CMultiplayRules
	DECLARE_CLASS( CSkeletonGameRules, CMultiplayRules );


In server\<mod>\<mod>_client.cpp:

void InstallGameRules()
	CreateGameRulesObject( "CSkeletonGameRules" );

Managing shared code

GameRules are mostly written in shared code so that the client can predict the effects of its actions. However, there are some functions that are only present on the client or on the server.

To do: Best practices for recognising and managing this. #ifdefs confuse Visual Studio...


GameRules are not themselves networked. Instead, a proxy object is created by inheriting from the CGameRulesProxy class.

Notable functions


int Damage_GetNoPhysicsForce()
int Damage_GetShouldGibCorpse()
Various functions that return a integer bitstring containing the damage types that fall into the given category.
bool ShouldCollide()
Whether entities in the given collision groups should collide with each other. Since the arguments are collision groups and not entities, this function isn't as useful as it might at first seem.
bool SwitchToNextBestWeapon()
CBaseCombatWeapon* GetNextBestWeapon()
Two functions that can be used to choose a weapon for the player if, for instance, their current one runs out of ammo. SwitchToNextBestWeapon() returns true if the switch was successful.
float GetAmmoDamage()
Decides how much damage to apply when an entity (any entity!) fires bullets. Normally forwards calls to GetAmmoDef().
unsigned char* GetEncryptionKey()
Your mod's ICE key for script encryption.
CViewVectors* GetViewVectors()
Accessor for g_DefaultViewVectors.


void LevelShutdown()
Called when the GameRules are destroyed by the world.
void FrameUpdatePostEntityThink()
Called each frame. By default forwards the call to Think(). To do: What is the difference between this "PostEntityThink" func and EndGameFrame()?
void EndGameFrame()
Called at the end of CServerGameDLL::GameFrame() (i.e. after all game logic has run).
Creates the external entities needed for the GameRules to function.
Warning.png Warning: An untraceable crash will occur if you override this function without calling BaseClass::CreateStandardEntities().

There are too many other important functions to list here individually. See game\shared\gamerules.h line 179 onwards for the complete list, which covers:

  • Difficulty levels
  • The single/multiplayer status of a game
  • Client connection & disconnection
  • Client damage rules
  • Client kills and scoring
  • Spawning and respawning control
  • Weapon Damage
  • Weapon/item/ammo retrieval
  • Weapon/item spawn and respawn control
  • AI Definitions
  • Healthcharger respawn control
  • What happens to a dead player's weapons and ammo
  • Teamplay rules
  • Footstep sounds
  • NPCs
  • TraceLines
  • Chat team/location prefixes
  • Achievements