Skip to content

Systems

Systems are persistent query callbacks that SIECS can schedule and run for you.

A system contains:

  • a name;
  • an entity query;
  • resource access metadata;
  • a callback;
  • a phase;
  • optional .after dependencies;
  • enabled/disabled state;
  • threading constraints;
  • deferred-operation policy;
  • an optional execution interval.

A system query is persistent. When the system runs, SIECS creates an iterator for the query and invokes the callback for every non-empty matching batch.

static void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, 0);
const Velocity *v = ecs_field(it, 1);
for (uint32_t i = 0; i < it->count; i++) {
p[i].x += v[i].x;
p[i].y += v[i].y;
}
}
ecs_system({
.name = "Move",
.phase = EcsOnUpdate,
.query.components = {
ecs_inout(Position),
ecs_in(Velocity),
},
.callback = Move,
});

The callback may run multiple times in one system execution because one query can match multiple tables.

That is expected. A system callback is a batch callback, not necessarily a once-per-frame callback.

If a system has no entity-matching terms, SIECS can invoke it with an empty iterator so it behaves like a task.

ecs_iter_t.delta_time contains the time delta for the system execution.

For a normal every-frame system, this is the frame delta.

static void Move(ecs_iter_t *it) {
Position *p = ecs_field(it, 0);
const Velocity *v = ecs_field(it, 1);
for (uint32_t i = 0; i < it->count; i++) {
p[i].x += v[i].x * it->delta_time;
p[i].y += v[i].y * it->delta_time;
}
}

For an interval system, delta_time is the accumulated time since that system’s previous automatic execution.

SIECS also exposes the built-in DeltaTime resource containing the current frame delta. These are not always identical for interval systems.

A system without an entity query can be used for work that runs once per system execution instead of once per matching entity.

Examples:

  • poll a platform API;
  • update a global resource;
  • begin or end a render pass;
  • pump networking;
  • flush an external queue.

A resource-only system is especially useful because its resource access can be used by the scheduler when building dependencies.

static void UpdateClock(ecs_iter_t *it) {
Clock *clock = ecs_get_resource(Clock);
clock->elapsed += it->delta_time;
}
ecs_system({
.name = "UpdateClock",
.phase = EcsPreUpdate,
.query.resources = {
{ .id = ecs_id(Clock), .access = EcsInOut },
},
.callback = UpdateClock,
});

SIECS phases provide broad ordering.

The built-in phase sequence includes startup phases followed by recurring load, update and render phases.

Typical recurring phases are:

Phase Typical use
EcsOnLoad Load or frame-input preparation.
EcsPostLoad Work after load/input.
EcsPreUpdate Prepare simulation state.
EcsOnUpdate Main simulation.
EcsPostUpdate Derived state and post-simulation work.
EcsPreRender Prepare render-visible data.
EcsOnRender Rendering-facing systems.
EcsPostRender Render cleanup.

Startup phases run only during the first progress cycle.

A system registered without a phase runs in EcsOnUpdate. ECS_PHASE_NONE is 0, so an omitted .phase field never selects a builtin phase by accident.

ecs_progress() runs enabled systems according to the phase plan.

while (ecs_progress()) {
/* application-controlled outer loop, if needed */
}

ecs_run() runs all enabled systems directly.

ecs_run_phase(phase) runs one phase immediately.

ecs_run_system(system) runs one enabled system immediately.

Forced phase/system execution ignores a system’s interval gate without advancing the interval accumulator as an automatic execution would.

Register a custom phase with ecs_phase_init() or ecs_phase(...).

A phase descriptor can place the new phase relative to existing phases with after and/or before. An omitted field means no constraint, so ecs_phase({ .name = "Physics", .after = EcsOnUpdate }) is a recurring phase placed after EcsOnUpdate. A custom phase without any constraint runs between EcsOnUpdate and EcsPostUpdate.

Custom phases are useful when a subsystem needs a stable scheduling boundary that should be visible to modules and tooling.

Do not create a new phase for every system. Fine-grained ordering inside a phase is better expressed with system dependencies.

Systems in the same phase can use .after dependencies.

ecs_system_id_t input = ecs_system({
.name = "Input",
.phase = EcsPreUpdate,
.callback = Input,
});
ecs_system({
.name = "Gameplay",
.phase = EcsPreUpdate,
.after = { input },
.callback = Gameplay,
});

