Skip to content
Flecs v4.1
queries/without

  • C++

    View on GitHub

    1#include <without.h>
    2#include <iostream>
    3
    4struct Position {
    5 double x, y;
    6};
    7
    8struct Npc { };
    9
    10int main(int, char *[]) {
    11 flecs::world ecs;
    12
    13 // Create a query for Position, !Npc. By adding the Npc component using the
    14 // "without" method, the component is not a part of the query type, and as a
    15 // result does not become part of the function signatures of each and iter.
    16 // This is useful for things like tags, which because they don't have a
    17 // value are less useful to pass to the each/iter functions as argument.
    18 //
    19 // The without method is short for:
    20 // .term<Npc>().not_()
    21 flecs::query<Position> q = ecs.query_builder<Position>()
    22 .without<Npc>()
    23 .build();
    24
    25 // Create a few test entities for the Position query
    26 ecs.entity("e1")
    27 .set<Position>({10, 20});
    28
    29 ecs.entity("e2")
    30 .set<Position>({10, 20});
    31
    32 // This entity will not match as it has Npc
    33 ecs.entity("e3")
    34 .set<Position>({10, 20})
    35 .add<Npc>();
    36
    37
    38 // Note how the Npc tag is not part of the each signature
    39 q.each([](flecs::entity e, Position& p) {
    40 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    41 });
    42
    43 // Output:
    44 // e1: {10, 20}
    45 // e2: {10, 20}
    46}
    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
    void each(Func &&func) const
    Each iterator.
    Definition iterable.hpp:37
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129