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 a method returns nil, the second value is the reason (not a player pawn, the handle does not resolve to a live entity, …).

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 value game events carry in their userid / attacker keys for this player. 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.

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; supported enums such as CSWeaponType, HitGroup_t, GameTick_tnumberWhole 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, char*string or nilIgnored.
char[N]stringString, truncated to N - 1 bytes.
Other fixed arrays T[N]array_tIndividual elements.
CHandle<…>Entity or nilIgnored.
Supported CUtlVector elements: int32, uint32, float, QAngle, Vectorutl_vector_*Container 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. Numeric t[n] returns lightuserdata at the field address plus n bytes.

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