Skip to content
Flecs v4.1
queries/basics

  • C

    View on GitHub

    1#include <basics.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x;
    6 double y;
    7} Position, Velocity;
    8
    9int main(int argc, char *argv[]) {
    10 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    11
    12 ECS_COMPONENT(ecs, Position);
    13 ECS_COMPONENT(ecs, Velocity);
    14
    15 // Create a cached query for Position, Velocity. Cached queries are the
    16 // fastest way to iterate entities as they cache results.
    17 ecs_query_t *q = ecs_query(ecs, {
    18 .terms = {
    19 { .id = ecs_id(Position) },
    20 { .id = ecs_id(Velocity), .inout = EcsIn}
    21 },
    22
    23 // QueryCache Auto automatically caches all terms that can be cached.
    24 .cache_kind = EcsQueryCacheAuto
    25 });
    26
    27 // Create a few test entities for a Position, Velocity query
    28 ecs_entity_t e1 = ecs_entity(ecs, { .name = "e1" });
    29 ecs_set(ecs, e1, Position, {10, 20});
    30 ecs_set(ecs, e1, Velocity, {1, 2});
    31
    32 ecs_entity_t e2 = ecs_entity(ecs, { .name = "e2" });
    33 ecs_set(ecs, e2, Position, {10, 20});
    34 ecs_set(ecs, e2, Velocity, {3, 4});
    35
    36 // This entity will not match as it does not have Position, Velocity
    37 ecs_entity_t e3 = ecs_entity(ecs, { .name = "e3" });
    38 ecs_set(ecs, e3, Position, {10, 20});
    39
    40 // Iterate entities matching the query
    41 ecs_iter_t it = ecs_query_iter(ecs, q);
    42
    43 // Outer loop, iterates archetypes
    44 while (ecs_query_next(&it)) {
    45 Position *p = ecs_field(&it, Position, 0);
    46 const Velocity *v = ecs_field(&it, Velocity, 1);
    47
    48 // Inner loop, iterates entities in archetype
    49 for (int i = 0; i < it.count; i ++) {
    50 p[i].x += v[i].x;
    51 p[i].y += v[i].y;
    52 printf("%s: {%f, %f}\n", ecs_get_name(ecs, it.entities[i]),
    53 p[i].x, p[i].y);
    54 }
    55 }
    56
    57 // Cleanup query resources.
    59
    60 return ecs_fini(ecs);
    61}
    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_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_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
    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.
    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.
    @ EcsQueryCacheAuto
    Cache query terms that are cacheable.
    Definition flecs.h:749
    @ EcsIn
    Term is only read.
    Definition flecs.h:731
    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

  • C++

    View on GitHub

    1#include <basics.h>
    2#include <iostream>
    3
    4struct Position {
    5 double x, y;
    6};
    7
    8struct Velocity {
    9 double x, y;
    10};
    11
    12int main(int, char *[]) {
    13 flecs::world ecs;
    14
    15 // Create a cached query for Position, Velocity. Cached queries are the
    16 // fastest way to iterate entities as they cache results.
    18 ecs.query_builder<Position, const Velocity>()
    19 .cached()
    20 .build();
    21
    22 // Create a few test entities for a Position, Velocity query
    23 ecs.entity("e1")
    24 .set<Position>({10, 20})
    25 .set<Velocity>({1, 2});
    26
    27 ecs.entity("e2")
    28 .set<Position>({10, 20})
    29 .set<Velocity>({3, 4});
    30
    31 // This entity will not match as it does not have Position, Velocity
    32 ecs.entity("e3")
    33 .set<Position>({10, 20});
    34
    35
    36 // The next lines show the different ways in which a query can be iterated.
    37 // Note how the 'const' qualifier matches the query template arguments.
    38
    39 // The each() function iterates each entity individually and accepts an
    40 // entity argument plus arguments for each query component:
    41 q.each([](flecs::entity e, Position& p, const Velocity& v) {
    42 p.x += v.x;
    43 p.y += v.y;
    44 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    45 });
    46
    47 // You can omit the flecs::entity argument if it's not needed:
    48 q.each([](Position& p, const Velocity& v) {
    49 p.x += v.x;
    50 p.y += v.y;
    51 std::cout << "{" << p.x << ", " << p.y << "}\n";
    52 });
    53
    54 // Each also accepts flecs::iter + index (for the iterated entity) arguments
    55 // currently being iterated. A flecs::iter has lots of information on what
    56 // is being iterated, which is demonstrated in the "iter" example.
    57 q.each([](flecs::iter& it, size_t i, Position& p, const Velocity& v) {
    58 p.x += v.x;
    59 p.y += v.y;
    60 std::cout << it.entity(i).name() << ": {" << p.x << ", " << p.y << "}\n";
    61 });
    62
    63 // Run is a bit more verbose, but allows for more control over how entities
    64 // are iterated as it provides multiple entities in the same callback.
    65 q.run([](flecs::iter& it) {
    66 while (it.next()) {
    67 auto p = it.field<Position>(0);
    68 auto v = it.field<const Velocity>(1);
    69
    70 for (auto i : it) {
    71 p[i].x += v[i].x;
    72 p[i].y += v[i].y;
    73 std::cout << it.entity(i).name() <<
    74 ": {" << p[i].x << ", " << p[i].y << "}\n";
    75 }
    76 }
    77 });
    78}
    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::field< A > field(int8_t index) const
    Get read-only access to field data.
    Definition iter.hpp:84
    flecs::entity entity(size_t row) const
    Obtain a mutable handle to the entity being iterated over.
    Definition iter.hpp:36
    bool next()
    Progress iterator.
    Definition iter.hpp:465
    void run(Func &&func) const
    Run the iterator.
    Definition iterable.hpp:53
    void each(Func &&func) const
    Each iterator.
    Definition iterable.hpp:37
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129