StructOp Handbook
Contents Examples Commands
Handbook / Game development

Entities and systems

The dungeon crawler represents each creature or item as a numbered entity. A component holds one part of its state, such as position or health. A system runs a function over the entities that have the components it needs.

For example, the game can render every entity that has both a position and a glyph. The same declarations work with the C and Rust core providers.

On this page

Components#

A component is a struct with storage for every entity. Without fields it is a tag.

components.st
ability "component" "Point" {
    fields = [{ name = "x", type = Int }, { name = "y", type = Int }]
}

ability "component" "Health" {
    fields = [{ name = "current", type = Int }, { name = "max", type = Int }]
}

ability "component" "Player" {
}

Fields may be enums declared by providers (type = Color), other structs (destination = Point) and arrays: the dungeon crawler's FieldOfView keeps one flag per map tile in { name = "visible_tiles", type = Array, of = Bool, size = 4000 }, and each monster checks its own set before chasing.

Spawning and removing#

spawner.st
ability "spawn" "Player" {
    components = {
        Player = {},
        Point  = { x = x, y = y },
        Render = { fg = Color.white, bg = Color.black, glyph = "@" },
        Health = { current = 10, max = 10 },
    }
    entity = player
}
AbilityRust (legion) equivalent
spawn with components and optional entityecs.push((...))
despawn with entitycommands.remove(entity)
attach with entity and componentscommands.add_component(entity, value)
detach with entity and a component listcommands.remove_component::<T>(entity)
despawn_allWorld::default()

Queries#

A query runs its body once for every live entity that has all components in with (and none in without). bind gives names to the current entity's components; entity receives its number; only restricts the query to one entity.

systems.st
ability "function" "entity_render" {
    body = [ability.query.Rendered]
}

ability "query" "Rendered" {
    with = [Point, Render]
    bind = { pos = Point, render = Render }
    body = [ability.draw.Entity]
}

ability "draw" "Entity" {
    x          = pos.x - data.value.camera.current.left_x
    y          = pos.y - data.value.camera.current.top_y
    glyph      = render.glyph
    color      = render.fg
    background = render.bg
}

bind adds a pointer local to the enclosing function, so a query that binds must sit inside a function. Reading is pos.x; writing a field is set target = health.current; replacing a whole component is store pointer = pos value = destination.

Systems and schedules#

Systems are ordinary functions, and a schedule is a function that calls them in order. In the dungeon crawler, a switch in on_tick chooses one of three schedules based on the turn state. The game can wait for input, process the player's turn or process the monsters' turn.

main.st
ability "switch" "Turn" {
    value = data.value.turn_state.current
    cases = [
        { value = TurnState.AwaitingInput, run = [ability.call.InputSystems] },
        { value = TurnState.PlayerTurn, run = [ability.call.PlayerSystems] },
        { value = TurnState.MonsterTurn, run = [ability.call.MonsterSystems] },
    ]
}

Messages of intent#

Systems exchange messages through short-lived entities. For example, the input system spawns a WantsToMove, the movement system reads every one, applies it and removes the message.

ability "query" "Moves" {
    with   = [WantsToMove]
    bind   = { want_move = WantsToMove }
    entity = message
    body   = [ability.if.CanMove, ability.despawn.Message]
}