Generalities On Entities
Introduction

This article is mainly about entities in the Source engine and will try to explain everything about entities.
Every object, even the world, is an entity in the Source engine. All entities are derived from CBaseEntity.
Naming Conventions
Within the Source SDK, server classes begin with a capital C (C
), while client classes begin with a capital C followed by an underscore (C_
) - this helps differentiate between the two code bases.
For example:
Server: CMyEntity Client: C_MyEntity
The style of naming convention followed through the SDK code base is known as Hungarian Notation. Beginners are recommended to maintain the same style as used throughout the code base to save confusion.
Base Classes
Every entity is based on CBaseEntity
, however there are many derived classes. The following is a list of the more common derived classes.
CBaseAnimating
Every entity that has a model uses CBaseAnimating. Classes derived from CBaseAnimating can set a model and animate.

AddEFlags( EFL_FORCE_CHECK_TRANSMIT );
CBaseTrigger
Triggers are brush based entities that are generally placed during the map creation process.
CBasePlayer
This entity is the player itself. Every player-entity in the game is CBasePlayer or is derived from this entity.
CGameRules
This entity regulates the rules of the current game. It's mainly the gameplay.
CBaseCombatCharacter
Every NPC & player are derived from this class.
Think Functions
Think functions are very important in the Source engine. Each entity "thinks", which is essentially just calling a function of that entity.
Server side
On the server, coders have the option to define multiple think-functions. However, only one think function can be called each time.
SetThink() and SetNextThink()
With the function SetThink
it is possible to set the think-function. With the function SetNextThink
the time is set when the function should be called.
For example:
SetThink(&CBasePlayer::DeathThink); SetNextThink( gpGlobals->curtime + 0.1f );
This piece of code will make sure that the function PlayerDeathThink
is called over 0.1 seconds. It the think-function needs to be called so fast as possible, setting the time to gpGlobals->curtime + 0.01f
would be better. When it's set to the current time, it may not work and the Source engine doesn't ever tick faster then 0.01 seconds.
Please note that after this call, the think-function is not called anymore. To keep it being called, a SetNextThink
call is needed in the function.
Every think-function needs to be defined in the datadesc of the entity. For example: BEGIN_DATADESC( CBasePlayer) DEFINE_THINKFUNC( DeathThink), END_DATADESC()
Client Side
The client does not provide the same methods for entities to think. These Client Thinks are handled in client_thinklist.cpp
. Multiple think functions cannot be defined here, as SetThink will not work properly on this side. Instead, ClientThink
and SetNextClientThink()
should be used.
ClientThink() and SetNextClientThink()
These are the equivalent of the server system, except only one function can be used. This will get called based on the SetNextClientThink() time set in the Spawn function, and in subsequent calls to ClientThink(). Two special time settings here: CLIENT_THINK_ALWAYS and CLIENT_THINK_NEVER.
Simulate()
Here's the commentary in c_baseentity.cpp: "Once-per-frame stuff should go in Simulate()." That really sums it up. Generally Simulate() should be used for Particle effects and other such graphical effects. Beware of what is run in Simulate(), because it is run every frame checks should be made to ensure that nothing too intensive is being called continuously.
Macros
Badly set up macros will give cause errors messages, crash the game and cause erratic behavior. The following is a list of the more common macro definitions and some of the issues that surround them.
LINK_ENTITY_TO_CLASS()
This is one of the main macros. An entity cannot be created without this macro being used to define the entity's classname (as returned from GetClassname()
on the server). However, on the client, an entity will not return it's classname unless there is a matching data description (DATADESC
).
PRECACHE_REGISTER()
This is used to tell the engine that it needs to precache the entity when it loads. This is provided with the name of the entity (the same name as passed to LINK_ENTITY_TO_CLASS() ).
DECLARE_CLASS()
This should be placed in the public section of an entities Class definition. This provides access to the BaseClass Macro.
DECLARE_DATADESC(), BEGIN_DATADESC(), END_DATADESC(), and DEFINE_XXX()
The Data Description macros provide for a number of different features;
- Specifying Think and Touch functions
- Providing Inputs and Outputs for mappers
- Providing external variable inputs (such as setting a model or the health of an item)
Further information is available on the Data Descriptions page.
Networking Entities
See the Networking section for more information on networking entities, as that is outside the scope of this article.