![]() |
Flecs v3.2
A fast entity component system (ECS) for C & C++
|
The relationships feature makes it possible to describe entity graphs natively in ECS. Graphs are created by adding and removing relationships from one entity to another entity. See this blog for an introduction to entity relationships.
Adding/removing relationships is similar to adding/removing regular components, with as difference that instead of a single component id, a relationship adds a pair of two things to an entity. In this pair, the first element represents the relationship (e.g. "Eats"), and the second element represents the relationship target (e.g. "Apples").
Relationships can be used to describe many things, from hierarchies to inventory systems to trade relationships between players in a game. The following sections go over how to use relationships, and what features they support.
Name | Description |
---|---|
Id | An id that can be added and removed |
Component | Id with a single element (same as an entity id) |
Pair | Id with two elements |
Tag | Component or pair not associated with data |
Relationship | Used to refer to first element of pair |
Target | Used to refer to second element of pair |
Source | Entity to which an id is added |
Make sure to check out the code examples in the repository:
The following code is a simple example that uses relationships:
In this example, we refer to Bob
as the "source", Likes
as the "relationship" and Alice
as the "target". A relationship when combined with an target is called a "relationship pair".
The same relationship can be added multiple times to an entity, as long as its target is different:
An application can query for relationships with the (Relationship, Target)
notation:
This example just shows a simple relationship query. Relationship queries are much more powerful than this as they provide the ability to match against entity graphs of arbitrary size. For more information on relationship queries see the query manual.
There are a number of ways an application can query for relationships. The following kinds of queries are available for all (unidirectional) relationships, and are all constant time:
More advanced queries are possible with filters, queries and rules. See the Queries manual for more details.
Relationship pairs, just like regular component, can be associated with data. To associate data with a relationship pair, at least one of its elements needs to be a component. A pair can be associated with at most one type. To determine which type is associated with a relationship pair, the following rules are followed in order:
The following examples show how these rules can be used:
A limitation of components is that they can only be added once to an entity. Relationships make it possible to get around this limitation, as a component can be added multiple times, as long as the pair is unique. Pairs can be constructed on the fly from new entity identifiers, which means this is possible:
When querying for relationship pairs, it is often useful to be able to find all instances for a given relationship or target. To accomplish this, an application can use wildcard expressions. Consider the following example, that queries for all entities with a Likes
relationship:
Wildcards may appear in query expressions, using the *
character:
Wildcards may used for the relationship or target part of a pair, or both:
An application can use pair wildcard expressions to find all instances of a relationship for an entity. The following example shows how to find all Eats
relationships for an entity:
Flecs comes with a few builtin relationships that have special meaning within the framework. While they are implemented as regular relationships and therefore obey the same rules as any custom relationship, they are used to enhance the features of different parts of the framework. The following two sections describe the builtin relationships of Flecs.
The IsA
relationship is a builtin relationship that allows applications to express that one entity is equivalent to another. This relationship is at the core of component sharing and plays a large role in queries. The IsA
relationship can be used like any other relationship, as is shown here:
In C++, adding an IsA
relationship has a shortcut:
This indicates to Flecs that an Apple
is equivalent to a Fruit
and should be treated as such. This equivalence is one-way, as a Fruit
is not equivalent to an Apple
. Another way to think about this is that IsA
allows an application to express subsets and supersets. An Apple
is a subset of Fruit
. Fruit
is a superset of Apple
.
We can also add IsA
relationships to Apple
:
This specifies that GrannySmith
is a subset of Apple
. A key thing to note here is that because Apple
is a subset of Fruit
, GrannySmith
is a subset of Fruit
as well. This means that if an application were to query for (IsA, Fruit)
it would both match Apple
and GrannySmith
. This property of the IsA
relationship is called "transitivity" and it is a feature that can be applied to any relationship. See the section on Transitivity for more details.
An entity with an IsA
relationship to another entity is equivalent to the other entity. So far the examples showed how querying for an IsA
relationship will find the subsets of the thing that was queried for. In order for entities to be treated as true equivalents though, everything the superset contains (its components, tags, relationships) must also be found on the subsets. Consider:
Here, the Frigate
"inherits" the contents of SpaceShip
. Even though MaxSpeed
was never added directly to Frigate
, an application can do this:
While the Frigate
entity also inherited the Defense
component, it overrode this with its own value, so that the following example works:
The ability to share components is also applied transitively, so Frigate
could be specialized further into a FastFrigate
:
This ability to inherit and override components is one of the key enabling features of Flecs prefabs, and is further explained in the Inheritance section of the manual.
The ChildOf
relationship is the builtin relationship that allows for the creation of entity hierarchies. The following example shows how hierarchies can be created with ChildOf
:
In C++, adding a ChildOf
relationship has a shortcut:
The ChildOf
relationship is defined so that when a parent is deleted, its children are also deleted. For more information on specifying cleanup behavior for relationships, see the Relationship cleanup properties section.
The ChildOf
relationship is defined as a regular relationship in Flecs. There are however a number of features that interact with ChildOf
. The following sections describe these features.
Entities in flecs can have names, and name lookups can be relative to a parent. Relative name lookups can be used as a namespacing mechanism to prevent clashes between entity names. This example shows a few examples of name lookups in combination with hierarchies:
In some scenarios a number of entities all need to be created with the same parent. Rather than adding the relationship to each entity, it is possible to configure the parent as a scope, which ensures that all entities created afterwards are created in the scope. The following example shows how:
Scopes in C++ can also be used with the scope
function on an entity, which accepts a (typically lambda) function:
Scopes are the mechanism that ensure contents of a module are created as children of the module, without having to explicitly add the module as a parent.
When entities that are used as tags, components, relationships or relationship targets are deleted, cleanup policies ensure that the store does not contain any dangling references. Any cleanup policy provides this guarantee, so while they are configurable, applications cannot configure policies that allows for dangling references.
Note: this only applies to entities (like tags, components, relationships) that are added to other entities. It does not apply to components that store an entity value, so:
The default policy is that any references to the entity will be removed. For example, when the tag Archer
is deleted, it will be removed from all entities that have it, which is similar to invoking the remove_all
operation:
Since entities can be used in relationship pairs, just calling remove_all
on just the entity itself does not guarantee that no dangling references are left. A more comprehensive description of what happens is:
This succeeds in removing all possible references to Archer
. Sometimes this behavior is not what we want however. Consider a parent-child hierarchy, where we want to delete the child entities when the parent is deleted. Instead of removing (ChildOf, parent)
from all children, we need to delete the children.
We also want to specify this per relationship. If an entity has (Likes, parent)
we may not want to delete that entity, meaning the cleanup we want to perform for Likes
and ChildOf
may not be the same.
This is what cleanup policies are for: to specify which action needs to be executed under which condition. They are applied to entities that have a reference to the entity being deleted: if I delete the Archer
tag I remove the tag from all entities that have it.
To configure a cleanup policy for an entity, a (Condition, Action)
pair can be added to it. If no policy is specified, the default cleanup action (Remove
) is performed.
There are three cleanup actions:
Remove
: as if doing remove_all(entity)
(default)Delete
: as if doing delete_with(entity)
Panic
: throw a fatal error (default for components)There are two cleanup conditions:
OnDelete
: the component, tag or relationship is deletedOnDeleteTarget
: a target used with the relationship is deletedPolicies apply to both regular and pair instances, so to all entities with T
as well as (T, *)
.
The following examples show how to use cleanup policies
**(OnDelete, Remove)**
**(OnDelete, Delete)**
**(OnDeleteTarget, Delete)**
While cleanup actions allow for specifying what needs to happen when a particular entity is deleted, or when an entity used with a particular relationship is deleted, they do not enforce a strict cleanup order. The reason for this is that there can be many orderings that satisfy the cleanup policies.
This is important to consider especially when writing OnRemove
triggers or hooks, as the order in which they are invoked highly depends on the order in which entities are cleaned up.
Take an example with a parent and a child that both have the Node
tag:
In this example, when calling p.destruct()
the observer is first invoked for the child, and then for the parent, which is to be expected as the child is deleted before the parent. Cleanup policies do not however guarantee that this is always the case.
An application could also call world.component<Node>().destruct()
which would delete the Node
component and all of its instances. In this scenario the cleanup policies for the ChildOf
relationship are not considered, and therefore the ordering is undefined. Another typical scenario in which ordering is undefined is when an application has cyclical relationships with a Delete
cleanup action.
Cleanup issues often show up during world teardown as the ordering in which entities are deleted is controlled by the application. While world teardown respects cleanup policies, there can be many entity delete orderings that are valid according to the cleanup policies, but not all of them are equally useful. There are ways to organize entities that helps world cleanup to do the right thing. These are:
Organize components, triggers, observers and systems in modules. Storing these entities in modules ensures that they stay alive for as long as possible. This leads to more predictable cleanup ordering as components will be deleted as their entities are, vs. when the component is deleted. It also ensures that triggers and observers are not deleted while matching events are still being generated.
Avoid organizing components, triggers, observers and systems under entities that are not modules. If a non-module entity with children is stored in the root, it will get cleaned up along with other regular entities. If you have entities such as these organized in a non-module scope, consider adding the EcsModule
/flecs::Module
tag to the root of that scope.
The next section goes into more detail on why this improves cleanup behavior and what happens during world teardown.
To understand why some ways to organize entities work better than others, having an overview of what happens during world teardown is useful. Here is a list of the steps that happen when a world is deleted:
ChildOf
relationship. Note that empty entities (entities without any components) are not found during this step.Relationship properties are tags that can be added to relationships to modify their behavior.
A relationship can be marked as a tag in which case it will never contain data. By default the data associated with a pair is determined by whether either the relationship or target are components. For some relationships however, even if the target is a component, no data should be added to the relationship. Consider the following example:
To prevent data from being associated with pairs that can apply to components, the Tag
property can be added to relationships:
The Tag
property is only interpreted when it is added to the relationship part of a pair.
Entities can be annotated with the Final
property, which prevents using them with IsA
relationship. This is similar to the concept of a final class as something that cannot be extended. The following example shows how use Final
:
Queries may use the final property to optimize, as they do not have to explore subsets of a final entity. For more information on how queries interpret final, see the Query manual. By default, all components are created as final.
The DontInherit
property prevents inheriting a component from a base entity (IsA
target). Consider the following example:
The builtin Prefab
, Disabled
, Identifier
and ChildOf
tags/relationships are marked as DontInherit
.
The AlwaysOverride
property ensures that a component is always automatically overridden when an inheritance (IsA
) relationship is added. The behavior of this property is as if OVERRIDE | Component
is always added together with Component
.
Relationships can be marked as transitive. A formal-ish definition if transitivity in the context of relationships is:
What this means becomes more obvious when translated to a real-life example:
In this example, LocatedIn
is the relationship and Manhattan
, New York
and USA
are entities A
, B
and C
. Another common example of transitivity is found in OOP inheritance:
In this example IsA
is the relationship and Square
, Rectangle
and Shape
are the entities.
When relationships in Flecs are marked as transitive, queries can follow the transitive relationship to see if an entity matches. Consider this example dataset:
If we were now to query for (LocatedIn, USA)
we would only match NewYork
, because we never added (LocatedIn, USA)
to Manhattan
. To make sure queries Manhattan
as well we have to make the LocatedIn
relationship transitive. We can simply do this by adding the transitive property to the relationship entity:
When now querying for (LocatedIn, USA)
, the query will follow the LocatedIn
relationship and return both NewYork
and Manhattan
. For more details on how queries use transitivity, see the Transitive Relationships section in the query manual.
A relationship can be marked reflexive which means that a query like Relationship(Entity, Entity)
should evaluate to true. The utility of Reflexive
becomes more obvious with an example:
Given this dataset:
we can ask whether an oak is a tree:
We can also ask whether a tree is a tree, which it obviously is:
However, this does not apply to all relationships. Consider a dataset with a LocatedIn
relationship:
we can now ask whether SanFrancisco is located in SanFrancisco, which it is not:
In these examples, IsA
is a reflexive relationship, whereas LocatedIn
is not.
A relationship can be marked with the Acyclic
property to indicate that it cannot contain cycles. Both the builtin ChildOf
and IsA
relationships are marked acyclic. Knowing whether a relationship is acyclic allows the storage to detect and throw errors when a cyclic relationship is introduced by accident.
Note that because cycle detection requires expensive algorithms, adding Acyclic
to a relationship does not guarantee that an error will be thrown when a cycle is accidentally introduced. While detection may improve over time, an application that runs without errors is no guarantee that it does not contain acyclic relationships with cycles.
Traversable relationships are allowed to be traversed automatically by queries, for example using the up
bitflag (upwards traversal, see query traversal flags). Traversable relationships are also marked as Acyclic
, which ensures a query won't accidentally attempt to traverse a relationship that contains cycles.
Events are propagated along the edges of traversable relationships. A typical example of this is when a component value is changed on a prefab. The event of this change will be propagated by traversing the IsA
relationship downwards, for all instances of the prefab. Event propagation does not happen for relationships that are not marked with Traversable
.
The Exclusive
property enforces that an entity can have only a single instance of a relationship. When a second instance is added, it replaces the first instance. An example of a relationship with the Exclusive
property is the builtin ChildOf
relationship:
To create a custom exclusive relationship, add the Exclusive
property:
The Union
is similar to Exclusive
in that it enforces that an entity can have only a single instance of a relationship. The difference between Exclusive
and Union
is that Union
combines different relationship targets in a single table. This reduces table fragmentation, and as a result speeds up add/remove operations. This increase in add/remove speed does come at a cost: iterating a query with union terms is more expensive than iterating a regular relationship.
The API for using the Union
property is similar to regular relationships, as this example shows:
When compared to regular relationships, union relationships have some differences and limitations:
And
operator(R, *)
term will return (R, *)
as term id for each entityThe Symmetric
property enforces that when a relationship (R, Y)
is added to entity X
, the relationship (R, X)
will be added to entity Y
. The reverse is also true, if relationship (R, Y)
is removed from X
, relationship (R, X)
will be removed from Y
.
The symmetric property is useful for relationships that do not make sense unless they are bidirectional. Examples of such relationships are AlliesWith
, MarriedTo
, TradingWith
and so on. An example:
The With
relationship can be added to components to indicate that it must always come together with another component. The following example shows how With
can be used with regular components/tags:
When the With
relationship is added to a relationship, the additional id added to the entity will be a relationship pair as well, with the same target as the original relationship:
The OneOf
property enforces that the target of the relationship is a child of a specified entity. OneOf
can be used to indicate that the target needs to be either a child of the relationship (common for enum relationships), or of another entity.
The following example shows how to constrain the relationship target to a child of the relationship:
The following example shows how OneOf
can be used to enforce that the relationship target is the child of an entity other than the relationship:
This section goes over the performance implications of using relationships.
The ECS storage needs to know two things in order to store components for entities:
Ids represent anything that can be added to an entity. An id that is not associated with a type is called a tag. An id associated with a type is a component. For regular components, the id is a regular entity that has the builtin Component
component. This component contains the information needed by the storage to associate the entity with a type. If an entity does not have the Component
component, it is a tag.
Relationships do not fundamentally change or extend the capabilities of the storage. Relationship pairs are two elements encoded into a single 64-bit id, which means that on the storage level they are treated the same way as regular component ids. What changes is the function that determines which type is associated with an id. For regular components this is simply a check on whether an entity has Component
. To support relationships, new rules are added to determine the type of an id.
Because of this, adding/removing relationships to entities has the same performance as adding/removing regular components. This becomes more obvious when looking more closely at a function that adds a relationship pair. The following example shows how the function that adds a regular component and the function that adds a pair actually map to the same functions:
This example also applies to C++, as the C++ API maps to the same C API functions.
While most of the storage uses the same code paths for regular components and relationships, there are a few properties of the storage that can impact performance when using relationships. These properties are not unique to relationships, but are more likely to be significant when using relationships.
Flecs reserves entity ids under a threshold (FLECS_HI_COMPONENT_ID
, default is 256) for components. This low id range is used by the storage to more efficiently encode graph edges between tables. Graph edges for components with low ids use direct array indexing, whereas graph edges for high ids use a hashmap. Graph edges are used to find the next archetype when adding/removing component ids, and are a contributing factor to the performance overhead of add/remove operations.
Because of the way pair ids are encoded, a pair will never be in the low id range. This means that adding/removing a pair id always uses a hashmap to find the next archetype. This introduces a small overhead, which is usually 5-10% of the total cost of an operation.
Fragmentation is a property of archetype-based ECS implementations where entities are spread out over more tables as the number of different component combinations increases. The overhead of fragmentation is visible in two areas:
Applications that make extensive use of relationships might observe high levels of fragmentation, as relationships can introduce many different combinations of components. While the Flecs storage is optimized for supporting large amounts (hundreds of thousands) of tables, fragmentation is a factor to consider when using relationships.
Fragmentation can be reduced by using union relationships. There are additional storage improvements on the roadmap that will decrease the overhead of fragmentation introduced by relationships.
To reduce fragmentation, a relationship can be flattened by calling the ecs_flatten
operation. This combines entities from different relationship targets in the same table, provided that they otherwise have the same components. The operation is recursive, meaning that for a specified relationship, the entire subtree is flattened.
Flattening a subtree can be done with or without preserving depth information of the original tree. Preserving depth information ensures that features like cascade
still work as usual, whereas not preserving depth information further decreases fragmentation.
The flattening operation by default removes names of the flattened entities. This behavior can be overridden with an option. When flattening for the builtin ChildOf
relationship, flattening with preservation of names can cause the operation to fail, if entities from different subtrees have the same name.
After the flatten operation, entities for the same target are stored in contiguous slices in the new table. The new table will have a (Target, Relationship)
pair, which contains data about the original target, and the start and number of entities in the contiguous block. This information is used by queries when iterating to ensure entities for the same target can still be iterated in bulk.
Relationship flattening is an experimental feature, and some limitations apply for the current implementation. After a subtree has been flattened, the entities in that subtree can no longer be individually deleted from a target, and cannot be moved to another parent. Additionally, no components can be added/removed to entities in a flattened subtree. Relationship flattening is currently supported only for exclusive, acyclic relationships.
For additional information, see the API documentation for the ecs_flatten
operation.
When an id added to an entity is deleted, all references to that id are deleted from the storage (see cleanup properties). For example, when the component Position
is deleted it is removed from all entities, and all tables with the Position
component are deleted. While not unique to relationships, it is more common for relationships to trigger cleanup actions, as relationship pairs contain regular entities.
The opposite is also true. Because relationship pairs can contain regular entities which can be created on the fly, table creation is more common than in applications that do not use relationships. While Flecs is optimized for fast table creation, creating and cleaning up tables is inherently more expensive than creating/deleting an entity. Therefore table creation is a factor to consider, especially for applications that make extensive use of relationships.
To improve the speed of evaluating queries, Flecs has indices that store all tables for a given component id. Whenever a new table is created, it is registered with the indices for the ids the table has, including ids for relationship pairs.
While registering a table for a relationship index is not more expensive than registering a table for a regular index, a table with relationships has to also register itself with the appropriate wildcard indices for its relationships. For example, a table with relationship (Likes, Apples)
registers itself with the (Likes, Apples)
, (Likes, *)
, (*, Apples)
and (*, *)
indices. For this reason, creating new tables with relationships has a higher overhead than a table without relationships.
A wildcard query for a relationship pair, like (Likes, *)
may return multiple results for each instance of the relationship. To find all instances of a relationship, the table index (see previous section) stores two additional pieces of information:
column
: At which offset in the table type does the id first occurcount
: How many occurrences of the id does the table haveIf the id is not a wildcard id, the number of occurrences will always be one. When the id is a wildcard, a table type may have multiple occurrences of a relationship. For wildcard queries in the form of (Likes, *)
, finding all occurrences is cheap, as a query can start at the column
and iterate the next count
members.
For wildcard queries in the form of (*, Apples)
, however, the pair ids are not stored contiguously in a table type. This means that if a table has multiple instances that match (*, Apples)
, a query will have to perform a linear search starting from column
. Once the query has found count
occurrences, it can stop searching.
The following example of a table type shows how relationships are ordered, and demonstrates why (Likes, *)
wildcards are easier to resolve than (*, Apples)
wildcards:
The index for (Likes, *)
will have column=1, count=3
, whereas the index for (*, Pears)
will have column=2, count=2
. To find all occurrences of (Likes, *)
a query can start iteration at index 1 and iterate 3 elements. To find all instances of (*, Pears)
a query has to start at index 2 and scan until the second instance is found.