> For the complete documentation index, see [llms.txt](https://lua2.axeprimecs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lua2.axeprimecs.com/instances-and-types/instances/entity-system.md).

# Entity System

Access game **entities** (players, weapons, etc.) through the entity list.

***

## Entity List Functions

| Function                               | Returns                     | Description                                                                                                                  |
| -------------------------------------- | --------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `entities.get_entity(index)`           | `entity` / `nil`            | Get entity by index                                                                                                          |
| `entities.get_local_pawn()`            | `player_pawn` / `nil`       | Local player pawn (alias)                                                                                                    |
| `entities.get_local_controller()`      | `player_controller` / `nil` | Local player controller                                                                                                      |
| `entities.get_players(enemies_only?)`  | `table`                     | Alive player pawns. `enemies_only` defaults to `true` — pass `false` to include teammates. Dead players are always excluded. |
| `entities.controllers()`               | `table`                     | All player controllers                                                                                                       |
| `entities.get_spectators()`            | `table`                     | Controllers spectating the local player                                                                                      |
| `entities.get_projectiles()`           | `table`                     | Cached grenade projectiles                                                                                                   |
| `entities.get_by_type(type_id)`        | `table`                     | Cached entities by type                                                                                                      |
| `entities.get_all()`                   | `table`                     | All cached entities                                                                                                          |
| `entities.get_by_handle(handle_value)` | `entity` / `nil`            | Entity from handle int                                                                                                       |
| `entities.to_ptr(entity)`              | `uintptr`                   | Raw pointer (FFI)                                                                                                            |
| `entities.get_local_pawn_ptr()`        | `uintptr`                   | Raw pointer to local pawn (FFI)                                                                                              |

***

## 🗂️ Schema Access

Raw game (schema) fields are **not** exposed as direct properties anymore. Read and write them through `get_schema`, which returns a `schema_accessor`.

```lua
local pawn = entities.get_local_pawn()
if pawn then
    local health = pawn:get_schema("m_iHealth"):get()
    pawn:get_schema("m_iHealth"):set(100)
end
```

`get_schema(field)` is available on entity, player, weapon and controller objects. The accessor reads/writes by inferring the type from the field name prefix:

| Prefix                            | Type       |
| --------------------------------- | ---------- |
| `m_b`                             | bool       |
| `m_fl`                            | float      |
| `m_vec` / `m_ang`                 | vec3       |
| `m_i` / `m_n`                     | int        |
| `m_un` / `m_ub` / `m_us` / `m_ul` | uint       |
| `m_h`                             | handle int |
| `m_sz`                            | string     |

### `schema_accessor`

| Method                  | Returns         | Description                                              |
| ----------------------- | --------------- | -------------------------------------------------------- |
| `is_valid()`            | `bool`          | Whether the field offset resolved                        |
| `get()`                 | value / `nil`   | Read the field value                                     |
| `set(value)`            |                 | Write the field value (basic types)                      |
| `get_as(module, class)` | `schema_object` | Treat the pointer at this field as `class` from `module` |

### `schema_object`

Returned by `schema_accessor:get_as(...)`. Lets you chain schema reads on a non-entity pointer.

| Method              | Returns           | Description                     |
| ------------------- | ----------------- | ------------------------------- |
| `is_valid()`        | `bool`            | Whether the pointer is non-null |
| `get_schema(field)` | `schema_accessor` | Field accessor on this object   |

```lua
local proxy = game.entity_system:get_first_by_class("C_CSGameRulesProxy")
if proxy then
    local rules = proxy:get_schema("m_pGameRules"):get_as("client.dll", "C_CSGameRules")
    if rules:is_valid() then
        print(rules:get_schema("m_bWarmupPeriod"):get())
    end
end
```

***

## Entity (c\_base\_entity)

| Property / Method                                                                                                                                                                     | Returns           | Description              |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- | ------------------------ |
| `index`                                                                                                                                                                               | `int`             | Entity index (property)  |
| `handle`                                                                                                                                                                              | `int`             | Entity handle (property) |
| `get_scene_origin()`                                                                                                                                                                  | `vec3`            | World position           |
| `get_schema(field)`                                                                                                                                                                   | `schema_accessor` | Schema field accessor    |
| `get_class_name()`                                                                                                                                                                    | `string`          | Entity class name        |
| `is_alive()`                                                                                                                                                                          | `bool`            | Health > 0               |
| `is_player_pawn()`                                                                                                                                                                    | `bool`            | Type check               |
| `is_player_controller()`                                                                                                                                                              | `bool`            | Type check               |
| `is_weapon()`                                                                                                                                                                         | `bool`            | Type check               |
| `is_enemy()`                                                                                                                                                                          | `bool`            | Is enemy                 |
| `is_breakable()`                                                                                                                                                                      | `bool`            | Check breakable          |
| `is_world()`, `is_sky()`, `is_preview()`, `is_inferno()`, `is_planted_c4()`, `is_bomb()`, `is_projectile()`, `is_he_projectile()`, `is_smoke_projectile()`, `is_molotov_projectile()` | `bool`            | Type checks              |

> Fields like health, team, flags, velocity, armor, eye angles, ammo, etc. are now read via `get_schema` (e.g. `pawn:get_schema("m_iHealth"):get()`).

***

## Player Controller (c\_cs\_player\_controller)

| Property / Method   | Type                  | Description           |
| ------------------- | --------------------- | --------------------- |
| `name`              | `string`              | Player name           |
| `pawn`              | `player_pawn` / `nil` | Associated pawn       |
| `observer_pawn`     | `player_pawn` / `nil` | Observer pawn         |
| `steam_id`          | `uint64`              | Steam ID              |
| `is_local`          | `bool`                | Is local player       |
| `get_schema(field)` | `schema_accessor`     | Schema field accessor |

> Other controller fields (ping, tick\_base, controlling\_bot, pawn\_is\_alive, comp\_team\_color, …) are read via `get_schema` (e.g. `ctrl:get_schema("m_iPing"):get()`).

***

## Player Pawn (c\_cs\_player\_pawn\_base)

Inherits from `entity` (incl. `get_schema`) with additional methods:

| Property / Method           | Returns                     | Description                                                                                    |
| --------------------------- | --------------------------- | ---------------------------------------------------------------------------------------------- |
| `has_defuser`               | `bool`                      | Has defuse kit (property)                                                                      |
| `has_helmet`                | `bool`                      | Has helmet (property)                                                                          |
| `get_eye_pos()`             | `vec3`                      | Eye/camera position (method)                                                                   |
| `get_bone_position(index)`  | `vec3` / `nil`              | World-space bone position by index. Returns nil if index out of range or skeleton unavailable. |
| `get_bounding_box()`        | `table` / `nil`             | Screen-space box `{ x, y, w, h }`, or `nil` if off screen.                                     |
| `get_hitbox_center(hitbox)` | `vec3` / `nil`              | World-space hitbox center (`defs.hitbox.*`).                                                   |
| `get_hitbox(hitbox)`        | `table` / `nil`             | `{ min, max, center, radius }` in world space.                                                 |
| `get_bones()`               | `table` / `nil`             | All bone world positions (`vec3`), 1-indexed.                                                  |
| `can_see(other, smoke?)`    | `bool`                      | Line-of-sight to another pawn (smoke-aware).                                                   |
| `has_recent_sound()`        | `bool`                      | True if this player made a recent sound.                                                       |
| `controller`                | `player_controller` / `nil` | Associated controller (property)                                                               |
| `active_weapon`             | `weapon` / `nil`            | Active weapon (property)                                                                       |
| `weapons`                   | `table`                     | Array of weapon entities (property)                                                            |
| `observer_mode`             | `int`                       | Observer mode (property)                                                                       |

> Raw fields (armor, is\_scoped, is\_defusing, eye angles, shots\_fired, flash\_duration, addon\_bits, …) are read via `get_schema` (e.g. `pawn:get_schema("m_ArmorValue"):get()`).

***

## Weapon

The `weapon` usertype inherits from `entity` (incl. `get_schema`) and provides weapon-specific methods:

| Method              | Returns           |
| ------------------- | ----------------- |
| `is_grenade()`      | `bool`            |
| `is_knife()`        | `bool`            |
| `get_inaccuracy()`  | `float`           |
| `get_spread()`      | `float`           |
| `get_schema(field)` | `schema_accessor` |
| `get_weapon_data()` | `table` / `nil`   |
| `get_name()`        | `string`          |

`get_weapon_data()` returns the weapon's static data: `damage`, `range`, `range_modifier`, `penetration`, `headshot_multiplier`, `armor_ratio`, `cycle_time`, `max_clip`, `max_speed`, `type`.

> Raw weapon fields (clip1, clip2, in\_reload, zoom\_level, recoil\_index, …) are read via `get_schema` (e.g. `wep:get_schema("m_iClip1"):get()`).

***

## 💡 Complete Entity Example

```lua
events.register("paint", function()
    if not engine.is_in_game() then return end

    local local_pawn = entities.get_local_pawn()
    if not local_pawn then return end

    local my_pos = local_pawn:get_eye_pos()
    local players = entities.get_players(false)

    for _, pawn in ipairs(players) do
        if pawn:is_alive() then
            local controller = pawn.controller
            if controller and pawn:is_enemy() then
                local head = pawn:get_bone_position(6)
                if head then
                    local screen = math.world_to_screen(head)
                    if screen then
                        local name = controller.name
                        local hp = pawn:get_schema("m_iHealth"):get()
                        local dist = my_pos:dist_to(head)
                        local text = string.format("%s [%dHP] %.0fm", name, hp, dist / 64)
                        render.text(screen.x, screen.y, text, color(255, 255, 255), 8)
                    end
                end
            end
        end
    end
end)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://lua2.axeprimecs.com/instances-and-types/instances/entity-system.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
