Skip to content
Flecs v4.1
queries/variables

  • C

    View on GitHub

    1#include <variables.h>
    2#include <stdio.h>
    3
    4int main(int argc, char *argv[]) {
    5 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    6
    7 ECS_TAG(ecs, Eats);
    8 ECS_TAG(ecs, Healthy);
    9
    10 ecs_entity_t Apples = ecs_entity(ecs, { .name = "Apples" });
    11 ecs_entity_t Salad = ecs_entity(ecs, { .name = "Salad" });
    12 ecs_entity_t Burgers = ecs_entity(ecs, { .name = "Burgers" });
    13 ecs_entity_t Pizza = ecs_entity(ecs, { .name = "Pizza" });
    14 ecs_entity_t Chocolate = ecs_entity(ecs, { .name = "Chocolate" });
    15
    16 ecs_add(ecs, Apples, Healthy);
    17 ecs_add(ecs, Salad, Healthy);
    18
    19 ecs_entity_t bob = ecs_entity(ecs, { .name = "Bob" });
    20 ecs_add_pair(ecs, bob, Eats, Apples);
    21 ecs_add_pair(ecs, bob, Eats, Burgers);
    22 ecs_add_pair(ecs, bob, Eats, Pizza);
    23
    24 ecs_entity_t alice = ecs_entity(ecs, { .name = "Alice" });
    25 ecs_add_pair(ecs, alice, Eats, Salad);
    26 ecs_add_pair(ecs, alice, Eats, Chocolate);
    27 ecs_add_pair(ecs, alice, Eats, Apples);
    28
    29 // Here we're creating a query that in the query DSL would look like this:
    30 // Eats($this, $Food), Healthy($Food)
    31 ecs_query_t *q = ecs_query(ecs, {
    32 .terms = {
    33 // Query variables are like wildcards, but enforce that the entity
    34 // substituted by the wildcard is the same across terms.
    35 //
    36 // For example, in this query it is not guaranteed that the entity
    37 // substituted by the * for Eats is the same as for Healthy:
    38 // (Eats, *), Healthy(*)
    39 //
    40 // By replacing * with $food, both terms will be constrained to
    41 // match the same entity for the wildcard.
    42 { .first.id = Eats, .second.name = "$food" },
    43 { .first.id = Healthy, .src.name = "$food" }
    44 }
    45 });
    46
    47 // Lookup the index of the variable. This will let us quickly lookup its
    48 // value while we're iterating.
    49 int food_var = ecs_query_find_var(q, "food");
    50
    51 // Iterate the query
    52 ecs_iter_t it = ecs_query_iter(ecs, q);
    53 while (ecs_query_next(&it)) {
    54 ecs_entity_t food = ecs_iter_get_var(&it, food_var);
    55 for (int i = 0; i < it.count; i ++) {
    56 printf("%s eats %s\n", ecs_get_name(ecs, it.entities[i]),
    57 ecs_get_name(ecs, food));
    58 }
    59 }
    60
    62
    63 // Output
    64 // Bob eats Apples
    65 // Alice eats Apples
    66 // Alice eats Salad
    67
    68 return ecs_fini(ecs);
    69}
    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_add_pair(world, subject, first, second)
    Add a pair to an entity.
    Definition flecs_c.h:275
    #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_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
    ecs_entity_t ecs_iter_get_var(ecs_iter_t *it, int32_t var_id)
    Get the value of an iterator variable as an entity.
    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.
    int32_t ecs_query_find_var(const ecs_query_t *query, const char *name)
    Find a variable index.
    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.
    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 <variables.h>
    2#include <iostream>
    3
    4struct Eats { };
    5struct Healthy { };
    6
    7int main(int, char *[]) {
    8 flecs::world ecs;
    9
    10 flecs::entity Apples = ecs.entity("Apples").add<Healthy>();
    11 flecs::entity Salad = ecs.entity("Salad").add<Healthy>();
    12 flecs::entity Burgers = ecs.entity("Burgers");
    13 flecs::entity Pizza = ecs.entity("Pizza");
    14 flecs::entity Chocolate = ecs.entity("Chocolate");
    15
    16 ecs.entity("Bob")
    17 .add<Eats>(Apples)
    18 .add<Eats>(Burgers)
    19 .add<Eats>(Pizza);
    20
    21 ecs.entity("Alice")
    22 .add<Eats>(Salad)
    23 .add<Eats>(Chocolate)
    24 .add<Eats>(Apples);
    25
    26 // Here we're creating a query that in the query DSL would look like this:
    27 // Eats($this, $Food), Healthy($Food)
    29 // Identifiers that start with $ are query variables. Query variables
    30 // are like wildcards, but enforce that the entity substituted by the
    31 // wildcard is the same across terms.
    32 //
    33 // For example, in this query it is not guaranteed that the entity
    34 // substituted by the * for Eats is the same as for Healthy:
    35 // (Eats, *), Healthy(*)
    36 //
    37 // By replacing * with $food, both terms will be constrained to
    38 // match the same entity for the wildcard.
    39 .with<Eats>("$food")
    40 .with<Healthy>().src("$food")
    41 .build();
    42
    43 // Lookup the index of the variable. This will let us quickly lookup its
    44 // value while we're iterating.
    45 int food_var = q.find_var("food");
    46
    47 // Iterate the query
    48 q.each([&](flecs::iter& it, size_t index) {
    49 std::cout
    50 << it.entity(index).name()
    51 << " eats " << it.get_var(food_var).name()
    52 << "\n";
    53 });
    54
    55 // Output:
    56 // Bob eats Apples
    57 // Alice eats Apples
    58 // Alice eats Salad
    59}
    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
    flecs::entity get_var(int var_id) const
    Get value of variable by ID.
    Definition iter.hpp:128
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129