Skip to content
Flecs v4.1
queries/iter_targets

  • C

    View on GitHub

    1#include <iter_targets.h>
    2#include <stdio.h>
    3
    4// This example shows how to iterate matching targets of a relationship without
    5// iterating the same entity multiple times.
    6//
    7// When creating a (Relationship, *) query, the query will return one result per
    8// matching relationship pair, which returns the same entity multiple times.
    9// This example uses a (Relationship, _) query which at most returns a single
    10// result per matching pair, and the returned table record to iterate the
    11// targets for the current result.
    12
    13int main(int argc, char *argv[]) {
    14 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    15
    16 ECS_TAG(ecs, Eats);
    17 ECS_TAG(ecs, Pizza);
    18 ECS_TAG(ecs, Salad);
    19
    20 ecs_entity_t bob = ecs_entity(ecs, { .name = "Bob" });
    21 ecs_add_pair(ecs, bob, Eats, Pizza);
    22 ecs_add_pair(ecs, bob, Eats, Salad);
    23
    24 ecs_query_t *q = ecs_query(ecs, {
    25 // EcsAny ensures that only a single result is returned per entity, as
    26 // opposed to EcsWildcard which returns a result per matched pair.
    27 .terms = {{ .id = ecs_pair(Eats, EcsAny) }}
    28 });
    29
    30 ecs_iter_t it = ecs_query_iter(ecs, q);
    31 while (ecs_query_next(&it)) {
    32 const ecs_type_t *type = ecs_table_get_type(it.table);
    33 const ecs_table_record_t *tr = it.trs[0];
    34
    35 for (int i = 0; i < it.count; i ++) {
    36 printf("%s eats:\n", ecs_get_name(ecs, it.entities[i]));
    37 for (int t = 0; t < tr->count; t ++) {
    38 ecs_entity_t tgt = ecs_pair_second(
    39 ecs, type->array[t + tr->index]);
    40 printf(" - %s\n", ecs_get_name(ecs, tgt));
    41 }
    42 }
    43 }
    44
    45 return ecs_fini(ecs);
    46
    47 // Output:
    48 // Bob eats:
    49 // - Pizza
    50 // - Salad
    51}
    const ecs_entity_t EcsAny
    Any entity ("_").
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:395
    struct ecs_table_record_t ecs_table_record_t
    Opaque type for table record.
    Definition flecs.h:541
    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_add_pair(world, subject, first, second)
    Add a pair to an entity.
    Definition flecs_c.h:275
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ECS_TAG(world, id)
    Declare and define a tag.
    Definition flecs_c.h:59
    #define ecs_query(world,...)
    Shorthand for creating a query with ecs_query_init().
    Definition flecs_c.h:215
    const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
    Get the name of an entity.
    bool ecs_query_next(ecs_iter_t *it)
    Progress a query iterator.
    ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
    Create a query iterator.
    const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
    Get the type for a table.
    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
    Queries are lists of constraints (terms) that match entities.
    Definition flecs.h:858
    A type is a list of (component) IDs.
    Definition flecs.h:412
    ecs_id_t * array
    Array with IDs.
    Definition flecs.h:413

  • C++

    View on GitHub

    1#include <iter_targets.h>
    2#include <iostream>
    3
    4// This example shows how to iterate matching targets of a relationship without
    5// iterating the same entity multiple times.
    6//
    7// When creating a (Relationship, *) query, the query will return one result per
    8// matching relationship pair, which returns the same entity multiple times.
    9// This example uses a (Relationship, _) query which at most returns a single
    10// result per matching pair, and a targets() function that iterates the targets
    11// for the current entity.
    12
    13struct Eats { };
    14struct Pizza { };
    15struct Salad { };
    16
    17int main(int, char *[]) {
    18 flecs::world ecs;
    19
    20 ecs.entity("Bob")
    21 .add<Eats, Pizza>()
    22 .add<Eats, Salad>();
    23
    24 ecs.query_builder()
    25 .with<Eats>(flecs::Any) // flecs::Any ensures that only a single result
    26 // is returned per entity, as opposed to
    27 // flecs::Wildcard which returns a result per
    28 // matched pair.
    29
    30 .each([](flecs::iter& it, size_t row) {
    31 flecs::entity e = it.entity(row);
    32 std::cout << e.name() << " eats: \n";
    33
    34 it.targets(0, [](flecs::entity tgt) {
    35 std::cout << " - " << tgt.name() << "\n";
    36 });
    37 });
    38
    39 // Output:
    40 // Bob eats:
    41 // - Pizza
    42 // - Salad
    43}
    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
    flecs::string_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    Class for iterating over query results.
    Definition iter.hpp:68
    flecs::entity entity(size_t row) const
    Obtain a mutable handle to the entity being iterated over.
    Definition iter.hpp:36
    void targets(int8_t index, const Func &func)
    Iterate targets for pair field.
    Definition iter.hpp:147
    The world.
    Definition world.hpp:129
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.