Relationships
Definitions
Section titled “Definitions”A relation connects a source entity to a target entity.
SIECS relations are registered in a dedicated relation namespace.
A relation has a storage policy and deletion policy.
Key terms:
| Term | Meaning |
|---|---|
| Source | Entity that owns the relation edge. |
| Target | Entity referenced by the edge. |
| Relation id | ecs_relation_id_t identifying the edge kind. |
| Dense | Per-source target stored alongside source rows, plus a source index on the target. |
| ByDepth | Dense relation with maintained depth information. |
| ByTarget | Sources are grouped into tables per target; no per-target source list. |
SIECS relations have a dedicated id space. The relation type and target are represented through SIECS’s relation-specific API and table metadata.
Examples
Section titled “Examples”Create a parent/child edge:
ecs_relate(child, ChildOf, parent);child.child_of(parent);Read the current target:
ecs_entity_t parent = ecs_target(child, ChildOf);ecs::entity parent = child.target<ChildOf>();Remove the edge:
ecs_unrelate(child, ChildOf);child.unrelate<ChildOf>();Custom relation:
ECS_RELATION(GroupOf, { .storage = EcsRelationByTarget, .on_delete_target = EcsRemoveRelation, .acyclic = false,});
ECS_RELATION_REGISTER(GroupOf);
ecs_relate(member, GroupOf, group);struct GroupOf {};
ecs::relation<GroupOf>({ .storage = EcsRelationByTarget, .on_delete_target = EcsRemoveRelation, .acyclic = false,});
member.relate<GroupOf>(group);Introduction
Section titled “Introduction”Relations model data that is naturally an edge rather than a value local to one entity.
Examples:
- parent of an entity;
- group membership;
- owner;
- selected target;
- abstract base;
- scene/layer membership.
A raw ecs_entity_t component can also store a reference, but a registered
relation gives the ECS additional structural knowledge:
- query filtering by relation;
- target grouping or depth indexing;
- deletion policy;
- acyclic validation;
- observer relation events;
- built-in inheritance/hierarchy behavior.
Relationship queries
Section titled “Relationship queries”Test if entity has a relation
Section titled “Test if entity has a relation”if (ecs_has_relation(entity, GroupOf)) { /* entity has a GroupOf target */}if (entity.has_relation<GroupOf>()) { // entity has a GroupOf target}The id-based form accepts an ecs_relation_id_t.
Get target for entity
Section titled “Get target for entity”ecs_entity_t group = ecs_target(entity, GroupOf);ecs::entity group = entity.target<GroupOf>();The target is 0 when the relation is not present.
Find sources for a target
Section titled “Find sources for a target”Use this when the common question is “which entities point to this target?”
Dense and ByDepth relations keep a source index on the target:
ecs_relation_sources(target, R) returns the direct sources in O(1). The view is
borrowed until the relation changes, and its order is not stable.
ByTarget relations keep no per-target source list (ecs_relation_sources
returns an empty view). Their sources share tables per target, so query them with
ecs_to(R, target).
Find all entities with a relation
Section titled “Find all entities with a relation”Use a query relation term:
ecs_query_id_t q = ecs_query({ .relations = { ecs_rel(GroupOf) },});auto q = ecs::query() .with_relation<GroupOf>() .build();This matches sources that currently have the relation.
Find all entities with a specific target
Section titled “Find all entities with a specific target”For ByTarget:
ecs_query_id_t q = ecs_query({ .relations = { ecs_to(GroupOf, group) },});auto q = ecs::query() .with<GroupOf>(group) .build();Matching tables are grouped around the target, so iteration can retrieve the shared target without a per-row lookup.
Iterate relation targets
Section titled “Iterate relation targets”For Dense or ByDepth relation storage:
ecs_iter_t it = ecs_query_iter(q);while (ecs_iter_next(&it)) { const ecs_relation_target_t *targets = ecs_targets(&it, GroupOf);
for (uint32_t i = 0; i < it.count; i++) { ecs_entity_t target = targets[i].entity; }}q.each([](ecs::entity source) { ecs::entity target = source.target<GroupOf>(); (void)target;});For ByTarget:
ecs_entity_t target = ecs_target_shared(&it, GroupOf);q.each([](ecs::entity source) { ecs::entity target = source.target<GroupOf>(); (void)target;});Optional and excluded relations
Section titled “Optional and excluded relations”Optional relation:
ecs_rel_opt(GroupOf)ecs::query().each([](ecs::entity entity) { if (entity.has_relation<GroupOf>()) { // handle the optional relation }});Excluded relation:
ecs_not_rel(GroupOf)ecs::query().each([](ecs::entity entity) { if (!entity.has_relation<GroupOf>()) { // handle entities without GroupOf }});These are relation filters and do not create component field slots. The typed C++ builder currently has required-target methods only; its examples therefore test relation presence on the typed entity handle. Prefer the C relation terms for a large query where optional/excluded relation filtering must remain part of table matching.
Relation storage modes
Section titled “Relation storage modes”EcsRelationDense stores a target per source row.
Use it when:
- sources can point to many different targets in the same archetype population;
- iteration commonly needs each row’s target;
- exact-target filtering is not the dominant operation.
ByDepth
Section titled “ByDepth”EcsRelationByDepth is intended for acyclic hierarchical relations where depth
is useful.
It supports:
- per-source target access;
- maintained depth;
ecs_depth(Relation, depth)query filters;ecs_order_by_depth(Relation)table ordering.
This is a natural choice for hierarchy-like relations.
ByTarget
Section titled “ByTarget”EcsRelationByTarget groups sources around target values.
Use it when:
- exact-target queries are common;
- many entities share one target;
- upward traversal through the relation is important;
- grouping by target improves the main workload.
It is required by exact-target query terms and SIECS upward component access.
Inspecting relationships
Section titled “Inspecting relationships”Relation metadata is available through ecs_relation_info().
This includes the registered name and descriptor, including storage, target deletion policy and acyclic setting.
Tooling should use metadata instead of assuming every relation behaves like
ChildOf.
Target deletion
Section titled “Target deletion”A relation descriptor chooses what happens when its target is deleted.
Remove relation
Section titled “Remove relation”EcsRemoveRelation removes the edge from surviving sources.
This is appropriate for weak association-like relations.
Delete sources
Section titled “Delete sources”EcsDeleteSources deletes source entities that point at the deleted target.
This is appropriate for ownership relations where the source should not outlive the target.
ChildOf uses ownership-style behavior for hierarchy cleanup.
Choose target deletion policy as part of domain modeling. Retrofitting it after large amounts of application code depend on different semantics is error-prone.
Acyclic relations
Section titled “Acyclic relations”A relation can be marked acyclic.
SIECS can then reject cycles and use assumptions needed by hierarchy depth and upward traversal.
Use acyclic only when the relation is truly a directed acyclic relationship.
Examples:
- parent;
- dependency owner;
- prototype ancestry.
Do not mark peer graphs or arbitrary links acyclic merely for performance.
Builtin relationships
Section titled “Builtin relationships”The IsA relationship
Section titled “The IsA relationship”IsA connects an instance to a base.
ecs_is_a(instance, base);instance.is_a(base);The base is automatically treated as abstract for the inheritance model.
Components are materialized or shared according to each component’s inheritance policy.
Component sharing
Section titled “Component sharing”EcsInheritShared components remain on the base and are visible as shared
read-only fields.
EcsInheritOwned components are materialized as owned values on the inheriting
entity.
See Inheritance.
The ChildOf relationship
Section titled “The ChildOf relationship”ChildOf forms ownership hierarchies.
ecs_relate(child, ChildOf, parent);child.child_of(parent);Deleting the parent cleans up the owned subtree.
Hierarchy traversal and depth ordering are documented in Hierarchies.
Namespacing
Section titled “Namespacing”SIECS names are world-level names rather than hierarchy-scoped path
names. ChildOf does not automatically create a hierarchical lookup namespace.
Use hierarchy edges for ownership/spatial structure and the name API for global diagnostic/tool lookup.
Relationship performance
Section titled “Relationship performance”Introduction
Section titled “Introduction”Relation performance depends heavily on storage mode and query pattern.
No one relation representation is ideal for all questions.
Ask whether the workload most often needs:
- target for each source;
- all sources for one target;
- hierarchy depth;
- upward traversal;
- deletion ownership.
Choose the storage mode around the dominant access pattern.
Storing relationships
Section titled “Storing relationships”Dense/ByDepth storage adds per-source relation target data.
ByTarget storage introduces target-oriented grouping/indexing and can affect archetype/table structure.
That is why relation changes are structural operations and can migrate entities.
Fragmentation
Section titled “Fragmentation”A target-grouped relation can create more structural groups when many distinct targets are active.
This is beneficial when queries repeatedly ask for one target, but costly when the world has enormous target cardinality and most groups contain few entities.
Measure the actual number and population of relation tables.
Table Creation
Section titled “Table Creation”New structural combinations create tables.
Adding a relation can therefore create a table that differs by relation state even when ordinary components are unchanged.
Query caches are updated when such tables become available.
Indexing
Section titled “Indexing”ByTarget/ByDepth storage exists to maintain useful indexes at mutation time so queries can avoid rebuilding those relationships every frame.
The tradeoff is additional bookkeeping when relations are created, retargeted or removed.
Target-filtered Queries
Section titled “Target-filtered Queries”An exact-target query is efficient when the relation uses ByTarget, because
matching can operate on target-grouped table metadata.
Do not choose a Dense relation and then expect the same exact-target query cost.
Deferred relation changes
Section titled “Deferred relation changes”Relation operations participate in deferred command handling.
ecs_defer_begin();
ecs_relate(entity, GroupOf, first_group);ecs_relate(entity, GroupOf, final_group);
ecs_defer_end();// Inside a typed system callback, structural operations are deferred by SIECS.entity.relate<GroupOf>(first_group);entity.relate<GroupOf>(final_group);The flush applies the final structural result. Observers receive relation events at flush time according to the resulting transitions.
This makes relation updates safe inside ordinary deferred system execution.
Relation registration
Section titled “Relation registration”A custom relation is declared/defined similarly to other typed SIECS concepts.
ECS_RELATION(GroupOf, { .storage = EcsRelationByTarget, .on_delete_target = EcsRemoveRelation, .acyclic = false,});struct GroupOf {};
ecs::relation<GroupOf>({ .storage = EcsRelationByTarget, .on_delete_target = EcsRemoveRelation, .acyclic = false,});Register it in the active world:
ECS_RELATION_REGISTER(GroupOf);ecs::relation<GroupOf>();Generic code can call ecs_relation_init() with a name and descriptor.
Relation ids live in their own namespace. A relation id is not a component id.
One target per relation/source
Section titled “One target per relation/source”The core SIECS relation model associates a source’s relation with a target.
Calling ecs_relate(source, R, target2) when R already points to target1
retargets the relation.
This differs from a universal multi-pair model where one source might have many
independent (R, target) ids simultaneously.
When an application needs many-to-many edges, represent them with explicit edge entities/components or another data structure depending on query requirements.
Each relation has one target per source entity.
Relation operations
Section titled “Relation operations”Relate
Section titled “Relate”ecs_relate(entity, GroupOf, group);entity.relate<GroupOf>(group);Unrelate
Section titled “Unrelate”ecs_unrelate(entity, GroupOf);entity.unrelate<GroupOf>();Has relation
Section titled “Has relation”bool has = ecs_has_relation(entity, GroupOf);bool has = entity.has_relation<GroupOf>();Has exact target
Section titled “Has exact target”Use the target-specific relation API where needed, or compare:
ecs_target(entity, GroupOf) == groupentity.target<GroupOf>().id() == group.id()Retarget
Section titled “Retarget”Calling ecs_relate() with a different target updates structural relation state.
Retargeting can migrate the entity because target-oriented/depth storage may change its table/index placement.
Relation source indexes
Section titled “Relation source indexes”SIECS maintains relation-specific indexes appropriate to the selected storage mode.
These indexes are why querying sources by target/depth can be efficient without building application-owned maps every frame.
The tradeoff is mutation maintenance cost.
If relation membership changes extremely frequently but target queries are rare, a plain component handle may sometimes be a better model.
Dense relation iteration
Section titled “Dense relation iteration”With Dense storage, per-row target data aligns with the current entity batch.
Conceptually:
entities[] e0 e1 e2GroupOf target gA gB gAecs_targets() exposes relation target records for the batch.
This is cache-friendly when systems process source + target id together.
Target entity component lookup is still a separate operation unless the query uses another supported traversal mechanism.
ByDepth relation semantics
Section titled “ByDepth relation semantics”ByDepth augments the relation with depth metadata.
The runtime must update depths when hierarchy links change.
That means reparenting a large subtree can be more expensive than merely changing one raw parent handle, because derived depth state may need maintenance.
The benefit is cheap depth filters/order during repeated hierarchy processing.
Choose ByDepth when that repeated processing matters.
ByTarget relation semantics
Section titled “ByTarget relation semantics”ByTarget makes target identity part of how sources are grouped/indexed.
Conceptually:
Table/group for target A: source e0 source e4 source e9
Table/group for target B: source e2 source e7Then:
ecs_to(GroupOf, targetA)ecs::query().with<GroupOf>(targetA);can begin from the target-associated structural/indexed state rather than scan every source relation value.
The downside is potential fragmentation with many targets.
Upward traversal
Section titled “Upward traversal”For acyclic ByTarget relations, ecs_up() can read a component by traversing
toward relation targets/ancestors.
This enables patterns such as:
child -> parent -> rootand resolve the nearest ancestor that provides configuration/transform data.
Because the data belongs to another entity, the resolved field is shared and read-only.
Use ecs_up_optional() when no ancestor providing the component is acceptable.
Component-implied relations
Section titled “Component-implied relations”SIECS exposes a helper that can associate a component with a default relation target.
When the component is added and the source does not already have that relation, the relation is introduced.
This can encode invariants such as:
UIElement -> ChildOf UIRootor module-specific default ownership.
Do not use it where callers need to freely choose different relation semantics for every add; explicit relation operations are clearer.
Target deletion examples
Section titled “Target deletion examples”Weak membership
Section titled “Weak membership”Member GroupOf GroupGroup deletedMember survives, GroupOf removedUse EcsRemoveRelation.
Owned object
Section titled “Owned object”Widget ChildOf PanelPanel deletedWidget deletedUse ownership/source deletion semantics.
Choose the rule that matches domain lifetime, not whichever seems faster.
Relation observers
Section titled “Relation observers”EcsOnRelationSet and EcsOnRelationRemove allow systems/tools to react to
edges.
Relation observers receive old/new target data.
This is useful for maintaining external indexes that are not already provided by the ECS, but do not duplicate SIECS’s own target/depth indexes unnecessarily.
Relations and archetypes
Section titled “Relations and archetypes”A relation can participate in structural type/table selection.
Therefore changing a relation can be a structural change even if no ordinary component was added or removed.
This affects:
- pointer validity;
- query caches;
- table count;
- mutation cost.
Treat frequent relation retargeting as part of the performance budget.
Relation design checklist
Section titled “Relation design checklist”- Is this truly an entity-to-entity edge?
- Is there one target or many?
- Does source lifetime depend on target lifetime?
- Can cycles occur?
- Is the hot query by source, target or depth?
- Will ancestor traversal be used?
- How many distinct targets exist simultaneously?
- How frequently does the target change?
- Would a plain entity-handle component be simpler?
- Does the relation need observer/tool visibility?
A relation is valuable when the ECS can exploit the edge structurally. If not, ordinary component data may be sufficient.