The dependency states that Gameplay must observe work from Input before it runs in that phase.

SIECS also analyzes component/resource read-write access to build a dependency graph. A ready system can start as soon as its prerequisites finish; it does not wait for unrelated systems unless a command-publication boundary requires it.

Explicit .after is for semantic ordering that cannot be inferred only from data hazards.

Systems can start disabled:

ecs_system_id_t damage = ecs_system({
.name = "Damage",
.phase = EcsOnUpdate,
.disabled = true,
.callback = Damage,
});

Enable or disable later:

ecs_system_enable(damage);
ecs_system_disable(damage);

Disabled systems remain registered. Their query and metadata still exist, but the scheduler does not execute them.

Modules use the same mechanism when a module is disabled.

Structural mutation changes tables. Performing it immediately while a system is iterating table memory can invalidate the current batch.

SIECS therefore runs ordinary query-iterating systems inside a defer scope.

Conceptually:

begin system
begin defer
iterate matching tables
callback records structural commands
end defer
flush commands
end system

This lets code request operations such as add/remove/relate while iteration is active without immediately moving the row being processed.

Application code can use the same mechanism:

ecs_defer_begin();
ecs_add(e, Enemy);
ecs_remove(e, Sleeping);
ecs_relate(e, ChildOf, parent);
ecs_defer_end();

The outermost ecs_defer_end() flushes the command buffer.

Defer scopes can nest. Only the outermost end performs the flush.

Deferred operations target the final structural state rather than forcing every API call to become an immediate migration.

This is especially valuable for entity initialization and systems that perform several structural changes to the same entity.

The command buffer can combine compatible operations and migrate once to the final table.

A scheduler boundary that requires one system’s structural changes to be visible to another must include a command flush before the dependent system observes the world.

Phase/dependency planning provides these synchronization points.

This matters with worker threads: a system may finish its callback but its structural commands are not semantically visible to later dependent work until the scheduler reaches the appropriate synchronization point.

ecs_system_desc_t.no_defer disables the automatic defer scope.

This mode is intentionally restricted: a no_defer system cannot iterate an entity query.

Use it for task-like systems that must perform immediate ECS operations and do not hold query batch pointers.

It should not be used simply to make mutations “happen sooner” inside an entity loop.

World features can enable persistent worker threads:

ecs_init_w_features(&(ecs_world_feat_desc_t){
.worker_threads = ECS_WORKERS_AUTO,
});

The scheduler uses query component/resource access to determine which systems can run concurrently.

Two systems that only read the same data can be independent.

A writer conflicts with readers/writers of the same accessed component/resource.

Correct access declarations are therefore part of correctness, not only documentation.

A system has at most one active callback unless it explicitly enables parallel portions. Other independent systems may still execute at the same time.

ecs_system({
.name = "Move",
.query.components = { ecs_inout(Position), ecs_in(Velocity) },
.callback = Move,
.parallel = true,
.chunk_size = 1024,
});

Zero selects 1,024 rows per portion. Portions stay within a single table and each matching row is processed exactly once. A short final portion is expected. The iterator’s entities, owned fields and dense/depth relation views all start at that portion’s first row. Shared fields keep their shared pointer.

Callbacks for different portions can execute simultaneously. Captures, user data, custom parameter hooks and component lifecycle operations used by callbacks must support that concurrency. Register all component, resource and relation types before starting parallel work. Accesses outside the declared query/resource set remain the application’s responsibility.

Parallel portions require an entity query with read-only resources. Combining them with ordered queries, main_thread_only, or no_defer is rejected. Writable global resources need a separate system; there is no automatic reduction.

With no workers, the same logical portions execute sequentially. Manual ecs_run_system / system_ref::run() executes synchronously on the calling thread, using those same portions.

Data conflicts preserve registration order. Every .after dependency is retained, and a successor waits for all portions of each prerequisite. A prerequisite with no pending commands can release its successors immediately, including from a worker while the calling thread is busy with another system.

Deferred commands are published in system registration order, then query table order and portion order, independently of which worker ran them. Publication waits for all already-started systems to finish their portions; new systems pause during that drain. Hooks and observers produced by publication run on the calling thread, and their deferred tail finishes before the next system is published. An earlier unfinished system can therefore delay publication of a later one.

