Skip to content

Relationships

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.

Create a parent/child edge:

ecs_relate(child, ChildOf, parent);

Read the current target:

ecs_entity_t parent = ecs_target(child, ChildOf);

Remove the edge:

ecs_unrelate(child, ChildOf);

Custom relation:

ECS_RELATION(GroupOf, {
.storage = EcsRelationByTarget,
.on_delete_target = EcsRemoveRelation,
.acyclic = false,
});
ECS_RELATION_REGISTER(GroupOf);
ecs_relate(member, GroupOf, group);

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.
if (ecs_has_relation(entity, GroupOf)) {
/* entity has a GroupOf target */
}

The id-based form accepts an ecs_relation_id_t.

ecs_entity_t group = ecs_target(entity, GroupOf);

The target is 0 when the relation is not present.

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).

Use a query relation term:

ecs_query_id_t q = ecs_query({
.relations = { ecs_rel(GroupOf) },
});

This matches sources that currently have the relation.

For ByTarget:

ecs_query_id_t q = ecs_query({
.relations = { ecs_to(GroupOf, group) },
});

Matching tables are grouped around the target, so iteration can retrieve the shared target without a per-row lookup.

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;
}
}

For ByTarget:

ecs_entity_t target = ecs_target_shared(&it, GroupOf);

Optional relation:

ecs_rel_opt(GroupOf)

Excluded relation:

ecs_not_rel(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.

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.

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.

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.

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.

A relation descriptor chooses what happens when its target is deleted.

EcsRemoveRelation removes the edge from surviving sources.

This is appropriate for weak association-like relations.

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.

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.

IsA connects an instance to a base.

ecs_is_a(instance, base);

The base is automatically treated as abstract for the inheritance model.

Components are materialized or shared according to each component’s inheritance policy.

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.

ChildOf forms ownership hierarchies.

ecs_relate(child, ChildOf, parent);

Deleting the parent cleans up the owned subtree.

Hierarchy traversal and depth ordering are documented in Hierarchies.

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.

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.

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.

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.

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.

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.

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.

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();

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.

A custom relation is declared/defined similarly to other typed SIECS concepts.

ECS_RELATION(GroupOf, {
.storage = EcsRelationByTarget,
.on_delete_target = EcsRemoveRelation,
.acyclic = false,
});

Register it in the active world:

ECS_RELATION_REGISTER(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.

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.

ecs_relate(entity, GroupOf, group);
ecs_unrelate(entity, GroupOf);
bool has = ecs_has_relation(entity, GroupOf);

Use the target-specific relation API where needed, or compare:

ecs_target(entity, GroupOf) == group

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.

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.

With Dense storage, per-row target data aligns with the current entity batch.

Conceptually:

entities[] e0 e1 e2
GroupOf target gA gB gA

ecs_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 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 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 e7

Then:

ecs_to(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.

For acyclic ByTarget relations, ecs_up() can read a component by traversing toward relation targets/ancestors.

This enables patterns such as:

child -> parent -> root

and 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.

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 UIRoot

or 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.

Member GroupOf Group
Group deleted
Member survives, GroupOf removed

Use EcsRemoveRelation.

Widget ChildOf Panel
Panel deleted
Widget deleted

Use ownership/source deletion semantics.

Choose the rule that matches domain lifetime, not whichever seems faster.

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.

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.

  1. Is this truly an entity-to-entity edge?
  2. Is there one target or many?
  3. Does source lifetime depend on target lifetime?
  4. Can cycles occur?
  5. Is the hot query by source, target or depth?
  6. Will ancestor traversal be used?
  7. How many distinct targets exist simultaneously?
  8. How frequently does the target change?
  9. Would a plain entity-handle component be simpler?
  10. 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.