Skip to content

Inheritance and Abstract Bases

SIECS inheritance is built on the IsA relation.

A base entity contains reusable component data. An instance connects to the base:

ecs_is_a(instance, base);

The base is treated as abstract so normal simulation queries focus on concrete instances.

Inheritance is intended for reusable defaults and templates, not for recreating deep object-oriented class hierarchies.

Create a base:

ecs_entity_t enemy = ecs_new();
ecs_set(enemy, Health, {100});
ecs_set(enemy, Speed, {2.5f});
ecs_abstract(enemy);

An entity used as an inheritance base is not meant to participate as a normal runtime instance.

Abstract entities are excluded from ordinary queries unless explicitly requested.

Every component chooses one inheritance policy at registration.

EcsInheritOwned materializes the effective base value as owned data on the instance.

Conceptually:

Base Health = 100
instance IsA Base
instance owns Health = copy of 100

Later changes to the base do not automatically synchronize that copied value.

Owned inheritance is appropriate for values that are defaults at instantiation/inheritance time.

EcsInheritShared keeps the value on the base.

Instances resolve it as a shared read-only field until they create an override.

Conceptually:

Base Material = Steel
Instance A -> shared Steel
Instance B -> shared Steel

Changing the base changes what non-overridden instances read.

This is useful for data shared by many instances.

An instance can set a component locally:

ecs_set(instance, Health, {500});

If the component was inherited, the instance now has an owned override for the effective value.

Queries can distinguish owned vs shared fields with ecs_field_kind().

Writable query access requires writable owned storage. Shared inherited values must not be mutated through an instance field pointer.

Test ancestry:

if (ecs_is(entity, base)) {
/* entity is base or derives through IsA */
}

Get the direct base:

ecs_entity_t base = ecs_entity_base(entity);

These operations let tools or gameplay code inspect inheritance without manually walking raw relation storage.

Queries can match effective inherited data.

A read-only component term may resolve to shared data from a base.

ecs_query_id_t q = ecs_query({
.components = { ecs_in(Material) },
});

For each batch, use ecs_field_kind() to determine whether the field is owned or shared.

Writable terms require local storage.

If a shared inherited component should be modified independently, create an override before running the writable operation.

This rule prevents a single instance from unintentionally modifying a value that other instances share from the same base.

Use the query descriptor’s is_a field:

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

This matches descendants of the selected base.

The ancestry test is transitive.

A useful template pattern is:

EnemyBase
Health = 100
Speed = 2.5
FastEnemyBase IsA EnemyBase
Speed = 5
Boss IsA EnemyBase
Health = 1000

Keep variant chains shallow enough that the effective data model stays obvious.

When a variant only differs by a small value, a normal component value can be simpler than creating another inheritance level.

A base may own a ChildOf subtree.

ecs_abstract(base) propagates abstract state through that owned subtree, which lets applications build template hierarchies that stay out of normal queries.

When instantiating a similar runtime hierarchy, application/module code can use the base structure as a model while creating concrete entities according to its needs.

SIECS does not expose prefab child slots or a dedicated Prefab tag API. Use Abstract, IsA, component inheritance and ChildOf explicitly.

ECS_COMPONENT_DEFINE(Health,
.inheritance = EcsInheritOwned
);
ECS_COMPONENT_DEFINE(Material,
.inheritance = EcsInheritShared
);
ecs_entity_t enemy = ecs_new();
ecs_set(enemy, Health, {100});
ecs_set(enemy, Material, {.kind = Steel});
ecs_abstract(enemy);
ecs_entity_t boss = ecs_new();
ecs_is_a(boss, enemy);
ecs_set(boss, Health, {500});

boss owns its Health override while it can continue to share Material.

Inheritance is most effective for:

  • template defaults;
  • shared immutable/configuration-like data;
  • grouping instances by a conceptual base;
  • tooling/template entities.

Prefer ordinary composition when behavior can be expressed by components without ancestry.

Prefer resources for world-global values.

Prefer relations for domain edges that are not “instance of this reusable base”.

ecs_is_a(entity, base) changes structural inheritance state.

The runtime builds an inheritance plan for components that must become locally owned according to their registration policy.

For owned-inheritance components, effective base values are materialized on the instance.

For shared components, the base remains the storage source.

Because this can create/move component storage, establishing/changing a base is a structural operation.

Retargeting IsA changes which effective data the instance sees.

Code that changes bases should consider:

  • which owned components must be materialized;
  • which shared values change source;
  • whether existing local overrides remain;
  • query cache/table transitions;
  • observer implications.

Do not use base retargeting every frame as a substitute for a small state field unless the structural behavior is actually desired.

A query can return a shared inherited field for a batch.

The batch groups entities for which the effective field source is compatible with the iterator representation.

Use ecs_field_kind() rather than assuming all batches in one query own their data.

The same query can encounter owned and shared batches.

Consider:

Base:
Material = Steel (shared)
Instance A:
IsA Base
Instance B:
IsA Base
Material = Gold (override)

A read query for Material can match both.

For A, the field is shared from Base.

For B, the field is owned.

A writable query should operate on B’s local storage but must not mutate A’s shared base storage as though it belonged to A.

IsA means reusable base/effective component inheritance.

ChildOf means hierarchy/ownership.

An entity can participate in both:

Soldier42 IsA SoldierBase
Soldier42 ChildOf Squad7

Do not use IsA merely to express containment.

Do not use ChildOf to imply component inheritance.

Keeping these meanings separate makes queries and lifetime behavior clear.

Because base values can be shared by many instances, changing a shared component on a base can affect many readers at once.

Treat base mutation like changing shared configuration.

If an instance needs independent state, override locally.

For high-frequency per-instance changes, use owned components rather than a shared base value.

Inheritance can save memory for shared data but introduces source resolution.

The useful tradeoff is:

less duplicated data
versus
additional shared/inherited matching/access logic

Owned inheritance copies data up front and then behaves like ordinary local storage.

Shared inheritance is most attractive when:

  • many instances share the same value;
  • reads are frequent;
  • writes are rare and usually happen on the base;
  • overrides are relatively uncommon.

A base should represent a coherent reusable default.

Avoid chains where every small variation creates another base level.

Prefer:

  • one stable base;
  • a few meaningful variants;
  • local component overrides for instance-specific differences.

Use ordinary tags/components to represent orthogonal capabilities instead of forcing them into a single inheritance tree.