After publication, dependencies for systems that have not started are rechecked. The access graph is cached and rebuilt when registration, matching tables or field ownership changes. Systems already executed are not repeated. Exclusive main-thread systems also trigger revalidation when their immediate mutations change the plan. Independent systems have no structural visibility guarantee before a publication boundary; use .after when that visibility matters.

ecs_new() and ecs_new_no_reuse() reserve live handles during parallel callbacks. Those entities enter query-visible tables at publication. Adding components, relating or destroying a reserved entity records deferred commands. Entity handle allocation can vary between runs even though command-publication order is stable. Workers reserve IDs in blocks of 256 and consume them locally. Unissued IDs stay dead and are returned at synchronization. Entity lookup and reservation use no index mutex; allocating a new record page can still enter the memory allocator. Records occupy 8 bytes in stable pages of 65,536 records, with a single page directory (512 KiB on a 64-bit target). Scheduler claims can group up to four portions while retaining separate callbacks and command buffers for each portion. This is not a guarantee of deterministic external side effects or undeclared writes from callbacks.

Global registration, query/observer configuration, serialization, and nested scheduler execution are forbidden in parallel callbacks. Nested scheduler execution is also rejected in exclusive callbacks. Run global operations in exclusive main-thread systems or outside scheduled work. Access to the world from unrelated external threads is unsupported.

Some work must stay on the calling thread:

  • graphics APIs with thread affinity;
  • platform event handling;
  • UI toolkits;
  • external libraries with main-thread restrictions.

Mark those systems:

ecs_system({
.name = "Render",
.phase = EcsOnRender,
.main_thread_only = true,
.callback = Render,
});

The C++ .main_thread() option restricts execution to the main thread. .no_defer() separately controls the outer mutation scope of resource-only systems. Neither option runs a system when it is registered. The returned system_ref is non-owning and provides .run(), .enable(), .disable(), and .id(); .after(other_system) accepts another system handle.

While the parallel scheduler is running, structural changes are deferred into execution-context command buffers.

Do not pass raw field pointers from one worker/system to another.

Use entity handles and ECS synchronization boundaries to communicate structural results.

A system can specify an automatic interval:

ecs_system({
.name = "Spawn",
.phase = EcsOnUpdate,
.interval = 0.5,
.callback = Spawn,
});

A positive interval waits until enough frame time has accumulated, then runs the system once and restarts its accumulator.

SIECS does not perform catch-up execution by repeatedly invoking the system for every missed interval.

This prevents a long frame from creating an unbounded burst of timer callbacks.

SIECS does not currently expose rate filters or nested tick-source graphs. Use interval, explicit resources or application systems when more complex timing relationships are needed.

Prefer systems that:

  • have a focused data transformation;
  • declare the smallest accurate read/write set;
  • iterate contiguous component fields;
  • use resources for world-level state;
  • leave structural mutation deferred;
  • use phases for broad ordering;
  • use .after only for real semantic dependencies.

Avoid using systems as arbitrary object-method containers. The main value of a system is that its data dependencies and iteration domain are explicit.

SIECS builds execution plans from system metadata.

The most important dependency inputs are:

  • phase;
  • explicit .after system ids;
  • component access modes;
  • resource access modes;
  • main_thread_only.

Two systems that only read the same component/resource do not create a data hazard through that item.

They can run concurrently if no other dependency prevents it.

A writer must not overlap a reader of the same data.

The scheduler creates an ordering/synchronization boundary.

Two writers to the same component/resource conflict.

For owned component fields, the scheduler checks whether the queries match any of the same tables. Writers on disjoint tables can run concurrently. Shared inherited and up reads are treated conservatively against writers of the same component because their source may lie outside the matching tables. Resource writes conflict regardless of query tables.

If two systems truly operate on disjoint structural populations, the scheduler still reasons conservatively from declared access/query overlap and its internal table analysis.

SIECS can compare cached query table sets when deciding whether two systems’ entity data could overlap.

This provides more information than component ids alone.

For example, two systems that write Position but match disjoint table sets may be schedulable differently from two writers that share tables.

Application code should still declare truthful access and not rely on incidental current table populations for correctness.

The scheduler has a phase graph/order and builds a plan of systems for each phase.

Registering/enabling/disabling systems can require plan rebuild/refresh.

Avoid creating and destroying scheduling metadata continuously during the hot frame loop unless the feature genuinely needs dynamic scheduling.

