Skip to content
Flecs v4.1
queries/group_iter_all

  • C

    View on GitHub

    1#include <group_iter_all.h>
    2#include <stdio.h>
    3
    4// This example demonstrates how to iterate over all active groups of a
    5// grouped query using ecs_query_get_groups(), and then iterate the entities
    6// within each group with ecs_iter_set_group(). This is useful when an
    7// application needs to do per-group logic before/after processing the entities
    8// in that group.
    9
    10typedef struct {
    11 double x, y;
    12} Position;
    13
    14int main(int argc, char *argv[]) {
    15 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    16
    17 ECS_COMPONENT(ecs, Position);
    18
    19 ecs_entity_t Asset_A = ecs_entity(ecs, { .name = "Asset_A" });
    20 ecs_entity_t Asset_B = ecs_entity(ecs, { .name = "Asset_B" });
    21 ecs_entity_t Asset_C = ecs_entity(ecs, { .name = "Asset_C" });
    22
    23 ecs_set(ecs, ecs_new_w_pair(ecs, EcsIsA, Asset_A), Position, {1, 1});
    24 ecs_set(ecs, ecs_new_w_pair(ecs, EcsIsA, Asset_A), Position, {2, 2});
    25 ecs_set(ecs, ecs_new_w_pair(ecs, EcsIsA, Asset_B), Position, {3, 3});
    26 ecs_set(ecs, ecs_new_w_pair(ecs, EcsIsA, Asset_C), Position, {4, 4});
    27 ecs_set(ecs, ecs_new_w_pair(ecs, EcsIsA, Asset_C), Position, {5, 5});
    28 ecs_set(ecs, ecs_new_w_pair(ecs, EcsIsA, Asset_C), Position, {6, 6});
    29
    30 // Group entities by their IsA target. With no group_by_callback set,
    31 // the default callback uses the relationship's target as the group ID.
    32 ecs_query_t *q = ecs_query(ecs, {
    33 .terms = {{ .id = ecs_id(Position) }},
    34 .group_by = EcsIsA
    35 });
    36
    37 // Iterate all active groups via the query's group map. The map key is
    38 // the group ID, which here is the asset entity (the target of IsA).
    39 const ecs_map_t *groups = ecs_query_get_groups(q);
    40 ecs_map_iter_t mit = ecs_map_iter(groups);
    41 while (ecs_map_next(&mit)) {
    42 uint64_t group_id = ecs_map_key(&mit);
    43 char *group_str = ecs_get_path(ecs, group_id);
    44 printf("Group %s:\n", group_str);
    45 ecs_os_free(group_str);
    46
    47 // Iterate just the entities in this group.
    48 ecs_iter_t it = ecs_query_iter(ecs, q);
    49 ecs_iter_set_group(&it, group_id);
    50 while (ecs_query_next(&it)) {
    51 Position *p = ecs_field(&it, Position, 0);
    52 for (int i = 0; i < it.count; i ++) {
    53 char *e_str = ecs_get_path(ecs, it.entities[i]);
    54 printf(" - %s: {%.0f, %.0f}\n", e_str, p[i].x, p[i].y);
    55 ecs_os_free(e_str);
    56 }
    57 }
    58 }
    59
    61
    62 // Output (group iteration order is unspecified):
    63 // Group Asset_B:
    64 // - #475: {3, 3}
    65 // Group Asset_A:
    66 // - #473: {1, 1}
    67 // - #474: {2, 2}
    68 // Group Asset_C:
    69 // - #476: {4, 4}
    70 // - #477: {5, 5}
    71 // - #478: {6, 6}
    72
    73 return ecs_fini(ecs);
    74}
    const ecs_entity_t EcsIsA
    Used to express inheritance relationships.
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    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
    #define ecs_new_w_pair(world, first, second)
    Create a new entity with a pair.
    Definition flecs_c.h:256
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_query(world,...)
    Shorthand for creating a query with ecs_query_init().
    Definition flecs_c.h:215
    #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_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    void ecs_iter_set_group(ecs_iter_t *it, uint64_t group_id)
    Set the group to iterate for a query iterator.
    bool ecs_query_next(ecs_iter_t *it)
    Progress a query iterator.
    void ecs_query_fini(ecs_query_t *query)
    Delete a query.
    ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
    Create a query iterator.
    const ecs_map_t * ecs_query_get_groups(const ecs_query_t *query)
    Return the map with query groups.
    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
    Queries are lists of constraints (terms) that match entities.
    Definition flecs.h:858

  • C++

    View on GitHub

    1#include <group_iter_all.h>
    2#include <iostream>
    3
    4// This example demonstrates how to iterate over all active groups of a
    5// grouped query using query::groups(), and then iterate the entities
    6// within each group with set_group(). This is useful when an
    7// application needs to do per-group logic before/after processing the entities
    8// in that group.
    9
    10struct Position { double x, y; };
    11
    12int main() {
    13 flecs::world ecs;
    14
    15 auto Asset_A = ecs.entity("Asset_A");
    16 auto Asset_B = ecs.entity("Asset_B");
    17 auto Asset_C = ecs.entity("Asset_C");
    18
    19 ecs.entity().is_a(Asset_A).set<Position>({1, 1});
    20 ecs.entity().is_a(Asset_A).set<Position>({2, 2});
    21 ecs.entity().is_a(Asset_B).set<Position>({3, 3});
    22 ecs.entity().is_a(Asset_C).set<Position>({4, 4});
    23 ecs.entity().is_a(Asset_C).set<Position>({5, 5});
    24 ecs.entity().is_a(Asset_C).set<Position>({6, 6});
    25
    26 // Group entities by their IsA target. The default group_by callback uses
    27 // the relationship's target as the group ID.
    28 flecs::query<Position> q = ecs.query_builder<Position>()
    29 .group_by(flecs::IsA)
    30 .build();
    31
    32 // Iterate all active groups, then iterate entities for each group with
    33 // set_group(). The map key (g.first) is the group ID, which here is the
    34 // asset entity (the target of IsA).
    35 for (auto g : q.groups()) {
    36 flecs::entity asset = ecs.entity(g.first);
    37 std::cout << "Group " << asset.path() << ":\n";
    38
    39 q.set_group(g.first).each([&](flecs::entity e, Position& p) {
    40 std::cout << " - " << e.path() << ": {" << p.x << ", " << p.y << "}\n";
    41 });
    42 }
    43
    44 // Output (group iteration order is unspecified):
    45 // Group ::Asset_B:
    46 // - #501: {3, 3}
    47 // Group ::Asset_A:
    48 // - #499: {1, 1}
    49 // - #500: {2, 2}
    50 // Group ::Asset_C:
    51 // - #502: {4, 4}
    52 // - #503: {5, 5}
    53 // - #504: {6, 6}
    54}
    flecs::query_builder< Comps... > query_builder(Args &&... args) const
    Create a query builder.
    Definition impl.hpp:342
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    const Self & is_a(entity_t second) const
    Shortcut for add(IsA, entity).
    Definition builder.hpp:122
    flecs::string path(const char *sep="::", const char *init_sep="::") const
    Return the entity path.
    Entity.
    Definition entity.hpp:30
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129