Generalities On Entities

From Valve Developer Community
Jump to navigation Jump to search
English (en)Русский (ru)Translate (Translate)

Introduction

Note.pngNote:This article assumes that the reader has at least basic experience in C++

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.

Note.pngNote:Entities without models are not networked by default. A workaround is AddEFlags( EFL_FORCE_CHECK_TRANSMIT );
Note.pngNote:To use animated models be sure to initialize the playback rate using SetPlaybackRate(1.0f); and call StudioFrameAdvance(); in the think function.

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

Main article:  Think()

Think functions are a group of functions comprising the main way to have an entity act without input. An entity's Think() is run once after it spawns, and can be told when to call itself again using SetNextThink(). Using SetThink() can change between several Think() functions, and GetLastThink() returns the time of the last think function. See Think() for detailed description.

Macros

Badly set up macros will give error 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 its 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 entity's 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.