As a program grows, one long list of abilities gets hard to follow. The StructOp handbook offers two ways to give a bigger program its shape: entities and systems, and classes composed through interfaces.
This post accompanies episode 9 of StructOp from zero. Last time we built a real app; this time we build one tiny dungeon (a hero, a goblin and an orc) both ways, and run each version for real. Every terminal block below is a real StructOp 0.1.0 session.
Part 1: entities, components and systems
The component model splits a program three ways:
- an entity is just a number;
- a component holds one part of its state, such as a name or health;
- a system is a function that runs over every entity that has the components it needs.
Components and spawn
Components are declared like structs. A component without fields is a tag: here Player marks the hero.
ability "component" "Name" { fields = [{ name = "label", type = String }] } ability "component" "Health" { fields = [{ name = "current", type = Int }, { name = "max", type = Int }] } ability "component" "Player" { } spawn creates an entity with the components it starts with. The hero gets the tag; the goblin and the orc do not.
ability "spawn" "Hero" { components = { Player = {}, Name = { label = "Hero" }, Health = { current = 10, max = 10 }, } } ability "spawn" "Goblin" { components = { Name = { label = "Goblin" }, Health = { current = 4, max = 4 }, } } (The orc is the same, with 9 health.)
Queries and systems
A query runs its body once for every live entity that has all the components in with and none of those in without. bind names the current entity's components. Because bind adds a local to the enclosing function, a query that binds sits inside a function, and that function is the system:
ability "function" "poison_system" { body = [ability.query.Poisoned] } ability "query" "Poisoned" { with = [Health] without = [Player] bind = { hp = Health } body = [ability.set.Hurt] } ability "set" "Hurt" { target = hp.current value = hp.current - 3 } Two more systems: a reaper that reads each entity's number with entity and despawns anything whose health has reached zero, and a status system that prints every survivor. The entity number goes into a local that the system function declares:
ability "function" "reaper_system" { locals = [{ name = "id", type = Int }] body = [ability.query.Fallen] } ability "query" "Fallen" { with = [Name, Health] bind = { name = Name, hp = Health } entity = id body = [ability.if.Dead] } ability "if" "Dead" { condition = hp.current <= 0 then = [ability.print.Falls, ability.despawn.Body] } ability "despawn" "Body" { entity = id depends_on = [ability.print.Falls] } Systems are ordinary functions, and a schedule is a function that calls them in order. Our turn prints its number, then runs poison, the reaper and status; the program spawns the three creatures and plays two turns.
ability "function" "turn" { params = [{ name = "n", type = Int }] body = [ability.print.Turn, ability.call.Poison, ability.call.Reap, ability.call.Status] } Run it
$ structop validate Success! The configuration is valid. $ structop apply ... Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Turn 1 #0 Hero: 10/10 #1 Goblin: 1/4 #2 Orc: 6/9 Turn 2 Goblin falls #0 Hero: 10/10 #2 Orc: 3/9 Switch intent.st to lang = "Rust" and the same declarations print exactly the same two turns:
$ structop plan # program.app will be replaced ~ program.app (.structop/build/app) compile: cc -std=c11 -> cargo build --release (edition 2021) providers: StructOp/c 2.3.0 -> StructOp/rust 2.3.0 ... Plan: 0 to add, 1 to change, 0 to destroy. Part 2: classes and interfaces
Now the same dungeon as objects. Instead of spreading a creature across components, a class keeps its fields and its methods together.
A class is a struct with methods. Its methods are ordinary functions the class lists; each name starts with the class name and an underscore (hero_hurt is the method hurt), and inside a method self is the object. Methods use only self, their parameters and their locals, never file-scope state; anything else you pass in as a parameter.
ability "class" "Hero" { fields = [{ name = "hp", type = Int }, { name = "armour", type = Int }] methods = [ability.function.hero_hurt, ability.function.hero_health, ability.function.hero_rest] implements = [ability.interface.Fighter] } ability "function" "hero_hurt" { params = [{ name = "by", type = Int }] body = [ability.set.HeroHurt] } ability "set" "HeroHurt" { target = self.hp value = self.hp - max(by - self.armour, 0) } ability "function" "hero_health" { returns = Int return = self.hp } An interface names methods that several classes share. Hero and Goblin both implement Fighter, which promises every method with the same parameter and return types; structop validate checks it.
ability "interface" "Fighter" { methods = [{ name = "hurt", params = [{ name = "by", type = Int }] }, { name = "health", returns = Int }] } Methods run through a call ability. An interface is only ever the type of a function parameter, so strike takes any fighter, and function = f.hurt runs the method of whichever class the object belongs to. A method can also be called through its object: ability.value.hero.rest.
ability "function" "strike" { params = [{ name = "f", type = Fighter }, { name = "who", type = String }] locals = [{ name = "left", type = Int }] body = [ability.call.Hit, ability.call.Check, ability.print.Left] } ability "call" "Hit" { function = f.hurt args = [3] } ability "call" "Check" { function = f.health result = left } ability "call" "Rest" { function = ability.value.hero.rest depends_on = [ability.call.StrikeGoblin] } Run it, then break the promise
$ structop apply ... Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Hero has 9 left Goblin has 1 left Hero has 10 left The hero's armour soaks two of the three points; after a rest (+2) the hero takes the next hit at eleven and is back to ten. Remove the goblin's health method and validate stops before any code is generated:
$ structop validate Error: class "Goblin" implements "Fighter" but has no method "health" on classes.st line 42, column 19 structop graph draws a call through an object to the method's function, and a call through an interface parameter to the interface. Like the dungeon, the classes build as C and as Rust from the same files.
Which one?
The handbook's dungeon crawler game uses entities and systems, for many creatures that share components. Classes keep one object's data and behaviour together, and compose through interfaces rather than inheritance. Either way, StructOp still sees declarations: checked, planned, drawn and built like everything else.
Next episode is the season finale: humans and AI on one representation.
Handbook pages used: Entities and systems · Classes and interfaces · Language overview