Modules can toggle groups of systems; that is expected but still not free.

SIECS has startup-oriented phases before recurring simulation phases.

Startup phases are one-shot.

They are useful for work that depends on the world being initialized but should not run every frame.

An interval value does not turn a startup phase into a recurring timer; startup phase semantics take precedence.

ecs_run_system(system) and ecs_run_phase(phase) are explicit calls.

They are useful for:

  • tests;
  • editor commands;
  • controlled simulation steps;
  • manually advancing one subsystem.

Forced execution ignores the automatic interval gate.

It does not pretend the elapsed interval timer fired naturally, so use it as an explicit command rather than as a way to manipulate timer accumulation.

ecs_system_desc_t can carry user_data and a destructor.

Use user data for system-local configuration/state that is truly private to the callback registration.

Prefer a resource when the state should be visible as an explicit world dependency.

Prefer a component when the state is per entity.

The user-data destructor lets registration-owned allocations be released at system/world teardown.

A system can have:

  • entity terms;
  • resource terms;
  • both;
  • neither.

Runs per matching table batch.

Runs per matching table batch and declares global dependency access.

Behaves like a task but participates in resource dependency scheduling.

A pure scheduled task. Ordering comes from phase/explicit dependencies and threading flags.

Choose the narrowest accurate descriptor.

static void Promote(ecs_iter_t *it) {
const Experience *xp = ecs_field(it, 0);
for (uint32_t i = 0; i < it->count; i++) {
if (xp[i].value >= 100) {
ecs_add(it->entities[i], Veteran);
}
}
}

Normal system execution records the structural adds and applies them safely after the relevant iteration/sync boundary.

if (health[i].value <= 0) {
ecs_kill(it->entities[i]);
}

Do not continue using field pointers for that entity as if the row disappeared immediately. The deferred model means the command is pending until flush.

Write system code around final intended state, not immediate row movement.

When deferred commands flush, component/relation hooks and observers execute according to the applied transitions.

This means event timing can be later than the API call that recorded the command.

If logic needs immediate synchronous behavior, reconsider whether it belongs inside an iterating system or whether a task/no-defer path is more appropriate.

Each logical system portion owns its command/defer context independently of the worker that executes it. C++ SystemParam initialization and world hooks run once per portion; table and row hooks receive its bounded view. A system without .parallel() keeps one parameter state for its whole query execution.

Do not share mutable callback-local scratch buffers between systems/workers without synchronization.

For per-system scratch state, use:

  • stack/local allocations;
  • immutable shared state;
  • properly synchronized user data;
  • resources with declared scheduler access.

ecs_world_feat_desc_t.target_fps allows SIECS to pace ecs_progress() toward a target frame rate.

This is a convenience for simple loops.

Applications with their own platform/render loop can leave frame pacing outside the ECS and still run phases/systems manually.

Frame pacing is not a simulation fixed-step solution by itself.

For deterministic fixed-step simulation, maintain an accumulator in application or resource state and run the desired simulation phase/system explicitly at the fixed step.

A positive system interval accumulates frame time.

When the threshold is reached, the system runs once.

SIECS intentionally does not run the callback N times to “catch up” all elapsed intervals.

This design bounds per-frame timer work.

If exact catch-up simulation is required, implement explicit accumulator logic rather than relying on a convenience interval.

EcsPreUpdate
PollInput (main thread)
↓ after
BuildCommands
EcsOnUpdate
Gameplay systems
EcsOnUpdate
simulation
EcsPostUpdate
derived transforms
EcsPreRender
build render data
EcsOnRender
submit render work (main thread)

Systems with disjoint query/resource writes can run in parallel when workers are enabled.

The goal is not maximum theoretical parallelism at any cost; it is correct parallelism derived from explicit data access.

A system reads/writes a global C variable without declaring it as a resource.

The scheduler cannot see the dependency.

Move schedulable shared state into a resource or force appropriate explicit ordering/thread affinity.

Declaring EcsIn and then mutating the component breaks dependency analysis.

System registration order is not a robust substitute for .after or phase ordering.

A phase boundary can reduce scheduler flexibility and add synchronization.

Use phases for meaningful stages, not every function.

If high-volume work can batch naturally, put it in a system.

Immediate structural changes in table iteration

Section titled “Immediate structural changes in table iteration”

Use default defer behavior. no_defer intentionally cannot be combined with entity query iteration.