Skip to content
Flecs v4.1
queries/each_w_iter_callback

  • C++

    View on GitHub

    1#include <each_w_iter_callback.h>
    2#include <iostream>
    3
    4// This example is the same as the each example, but in addition also shows how
    5// to get access to the flecs::iter object. Flecs iterators provide access to a
    6// lot of information about the currently iterated result, such as the matched
    7// entities, table, component ids, whether fields are set or not and more.
    8
    9struct Position {
    10 double x, y;
    11};
    12
    13struct Velocity {
    14 double x, y;
    15};
    16
    17int main(int, char *[]) {
    18 flecs::world ecs;
    19
    20 // Create a query for Position, Velocity.
    22 ecs.query<Position, const Velocity>();
    23
    24 // Create a few test entities for a Position, Velocity query
    25 ecs.entity("e1")
    26 .set<Position>({10, 20})
    27 .set<Velocity>({1, 2});
    28
    29 ecs.entity("e2")
    30 .set<Position>({10, 20})
    31 .set<Velocity>({3, 4});
    32
    33 // This entity will not match as it does not have Position, Velocity
    34 ecs.entity("e3")
    35 .set<Position>({10, 20});
    36
    37 // Arguments passed to each match components passed to system
    38 q.each([](flecs::iter& it, size_t row, Position& p, const Velocity& v) {
    39 flecs::entity e = it.entity(row);
    40 p.x += v.x;
    41 p.y += v.y;
    42 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    43 });
    44
    45 // Output
    46 // e1: {11, 22}
    47 // e2: {13, 24}
    48}
    conditional_t< sizeof...(Args)==0 &&_::is_sparse_query< Comps... >::value, flecs::sparse_query< Comps... >, flecs::query< Comps... > > query(Args &&... args) const
    Create a query.
    Definition impl.hpp:326
    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 each(Func &&func) const
    Each iterator.
    Definition iterable.hpp:37
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129