Cookbook
Minimal world
Section titled “Minimal world”ecs_init();ecs_entity_t e = ecs_new();ecs_fini();ecs::init();auto e = ecs::spawn();ecs::fini();Create a moving entity
Section titled “Create a moving entity”ecs_entity_t e = ecs_new();ecs_set(e, Position, {0, 0});ecs_set(e, Velocity, {1, 0});auto e = ecs::spawn() .set(Position{0, 0}, Velocity{1, 0});Iterate Position + Velocity
Section titled “Iterate Position + Velocity”ecs_query_id_t q = ecs_query({ .components = { ecs_inout(Position), ecs_in(Velocity), },});
ecs_iter_t it = ecs_query_iter(q);while (ecs_iter_next(&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_query_fini(q);auto q = ecs::query().build();q.each([](Position &p, const Velocity &v) { p.x += v.x; p.y += v.y;});Optional component
Section titled “Optional component”ecs_query_id_t q = ecs_query({ .components = { ecs_inout(Position), ecs_in_optional(Velocity), },});ecs::query().each([](Position &p, ecs::optional<const Velocity> v) { if (v) { p.x += v->x; }});The optional field is NULL for a batch whose table lacks the component.
Exclude a tag
Section titled “Exclude a tag”.components = { ecs_in(Position), ecs_not(Frozen),}ecs::query().without<Frozen>();Parent and child
Section titled “Parent and child”ecs_entity_t parent = ecs_new();ecs_entity_t child = ecs_new();
ecs_relate(child, ChildOf, parent);auto parent = ecs::spawn();auto child = ecs::spawn().child_of(parent);Query one relation target
Section titled “Query one relation target”For a ByTarget relation:
.relations = { ecs_to(GroupOf, group),}ecs::query().with<GroupOf>(group);Shared inherited configuration
Section titled “Shared inherited configuration”Register a component with shared inheritance, set it on an abstract base, then
create instances with ecs_is_a().
Read-only queries can receive the base value as EcsFieldShared.
Override inherited data
Section titled “Override inherited data”ecs_set(instance, Material, local_material);instance.set(local_material);The instance now has local storage for that component.
Defer structural mutation
Section titled “Defer structural mutation”ecs_defer_begin();
ecs_add(e, Enemy);ecs_remove(e, Sleeping);ecs_relate(e, ChildOf, parent);
ecs_defer_end();// In a typed system callback, SIECS defers structural changes automatically.ecs::system("Wake") .each([parent](ecs::entity entity) { entity.add<Enemy>().remove<Sleeping>().child_of(parent); });Disable an entity
Section titled “Disable an entity”ecs_add(e, Disabled);e.disable();Enable it:
ecs_remove(e, Disabled);e.enable();System with resource dependency
Section titled “System with resource dependency”ecs_system({ .name = "Move", .phase = EcsOnUpdate, .query = { .components = { ecs_inout(Position), ecs_in(Velocity), }, .resources = { { .id = ecs_id(Time), .access = EcsIn }, }, }, .callback = Move,});ecs::system("Move") .phase(EcsOnUpdate) .each([](Position &p, const Velocity &v, ecs::res<const Time> time) { p.x += v.x * time->dt; p.y += v.y * time->dt; });Run every half second
Section titled “Run every half second”ecs_system({ .name = "Spawner", .phase = EcsOnUpdate, .interval = 0.5, .callback = Spawn,});ecs::system("Spawner") .phase(EcsOnUpdate) .interval(0.5) .each([] {});React to a set
Section titled “React to a set”ecs_observer({ .on = EcsOnSet, .query.components = { ecs_in(Position) }, .callback = OnPositionSet,});ecs::observe<ecs::OnSet>() .each([](const Position &) {});Custom event
Section titled “Custom event”ecs_event_t Hit = ecs_event();
ecs_observer({ .on = Hit, .callback = OnHit,});
HitPayload payload = { .damage = 10 };ecs_observer_trigger(target, Hit, &payload);struct Hit { int damage; };
ecs::observe<Hit>() .each([](ecs::event<Hit> event) { const int damage = event->damage; (void)damage; });
ecs::emit(target, Hit{ .damage = 10 });Save and reload scene
Section titled “Save and reload scene”ecs_save("level.siecs");ecs_load("level.siecs");// Scene serialization currently uses the C-compatible scene API in C++ too.ecs_save("level.siecs");ecs_load("level.siecs");Query disabled entities
Section titled “Query disabled entities”ecs_query_id_t q = ecs_query({ .components = { ecs_in(Position), ecs_filter(Disabled), },});ecs::query().with<Disabled>();Mentioning Disabled explicitly allows that population to match.
Query descendants of a base
Section titled “Query descendants of a base”ecs_query_id_t q = ecs_query({ .components = { ecs_in(Health), }, .is_a = enemy_base,});auto q = ecs::query() .with<Health>() .is_a(enemy_base) .build();Read an ancestor component
Section titled “Read an ancestor component”For an acyclic ByTarget hierarchy relation named SettingsParent (the
built-in ChildOf relation uses ByDepth storage):
.components = { ecs_in(LocalTransform), ecs_up(WorldSettings, SettingsParent),}ecs::query() .with<LocalTransform>() .up<WorldSettings, SettingsParent>();Order by hierarchy depth
Section titled “Order by hierarchy depth”.order_by = ecs_order_by_depth(ChildOf)ecs::query().order_by_depth<ChildOf>();Use when parents/lower depths must be processed before descendants.
Main-thread render system
Section titled “Main-thread render system”ecs_system({ .name = "Render", .phase = EcsOnRender, .main_thread_only = true, .callback = Render,});ecs::system("Render") .phase(EcsOnRender) .main_thread() .each([] {});Explicit system dependency
Section titled “Explicit system dependency”ecs_system_id_t build = ecs_system({ .name = "Build", .phase = EcsPostUpdate, .callback = Build,});
ecs_system({ .name = "Consume", .phase = EcsPostUpdate, .after = { build }, .callback = Consume,});auto build = ecs::system("Build") .phase(EcsPostUpdate) .each([] {});
ecs::system("Consume") .phase(EcsPostUpdate) .after(build) .each([] {});No-defer task
Section titled “No-defer task”Use only for a task without entity query iteration:
ecs_system({ .name = "ImmediateTask", .phase = EcsPostUpdate, .no_defer = true, .callback = ImmediateTask,});// The typed builder has no no-defer mode; task callbacks use normal scheduling.ecs::system("ImmediateTask") .phase(EcsPostUpdate) .each([] {});Monotonic entity allocation
Section titled “Monotonic entity allocation”ecs_entity_t tool_entity = ecs_new_no_reuse();auto tool_entity = ecs::spawn_no_reuse();Use when runtime numeric allocation order is needed; do not treat the index as a persistent cross-session id.
Validate a scene before loading
Section titled “Validate a scene before loading”if (ecs_scene_validate(data, size)) { ecs_load_memory(data, size);}if (!ecs_scene_validate(data, size)) { throw std::runtime_error("invalid scene data");}if (!ecs_load_memory(data, size)) { throw std::runtime_error("scene loading failed");}
