Skip to content
Flecs v4.1
systems/multiple_queries

  • C++

    View on GitHub

    1#include <multiple_queries.h>
    2#include <iostream>
    3
    4// This example shows how to pass one or more existing queries to a system,
    5// which allows for systems that iterate queries multiple times and/or iterate
    6// multiple queries.
    7
    8struct Position {
    9 float x, y;
    10};
    11
    12struct Bee {
    13 static const int MaxRange = 10;
    14 flecs::entity flower;
    15};
    16
    17struct Flower { };
    18
    19float randf(int n) {
    20 return static_cast<float>(rand() % n);
    21}
    22
    23float pow2(float n) {
    24 return n * n;
    25}
    26
    27int main(int, char *[]) {
    28 flecs::world ecs;
    29
    30 // Create two queries. Since they'll stick around until the end of the ECS
    31 // world (or until we delete them) give them a name which makes them easier
    32 // to find in the explorer.
    33 // Giving the query a name also causes the caching policy to default to
    34 // cached. Named queries are (very) likely candidates for being evaluated
    35 // many times, which is the sweet spot for caching.
    36
    38 ecs.query<const Position, Bee>("BeeQuery");
    39
    41 ecs.query_builder<const Position>("FlowerQuery")
    42 .with<Flower>()
    43 .build();
    44
    45 // Find the closest in range flower for each bee.
    46 ecs.system("FlowersAndTheBees")
    47 // Capture queries by value. This prevents common bugs caused by local
    48 // variables going out of scope before the system runs.
    49 .run([bees, flowers](flecs::iter&) {
    50 bees.each([&](flecs::entity bee, const Position& p_bee, Bee& b) {
    51 float dsqr_min = 2 * pow2(Bee::MaxRange);
    52
    53 b.flower = flecs::entity();
    54
    55 flowers.each([&](flecs::entity flower, const Position& p_flower) {
    56 float dsqr = pow2(p_bee.x - p_flower.x) +
    57 pow2(p_bee.y - p_flower.y);
    58
    59 if (dsqr < dsqr_min) {
    60 dsqr_min = dsqr;
    61 b.flower = flower;
    62 }
    63 });
    64
    65 if (b.flower) {
    66 std::cout << "Bee " << bee.path()
    67 << " picked flower " << b.flower.path()
    68 << std::endl;
    69 }
    70 });
    71 });
    72
    73 // Find bees that picked the same flower.
    74 ecs.system("BumpingBees")
    75 .run([bees](flecs::iter&) {
    76 bees.each([&](flecs::entity bee1, const Position&, Bee& b1) {
    77 bees.each([&](flecs::entity bee2, const Position&, Bee& b2) {
    78 if (bee1 > bee2) { // Don't handle the same collision twice
    79 if (b1.flower && b1.flower == b2.flower) {
    80 std::cout << "Bee " << bee1.path()
    81 << " and bee " << bee2.path()
    82 << " bumped into each other"
    83 << std::endl;
    84 }
    85 }
    86 });
    87 });
    88 });
    89
    90 // Create flowers and bees
    91 for (int i = 0; i < 5; i ++) {
    92 ecs.entity()
    93 .add<Bee>() // Add with default value
    94 .set(Position{ randf(20), randf(20) });
    95 }
    96
    97 for (int i = 0; i < 10; i ++) {
    98 ecs.entity()
    99 .add<Flower>()
    100 .set(Position{ randf(20), randf(20) });
    101 }
    102
    103 ecs.progress();
    104
    105 // Output
    106 // Bee #544 picked flower #552
    107 // Bee #545 picked flower #555
    108 // Bee #546 picked flower #552
    109 // Bee #547 picked flower #555
    110 // Bee #548 picked flower #554
    111 // Bee #546 and bee #544 bumped into each other
    112 // Bee #547 and bee #545 bumped into each other
    113}
    flecs::system system(flecs::entity e) const
    Upcast an entity to a system.
    Definition impl.hpp:181
    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::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
    bool progress(ecs_ftime_t delta_time=0.0) const
    Progress the world one tick.
    Definition impl.hpp:47
    flecs::string path(const char *sep="::", const char *init_sep="::") const
    Return the entity path.
    Entity.
    Definition entity.hpp:30
    Class for iterating over query results.
    Definition iter.hpp:68
    void each(Func &&func) const
    Each iterator.
    Definition iterable.hpp:37
    Typed query.
    Definition impl.hpp:271
    system & run(Func &&func)
    Replace the system's run callback.
    Definition impl.hpp:108
    The world.
    Definition world.hpp:129