Skip to content
Flecs v4.1
entities/hierarchy

  • C

    View on GitHub

    1#include <hierarchy.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x, y;
    6} Position;
    7
    8// Forward declare component so we can use it from functions other than main
    10
    11void iterate_tree(ecs_world_t *ecs, ecs_entity_t e, Position p_parent) {
    12 // Print hierarchical name of entity & the entity type
    13 char *path_str = ecs_get_path(ecs, e);
    14 char *type_str = ecs_type_str(ecs, ecs_get_type(ecs, e));
    15 printf("%s [%s]\n", path_str, type_str);
    16 ecs_os_free(type_str);
    17 ecs_os_free(path_str);
    18
    19 // Get entity position
    20 const Position *ptr = ecs_get(ecs, e, Position);
    21
    22 // Calculate actual position
    23 Position p_actual = {ptr->x + p_parent.x, ptr->y + p_parent.y};
    24 printf("{%f, %f}\n\n", p_actual.x, p_actual.y);
    25
    26 // Iterate children recursively
    27 ecs_iter_t it = ecs_children(ecs, e);
    28 while (ecs_children_next(&it)) {
    29 for (int i = 0; i < it.count; i ++) {
    30 iterate_tree(ecs, it.entities[i], p_actual);
    31 }
    32 }
    33}
    34
    35int main(int argc, char *argv[]) {
    36 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    37
    38 ECS_COMPONENT_DEFINE(ecs, Position);
    39 ECS_TAG(ecs, Star);
    40 ECS_TAG(ecs, Planet);
    41 ECS_TAG(ecs, Moon);
    42
    43 // Create a simple hierarchy.
    44 // Hierarchies use ECS relationships and the builtin flecs::ChildOf relationship to
    45 // create entities as children of other entities.
    46
    47 ecs_entity_t sun = ecs_entity(ecs, { .name = "Sun" });
    48 ecs_add(ecs, sun, Star);
    49 ecs_set(ecs, sun, Position, {1, 1});
    50
    51 ecs_entity_t mercury = ecs_entity(ecs, { .parent = sun, .name = "Mercury" });
    52 ecs_add(ecs, mercury, Planet);
    53 ecs_set(ecs, mercury, Position, {1, 1});
    54
    55 ecs_entity_t venus = ecs_entity(ecs, { .parent = sun, .name = "Venus" });
    56 ecs_add(ecs, venus, Planet);
    57 ecs_set(ecs, venus, Position, {2, 2});
    58
    59 ecs_entity_t earth = ecs_entity(ecs, { .parent = sun, .name = "Earth" });
    60 ecs_add(ecs, earth, Planet);
    61 ecs_set(ecs, earth, Position, {3, 3});
    62
    63 ecs_entity_t moon = ecs_entity(ecs, { .parent = earth, .name = "Moon" });
    64 ecs_add(ecs, moon, Moon);
    65 ecs_set(ecs, moon, Position, {0.1, 0.1});
    66
    67 // Is the Moon a child of Earth?
    68 printf("Child of Earth? %d\n\n", ecs_has_pair(ecs, moon, EcsChildOf, earth));
    69
    70 // Lookup moon by name
    71 ecs_entity_t e = ecs_lookup(ecs, "Sun.Earth.Moon");
    72 char *path = ecs_get_path(ecs, e);
    73 printf("Moon found: %s\n\n", path);
    74 ecs_os_free(path);
    75
    76 // Do a depth-first walk of the tree
    77 iterate_tree(ecs, sun, (Position){0, 0});
    78
    79 return ecs_fini(ecs);
    80
    81 // Output:
    82 // Child of Earth? 1
    83 //
    84 // Moon found: Sun.Earth.Moon
    85 //
    86 // Sun [Position, Star, (Identifier,Name)]
    87 // {1.000000, 1.000000}
    88 //
    89 // Sun.Mercury [Position, Planet, (Identifier,Name), (ChildOf,Sun)]
    90 // {2.000000, 2.000000}
    91 //
    92 // Sun.Venus [Position, Planet, (Identifier,Name), (ChildOf,Sun)]
    93 // {3.000000, 3.000000}
    94 //
    95 // Sun.Earth [Position, Planet, (Identifier,Name), (ChildOf,Sun)]
    96 // {4.000000, 4.000000}
    97 //
    98 // Sun.Earth.Moon [Position, Sun.Earth.Moon, (Identifier,Name), (ChildOf,Sun.Earth)]
    99 // {4.100000, 4.100000}
    100}
    const ecs_entity_t EcsChildOf
    Used to express parent-child relationships.
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:395
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    bool ecs_children_next(ecs_iter_t *it)
    Progress an iterator created with ecs_children().
    ecs_iter_t ecs_children(const ecs_world_t *world, ecs_entity_t parent)
    Iterate children of a parent.
    const ecs_type_t * ecs_get_type(const ecs_world_t *world, ecs_entity_t entity)
    Get the type of an entity.
    char * ecs_type_str(const ecs_world_t *world, const ecs_type_t *type)
    Convert a type to a string.
    #define ecs_add(world, entity, T)
    Add a component to an entity.
    Definition flecs_c.h:271
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ECS_COMPONENT_DEFINE(world, id_)
    Define a forward-declared component.
    Definition flecs_c.h:75
    #define ECS_TAG(world, id)
    Declare and define a tag.
    Definition flecs_c.h:59
    #define ECS_COMPONENT_DECLARE(id)
    Forward declare a component.
    Definition flecs_c.h:65
    #define ecs_get_path(world, child)
    Get path from root.
    Definition flecs_c.h:594
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    #define ecs_get(world, entity, T)
    Get a component.
    Definition flecs_c.h:339
    #define ecs_has_pair(world, entity, first, second)
    Test if an entity has a pair.
    Definition flecs_c.h:527
    ecs_entity_t ecs_lookup(const ecs_world_t *world, const char *path)
    Look up an entity by its path.
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.
    Iterator.
    Definition flecs.h:1183
    int32_t count
    Number of entities to iterate.
    Definition flecs.h:1189
    const ecs_entity_t * entities
    Entity identifiers.
    Definition flecs.h:1190

  • C++

    View on GitHub

    1#include <hierarchy.h>
    2#include <iostream>
    3
    4struct Position {
    5 double x, y;
    6};
    7
    8struct Star { };
    9struct Planet { };
    10struct Moon { };
    11
    12void iterate_tree(flecs::entity e, Position p_parent = {0, 0}) {
    13 // Print hierarchical name of entity & the entity type
    14 std::cout << e.path() << " [" << e.type().str() << "]\n";
    15
    16 // Get entity position
    17 const Position& p = e.get<Position>();
    18
    19 // Calculate actual position
    20 Position p_actual = {p.x + p_parent.x, p.y + p_parent.y};
    21 std::cout << "{" << p_actual.x << ", " << p_actual.y << "}\n\n";
    22
    23 // Iterate children recursively
    24 e.children([&](flecs::entity child) {
    25 iterate_tree(child, p_actual);
    26 });
    27}
    28
    29int main(int, char *[]) {
    30 flecs::world ecs;
    31
    32 // Create a simple hierarchy.
    33 // Hierarchies use ECS relationships and the builtin flecs::ChildOf relationship to
    34 // create entities as children of other entities.
    35
    36 flecs::entity sun = ecs.entity("Sun")
    37 .add<Star>()
    38 .set<Position>({1, 1});
    39
    40 ecs.entity(sun, "Mercury")
    41 .add<Planet>()
    42 .set<Position>({1, 1});
    43
    44 ecs.entity(sun, "Venus")
    45 .add<Planet>()
    46 .set<Position>({2, 2});
    47
    48 flecs::entity earth = ecs.entity(sun, "Earth")
    49 .add<Planet>()
    50 .set<Position>({3, 3});
    51
    52 flecs::entity moon = ecs.entity(earth, "Moon")
    53 .add<Moon>()
    54 .set<Position>({0.1, 0.1});
    55
    56 // Is the Moon a child of Earth?
    57 std::cout << "Child of Earth? " << moon.has(flecs::ChildOf, earth) << "\n\n";
    58
    59 // Lookup the moon by name
    60 flecs::entity e = ecs.lookup("Sun::Earth::Moon");
    61 std::cout << "Moon found: " << e.path() << "\n\n";
    62
    63 // Do a depth-first walk of the tree
    64 iterate_tree(sun);
    65
    66 // Output:
    67 // Child of Earth? 1
    68 //
    69 // Moon found: ::Sun::Earth::Moon
    70 //
    71 // ::Sun [Star, Position, (Identifier,Name)]
    72 // {1, 1}
    73 //
    74 // ::Sun::Mercury [Position, Planet, (Identifier,Name), (ChildOf,Sun)]
    75 // {2, 2}
    76 //
    77 // ::Sun::Venus [Position, Planet, (Identifier,Name), (ChildOf,Sun)]
    78 // {3, 3}
    79 //
    80 // ::Sun::Earth [Position, Planet, (Identifier,Name), (ChildOf,Sun)]
    81 // {4, 4}
    82 //
    83 // ::Sun::Earth::Moon [Position, Moon, (Identifier,Name), (ChildOf,Sun.Earth)]
    84 // {4.1, 4.1}
    85}
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::type type() const
    Get the entity's type.
    Definition impl.hpp:94
    flecs::string path(const char *sep="::", const char *init_sep="::") const
    Return the entity path.
    void children(flecs::entity_t rel, Func &&func) const
    Iterate children for an entity.
    Entity.
    Definition entity.hpp:30
    flecs::string str() const
    Convert the type to a comma-separated string.
    Definition type.hpp:38
    The world.
    Definition world.hpp:129
    flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
    Lookup entity by name.
    Definition world.hpp:102