Skip to content
Flecs v4.1
queries/world_query

  • C++

    View on GitHub

    1#include <world_query.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 few test entities for a Position, Velocity query
    16 ecs.entity("e1")
    17 .set<Position>({10, 20})
    18 .set<Velocity>({1, 2});
    19
    20 ecs.entity("e2")
    21 .set<Position>({10, 20})
    22 .set<Velocity>({3, 4});
    23
    24 // This entity will not match as it does not have Position, Velocity
    25 ecs.entity("e3")
    26 .set<Position>({10, 20});
    27
    28 // world::each is a quick way to run simple component queries.
    29 ecs.each([](flecs::entity e, Position& p, Velocity& v) {
    30 p.x += v.x;
    31 p.y += v.y;
    32 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    33 });
    34}
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.
    Definition impl.hpp:368
    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
    The world.
    Definition world.hpp:129