Skip to content
Flecs v4.1
queries/each_w_entity_callback

  • C++

    View on GitHub

    1#include <each_w_entity_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 matched entity.
    6
    7struct Position {
    8 double x, y;
    9};
    10
    11struct Velocity {
    12 double x, y;
    13};
    14
    15int main(int, char *[]) {
    16 flecs::world ecs;
    17
    18 // Create a query for Position, Velocity.
    20 ecs.query<Position, const Velocity>();
    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 // Arguments passed to each match components passed to system
    36 q.each([](flecs::entity e, Position& p, const Velocity& v) {
    37 p.x += v.x;
    38 p.y += v.y;
    39 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    40 });
    41
    42 // Output
    43 // e1: {11, 22}
    44 // e2: {13, 24}
    45}
    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
    void each(Func &&func) const
    Each iterator.
    Definition iterable.hpp:37
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129