Skip to Content

Entities

base_entity_t: schema field access, entity methods and entity lifetime.


An entity (base_entity_t) exposes its class's schema fields, including inherited fields. Names are exact and case-sensitive.

local pawn = entitylist.local_pawn()
if pawn then
    print(pawn:classname(), pawn.m_iHealth, pawn.m_ArmorValue)
end

a == b is true when both are the same entity: their handle() values, index and serial, are equal. Two lookups of one entity compare equal; an entity that later took the same index does not.

Every method exists on every entity. A pawn, controller or weapon method called on another kind of entity returns nil, reason and never raises. The kind is the entity's schema class lineage: any class deriving from C_BasePlayerPawn is a pawn, from CBasePlayerController a controller, from C_CSWeaponBase a weapon. When one of those methods or is_enemy() returns nil, the second value is the reason: not a player pawn, not a player controller, not a weapon, no local player controller, the handle does not resolve to a live entity (a handle field such as the active weapon, the controller or the pawn names nothing live), the pawn has no weapon services, the weapon has no vdata or the class is not in the client.dll schema. classname() and hitbox() return a bare nil.

MethodReturns
e:classname()Schema class name string, or nil.
e:index()Entity index (number).
e:handle()Entity handle (number): index in the low 15 bits, serial above. Resolve it with entitylist.handle.
e:is_alive()true while health is above zero and the life state is alive. A controller answers for its pawn.
e:is_enemy()true when the entity is not on the local player's team, or for everyone else while mp_teammates_are_enemies is on; never for the local player or its pawn. nil, reason without a local controller.
e:origin()World position as a vector copy.
e:angles()Absolute rotation as an angle copy.
e:velocity()Absolute velocity as a vector copy, from the game's own velocity query.
e:hitbox(index)hitbox_t for element index (zero-based) of the entity's hitbox set, or nil if the entity has no model, no hitbox set or no such element.
tostring(e)Class: 0x….

Pawn methods:

MethodReturns
e:weapon()The active weapon entity.
e:weapons()Lua table (array) of the weapons the pawn owns (m_hMyWeapons), in the game's order.
e:eye_position()Eye position as a vector copy, from the game's own eye-position query.
e:controller()The pawn's controller entity (m_hController).

Controller methods:

MethodReturns
e:playerslot()Player slot: the controller's entity index minus one.
e:userid()The player's userid, the number the engine's game events carry in their userid / attacker keys (a gameevent resolves those keys to the controller). In CS2 it is the player slot, so it equals playerslot().
e:pawn()The pawn the controller controls now (m_hPawn).
e:name()The player name as the scoreboard shows it, UTF-8. Only a CCSPlayerController has one; another controller answers nil, "not a player controller".

Weapon methods:

MethodReturns
e:vdata()table_t over the weapon's CCSWeaponBaseVData (w:vdata().m_nPrice).
e:item()table_t over the weapon's C_EconItemView (w:item().m_iItemDefinitionIndex).

Schema field access

Unknown fields read as nil; writes to unknown fields are ignored. Basic fields reject values of the wrong Lua type.

Schema typeRead resultWrite support
boolbooleanBoolean only.
8/16/32-bit integer, and CEntityIndex, GameTick_t, CSWeaponType, CSWeaponCategory, HitGroup_t, ObserverMode_t, CUtlStringTokennumberWhole number only.
uint64number, exact up to 2^53Whole number.
float, GameTime_tnumberNumber.
Vector2D, Vector, Vector4D / Quaternion, QAngleMatching FFI copy (vector2d, vector, vector4d, angle)Matching value kind.
CUtlString, CUtlSymbolLarge, CGlobalSymbol, char*string or nilIgnored.
char[N]stringString, truncated to N - 1 bytes.
Other fixed arrays T[N] of the basic types above, and CFiringModeFloat / CFiringModeInt (two elements: primary and secondary mode)array_tIndividual elements. Elements of other types read nil.
CHandle<…>Entity or nilIgnored.
CUtlVector of int32, uint32, float, QAngle or Vector; CUtlVector< CEntityHandle > (the raw handles, resolve them with entitylist.handle)utl_vector_int, utl_vector_uint32_t, utl_vector_float, utl_vector_angle_t, utl_vector_vec3_tContainer operations.
Pointer T*Pointed-to value, or nil for nullIgnored.
Embedded classtable_tIndividual fields.
Other types, including other enums, int64, float64, ColorField address as lightuserdataIgnored.

table_t uses the same field-access rules. An integer t[n] returns lightuserdata at the table's address plus n bytes; an entity's e[n] does the same from the entity's address. tostring(t) is the class and its bases, Class, Base, …: 0x…; tostring(a) for an array_t is array[N]: 0x….

Fields declared on a base class resolve on every derived entity: entitylist.local_pawn().m_pMovementServices is the pawn's movement services as a table_t, so pawn.m_pMovementServices.m_flMaxspeed reads its float. A null pointer field reads nil; a pointer to a basic type reads the pointed-to value, and a pointer to any other type that is not a schema class reads as its address (lightuserdata). A CHandle<…> field resolves the same way entitylist.handle does: nil once its entity is gone.

array_t uses zero-based indexing. #a returns its length, and a[i] = value writes an element. An integer outside 0..#a - 1 raises array index out of range on both reads and writes. A non-integer key reads as nil.

Entity lifetime

Entities, embedded table_t values, array_t values and utl_vector_* containers reference game memory. Do not retain them after the current callback. Store an entity handle and resolve it again when needed.

On this page