# Things and components

Everything in your game is a thing, and what a thing is comes from the components you
give it. A thing
(the engine's code calls it an entity) is a number and, if you give it one, a name. A
component is a small record of named fields: `Transform = { x = 40, y = 90 }` says where
it is, `Rect = { w = 12, h = 12, color = 0xf2364b }` that it is drawn as a red square.
There are no classes and no inheritance: a thing that should fall gets a `Body`, one that
glows gets an `Emission`, and a script finds things by what they have.

## The engine's components and your own

The engine declares the components its systems read: drawing (`Transform`, `Rect`,
`Sprite`, `Text`, `Tilemap`, `Camera`, `Mesh`, `Light`, `Sky`), physics (`Body`,
`Collider`, `Mover`, `Joint`), the interface (`Node`, `Button`, `Bar`, ...), animation
(`Clip`, `Tween`) and more. Each one's fields, types and defaults are in the
[reference](../reference/index.md).

A game declares its own in its project file, under `[components]`: a name and its
fields with their defaults. The default's form is the field's type: `0.0` is a number,
`0` a whole number, `""` text, `false` true or false.

```toml
[components]
Spinner = { speed = 90.0 }
Coin = { value = 1 }
```

Give each kind of thing its own component, even an empty one (`Coin = {}`): `each("Coin")`
then finds every coin, the editor groups them, and the [protocol](protocol.md) can ask
for them. A name the engine owns cannot be one of yours (`E0528` says which revision
took it, so you can rename yours).

## In a scene, in a script

A scene file names the things a game starts with and gives each its components. This is
the basics example the concepts pages share (from the engine's examples, shipped at launch):

```toml
# example: basics/scene.toml
# Every thing the game starts with, by name, and its components.
[hero]
Transform = { x = 40, y = 90 }
Rect = { w = 12, h = 12, color = 0xf2364b }
Spinner = { speed = 0.0 }

[coin]
Transform = { x = 200, y = 90 }
Rect = { w = 8, h = 8, color = 0xf5c542 }
Rotation = { angle = 0 }
Spinner = { speed = 180.0 }
Coin = { value = 5 }

[score]
Transform = { x = 8, y = 168 }
Text = { text = "SCORE 0", size = 1, color = 0xeceae6 }
```

A script reads and writes fields by name, finds things by name or by components, makes
and removes them, and sends events:

```lua
-- example: basics/main.luau
-- Moves the hero with the move_x action, spins every Spinner, and turns a touched coin
-- into points: an event sent in one place and read in another.
state.score = state.score or 0

local SPEED = 90

function step(dt)
    local hero = find("hero")
    set(hero, "Transform", "x", get(hero, "Transform", "x") + input.get(0, "move_x") * SPEED * dt)

    for _, thing in ipairs(each("Spinner", "Rotation")) do
        local angle = get(thing, "Rotation", "angle") + get(thing, "Spinner", "speed") * dt
        set(thing, "Rotation", "angle", angle % 360)
    end

    local coin = find("coin")
    if coin and math.abs(get(coin, "Transform", "x") - get(hero, "Transform", "x")) < 10 then
        send("picked", coin, get(coin, "Coin", "value"))
        despawn(coin)
    end

    for _, picked in ipairs(events("picked")) do
        state.score += picked.value
        set(find("score"), "Text", "text", "SCORE " .. state.score)
    end
end
```

| Call | Does |
| --- | --- |
| `find("hero")` | The named thing, or nil |
| `each("Spinner", "Rotation")` | Every thing with both components |
| `get(e, "Transform", "x")`, `set(e, "Transform", "x", 40)` | Read or write one field |
| `has(e, "Coin")` | Whether it has the component |
| `spawn({ name = "spark", Transform = { x = 1, y = 2 } })`, `despawn(e)` | Make or remove a thing (and what is under it) |
| `send("picked", e, 5)`, `events("picked")` | Send an event, read the step's events of that name |

## Names and parents

A name is part of the world: the scene's keys give them, `spawn({ name = ... })` gives
one, and no two living things share one (`E0006`; `E0106` when a scene brings it). A thing
can be under another with `Parent = { of = "name" }`: under a thing with a `Transform`
its own `Transform` is local to it, so it moves with it; under a group (a named thing
with no `Transform`) nothing moves, it is only filed there. Despawning a thing despawns
what is under it. The third-person sample puts
the hero's drawn capsule under the capsule the physics walks.

## What is not a component

What a script keeps in `state` (plain numbers, text, true or false, and tables of
those) is game state too, and the checksum covers it. A script's own `local` outside its
functions is not: it is a cache that a reload, a seek or a rollback does not bring back.
[Steps and determinism](steps.md) says why that matters.
