Skip to content
Flecs v4.1
queries/iter_info

  • C++

    View on GitHub

    1#include <iter_info.h>
    2#include <iostream>
    3
    4struct Position {
    5 double x, y;
    6};
    7
    8struct Velocity {
    9 double x, y;
    10};
    11
    12struct Mass {
    13 double value;
    14};
    15
    16int main(int, char *[]) {
    17 flecs::world ecs;
    18
    19 // Create a query for Position, Velocity.
    21 ecs.query<Position, const Velocity>();
    22
    23 // Create a few test entities for a Position, Velocity query
    24 ecs.entity("e1")
    25 .set<Position>({10, 20})
    26 .set<Velocity>({1, 2});
    27
    28 ecs.entity("e2")
    29 .set<Position>({10, 20})
    30 .set<Velocity>({3, 4});
    31
    32 ecs.entity("e3")
    33 .set<Position>({10, 20})
    34 .set<Velocity>({4, 5})
    35 .set<Mass>({50});
    36
    37 // The run() function provides a flecs::iter object which contains all sorts
    38 // of information on the entities currently being iterated.
    39 q.run([&](flecs::iter& it) {
    40 while (it.next()) {
    41 auto p = it.field<Position>(0);
    42 auto v = it.field<const Velocity>(1);
    43
    44 // Print the table & number of entities matched in current callback
    45 std::cout << "Table [" << it.type().str() << "]" << std::endl;
    46 std::cout << " - number of entities: " << it.count() << std::endl;
    47
    48 // Print information about the components being matched
    49 for (int8_t i = 0; i < it.field_count(); i ++) {
    50 std::cout << " - term " << i << ": " << std::endl;
    51 std::cout << " - component: " << it.id(i).str() << std::endl;
    52 std::cout << " - type size: " << it.size(i) << std::endl;
    53 }
    54
    55 std::cout << std::endl;
    56
    57 // Iterate entities
    58 for (auto i : it) {
    59 p[i].x += v[i].x;
    60 p[i].y += v[i].y;
    61 std::cout << " - " << it.entity(i).name() <<
    62 ": {" << p[i].x << ", " << p[i].y << "}\n";
    63 }
    64
    65 std::cout << std::endl;
    66 }
    67 });
    68}
    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 str() const
    Convert ID to string.
    Definition decl.hpp:128
    Class for iterating over query results.
    Definition iter.hpp:68
    flecs::id id(int8_t index) const
    Obtain the ID matched for the field.
    Definition iter.hpp:48
    size_t size(int8_t index) const
    Size of the field data type.
    Definition iter.hpp:237
    flecs::field< A > field(int8_t index) const
    Get read-only access to field data.
    Definition iter.hpp:84
    flecs::type type() const
    Get the type of the iterated table.
    Definition iter.hpp:62
    int32_t field_count() const
    Number of fields in the iterator.
    Definition iter.hpp:228
    size_t count() const
    Get the number of entities to iterate over.
    Definition iter.hpp:112
    bool next()
    Progress iterator.
    Definition iter.hpp:465
    void run(Func &&func) const
    Run the iterator.
    Definition iterable.hpp:53
    Typed query.
    Definition impl.hpp:271
    flecs::string str() const
    Convert the type to a comma-separated string.
    Definition type.hpp:38
    The world.
    Definition world.hpp:129