Skip to content

Cookbook

ecs_init();
ecs_entity_t e = ecs_new();
ecs_fini();
ecs_entity_t e = ecs_new();
ecs_set(e, Position, {0, 0});
ecs_set(e, Velocity, {1, 0});
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);
ecs_query_id_t q = ecs_query({
.components = {
ecs_inout(Position),
ecs_in_optional(Velocity),
},
});

The optional field is NULL for a batch whose table lacks the component.

.components = {
ecs_in(Position),
ecs_not(Frozen),
}
ecs_entity_t parent = ecs_new();
ecs_entity_t child = ecs_new();
ecs_relate(child, ChildOf, parent);

For a ByTarget relation:

.relations = {
ecs_to(GroupOf, group),
}

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.

ecs_set(instance, Material, local_material);

The instance now has local storage for that component.

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

Enable it:

ecs_remove(e, Disabled);
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({
.name = "Spawner",
.phase = EcsOnUpdate,
.interval = 0.5,
.callback = Spawn,
});
ecs_observer({
.on = EcsOnSet,
.query.components = { ecs_in(Position) },
.callback = OnPositionSet,
});
ecs_event_t Hit = ecs_event();
ecs_observer({
.on = Hit,
.callback = OnHit,
});
HitPayload payload = { .damage = 10 };
ecs_observer_trigger(target, Hit, &payload);
ecs_save("level.siecs");
ecs_load("level.siecs");
ecs_query_id_t q = ecs_query({
.components = {
ecs_in(Position),
ecs_filter(Disabled),
},
});

Mentioning Disabled explicitly allows that population to match.

ecs_query_id_t q = ecs_query({
.components = {
ecs_in(Health),
},
.is_a = enemy_base,
});

For an acyclic ByTarget hierarchy relation named SettingsParent (the built-in ChildOf relation uses ByDepth storage):

.components = {
ecs_in(LocalTransform),
ecs_up(WorldSettings, SettingsParent),
}
.order_by = ecs_order_by_depth(ChildOf)

Use when parents/lower depths must be processed before descendants.

ecs_system({
.name = "Render",
.phase = EcsOnRender,
.main_thread_only = true,
.callback = Render,
});
ecs_system_id_t build = ecs_system({
.name = "Build",
.phase = EcsPostUpdate,
.callback = Build,
});
ecs_system({
.name = "Consume",
.phase = EcsPostUpdate,
.after = { build },
.callback = Consume,
});

Use only for a task without entity query iteration:

ecs_system({
.name = "ImmediateTask",
.phase = EcsPostUpdate,
.no_defer = true,
.callback = ImmediateTask,
});
ecs_entity_t tool_entity = ecs_new_no_reuse();

Use when runtime numeric allocation order is needed; do not treat the index as a persistent cross-session id.

if (ecs_scene_validate(data, size)) {
ecs_load_memory(data, size);
}