Documentation
¶
Overview ¶
Package system runs game logic ("systems") against an ecs.World.
A system is a plain function. A Schedule groups systems into ordered stages and applies deferred structural changes (Commands) between stages, so systems can queue spawns and despawns while iterating a view.
The package is deliberately separate from package ecs: the World knows nothing about systems, and nothing here knows about rendering or any game framework. A host loop (a game's update, a server tick, a test) owns the timing and calls Schedule.Run.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Commands ¶
type Commands struct {
// contains filtered or unexported fields
}
Commands buffers structural World changes so they can be applied after the systems that requested them have finished iterating. A Commands value is a World resource: get it with CommandsOf. Schedule.Run applies it after every stage.
func CommandsOf ¶
CommandsOf returns the World's Commands buffer, creating and registering it as a resource on first use.
Panics with "system: nil world" if w is nil.
func (*Commands) Apply ¶
Apply runs the queued commands in the order they were queued, then empties the buffer. Commands queued by a command (via Do) while Apply runs are applied in the same call. The backing slice is reused, so steady-state Apply does not allocate.
Apply must not be called while a view is iterating.
func (*Commands) Despawn ¶
Despawn queues w.DeleteEntity(e). If e is no longer alive when the command is applied (for example it was despawned twice in one stage) the command is skipped rather than panicking.
type Context ¶
type Context struct {
// World is the World the schedule runs against.
World *ecs.World
// Time describes the current tick.
Time Time
// Commands is the World's deferred-change buffer, the same one
// [CommandsOf] returns. The Schedule applies it after every stage.
Commands *Commands
}
Context is what a Schedule hands to every system call. It carries the World and the per-tick data a system commonly needs, and is the place to add more of it later without changing the System signature.
Rule for new fields: only data that is owned and refreshed by the Schedule each tick belongs here. State owned by the game (settings, score, input devices) lives in World resources.
Lifetime: the Schedule reuses one Context for all of its calls, which is what keeps Run free of allocations. The pointer is valid only for the duration of the call it is passed to. Systems must not keep it, or its Commands or Time, after returning; keep ctx.World if the World is needed later.
type EventReader ¶
type EventReader[T any] struct { // contains filtered or unexported fields }
EventReader reads a World's T events. Each reader sees each event at most once; several readers of the same type each see every event. A reader that does not read for more than one tick misses the dropped events silently. Keep one reader per system, created in its Init.
func NewEventReader ¶
func NewEventReader[T any](w *ecs.World) *EventReader[T]
NewEventReader returns a reader of w's T events, creating the queue if needed. It starts with the oldest event still in the queue.
Panics with "system: nil world" if w is nil.
func (*EventReader[T]) Read ¶
func (r *EventReader[T]) Read() iter.Seq[T]
Read yields the events this reader has not seen yet, oldest first. An event counts as read once it is yielded, so breaking out of the loop leaves the rest for the next Read. Events sent while the loop runs are yielded too. Read does not allocate.
type Events ¶
type Events[T any] struct { // contains filtered or unexported fields }
Events is a World's queue of events of type T: systems Events.Send values and others read them with an EventReader. It is a World resource: get it with EventsOf.
An event can be read until the end of the tick after the one it was sent in; then it is dropped. Schedule.Run ages every queue of the World once, after its last stage. So a reader that runs after the sender sees the event in the same tick, and one that runs before it in the next tick. Events sent between Runs (setup code, a callback) count as part of the next tick.
Events are plain values: an ecs.Entity in an event may no longer be alive when a reader sees it. Events are never saved.
func EventsOf ¶
EventsOf returns the World's queue of T events, creating and registering it as a resource on first use.
Panics with "system: nil world" if w is nil.
type Initializer ¶
type Initializer interface {
Init(ctx *Context)
}
Initializer is an optional interface for a Runner that needs one-time setup against the World: setting resources, queueing initial spawns with ctx.Commands, or checking that a required resource exists. A Schedule calls Init once per runner, at the start of the first Schedule.Run that follows adding it, before any stage runs. ctx.Time describes that first tick. Plain System functions cannot implement it.
type Runner ¶
type Runner interface {
Run(ctx *Context)
}
Runner is anything a Schedule can run. Use it for systems that keep state (prebuilt views, scratch buffers, configuration) in a struct:
type Movement struct{ view ecs.View2[Position, Velocity] }
func (m *Movement) Run(ctx *system.Context) { ... }
A System is itself a Runner, so plain functions and structs mix freely.
type Schedule ¶
type Schedule struct {
// contains filtered or unexported fields
}
Schedule runs systems in a fixed order, grouped into named stages. The zero value is an empty schedule ready to use. A Schedule is not safe for concurrent use.
Example ¶
A movement system plus a cleanup stage. The host loop (here a for loop, in a game an Update method) owns the timing.
w := ecs.NewWorld()
ecs.NewEntity2(w, Position{X: 0}, Velocity{X: 2})
ecs.NewEntity2(w, Position{X: 9}, Velocity{X: 2})
view := ecs.NewView2[Position, Velocity](w)
var s system.Schedule
s.AddStage("move", func(ctx *system.Context) {
for _, c := range view.Each() {
p, v := c.Unpack()
p.X += v.X * int(ctx.Time.Delta/time.Second)
}
})
s.AddStage("cleanup", func(ctx *system.Context) {
cmds := ctx.Commands
for e, c := range view.Each() {
if p, _ := c.Unpack(); p.X > 10 {
cmds.Despawn(e) // deferred: safe while iterating
}
}
})
for range 3 {
s.Run(w, time.Second)
}
n := 0
for range view.Each() {
n++
}
fmt.Println(n)
Output: 1
func (*Schedule) Add ¶
Add appends sys to the most recently added stage, creating a stage named "default" if the schedule has none. Systems run in the order added.
Panics with "system: nil system" if any system is nil.
func (*Schedule) AddRunner ¶
AddRunner is like Schedule.Add for stateful systems implemented as a Runner. The Runner is called as-is, so it keeps its own state between ticks. If it implements Initializer, its Init runs at the start of the next Schedule.Run.
Panics with "system: nil system" if any runner is nil.
func (*Schedule) AddStage ¶
AddStage appends a stage named name holding sys, and makes it the stage that later Schedule.Add calls extend. Stages run in the order they were added.
Panics with "system: duplicate stage: <name>" if a stage of that name exists and with "system: nil system" if any system is nil.
func (*Schedule) Run ¶
Run executes one tick: every stage in order, with delta recorded as ctx.Time.Delta and the tick index as ctx.Time.Tick. After each stage it applies the World's Commands, so changes queued by one stage are visible to the next. Before the first stage it calls Init on runners added since the last Run (see Initializer), in the order they were added, and applies the commands they queued. After the last stage it ages the World's Events queues, dropping the events of the previous tick. Run makes no allocations in steady state.
Panics with "system: nil world" if w is nil, and with "system: schedule used with a different world" if w is not the World of the schedule's first Run.
type System ¶
type System func(ctx *Context)
System is one unit of game logic. It runs on the World's goroutine and may iterate views, read and write components and use resources. Structural changes made while a view is iterating panic in package ecs, so systems queue them with ctx.Commands instead. See Context for the lifetime of ctx.