Skip to content
Flecs v4.1
queries/hierarchy

  • C++

    View on GitHub

    1#include <hierarchy.h>
    2#include <iostream>
    3
    4// Simple transform location with 2D location
    5struct Transform {
    6 double x, y;
    7};
    8
    9// Create two distinct types for world and local transform
    10struct WorldTransform : Transform { };
    11struct LocalTransform : Transform { };
    12
    13int main(int, char *[]) {
    14 flecs::world ecs;
    15
    16 // Whenever we add LocalTransform, also add WorldTransform
    17 ecs.component<LocalTransform>()
    18 .add(flecs::With, ecs.component<WorldTransform>());
    19
    20 // Create a hierarchy. For an explanation see the entities/hierarchy example
    21 flecs::entity sun = ecs.entity("Sun")
    22 .set(LocalTransform{1, 1});
    23
    24 ecs.entity(sun, "Mercury")
    25 .set(LocalTransform{1, 1});
    26
    27 ecs.entity(sun, "Venus")
    28 .set(LocalTransform{2, 2});
    29
    30 flecs::entity earth = ecs.entity(sun, "Earth")
    31 .set(LocalTransform{3, 3});
    32
    33 ecs.entity(earth, "Moon")
    34 .set(LocalTransform{0.1, 0.1});
    35
    36 // Create a hierarchical query to compute the global position from the
    37 // local position and the parent position. The three template arguments are:
    38 // - Local position
    39 // - World parent position (* = optional, so we match root entities)
    40 // - World position
    42 ecs.query_builder<const LocalTransform, const WorldTransform*, WorldTransform>()
    43 // Select the second query argument (const WorldTransform) from the
    44 // parent entity with .parent(). The cascade() modifier ensures that
    45 // the query iterates parents before children.
    46 .term_at(1).parent().cascade()
    47 // Finalize the query
    48 .build();
    49
    50 // Run the query
    51 q.each([](
    52 const LocalTransform& t_local,
    53 const WorldTransform *t_parent,
    54 WorldTransform& t_world)
    55 {
    56 t_world.x = t_local.x;
    57 t_world.y = t_local.y;
    58 if (t_parent) {
    59 t_world.x += t_parent->x;
    60 t_world.y += t_parent->y;
    61 }
    62 });
    63
    64 // Print world positions
    65 ecs.each([](flecs::entity e, WorldTransform& p) {
    66 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    67 });
    68
    69 // Output:
    70 // Sun: {1, 1}
    71 // Mercury: {2, 2}
    72 // Venus: {3, 3}
    73 // Earth: {4, 4}
    74 // Moon: {4.1, 4.1}
    75}
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    flecs::query_builder< Comps... > query_builder(Args &&... args) const
    Create a query builder.
    Definition impl.hpp:342
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.
    Definition impl.hpp:368
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::string_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    void each(Func &&func) const
    Each iterator.
    Definition iterable.hpp:37
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129