Skip to content
Flecs v4.1
entities/iterate_components

  • C

    View on GitHub

    1#include <iterate_components.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x, y;
    6} Position, Velocity;
    7
    8void iterate_components(ecs_world_t *ecs, ecs_entity_t e) {
    9 // First get the entity's type, which is a vector of (component) ids.
    10 const ecs_type_t *type = ecs_get_type(ecs, e);
    11
    12 // 1. The easiest way to print the components is to use ecs_type_str
    13 char *type_str = ecs_type_str(ecs, type);
    14 printf("ecs_type_str: %s\n\n", type_str);
    15 ecs_os_free(type_str);
    16
    17 // 2. To print individual ids, iterate the type array with ecs_id_str
    18 const ecs_id_t *type_ids = type->array;
    19 int32_t i, count = type->count;
    20
    21 for (i = 0; i < count; i ++) {
    22 ecs_id_t id = type_ids[i];
    23 char *id_str = ecs_id_str(ecs, id);
    24 printf("%d: %s\n", i, id_str);
    25 ecs_os_free(id_str);
    26 }
    27
    28 printf("\n");
    29
    30 // 3. we can also inspect and print the ids in our own way. This is a
    31 // bit more complicated as we need to handle the edge cases of what can be
    32 // encoded in an id, but provides the most flexibility.
    33 for (i = 0; i < count; i ++) {
    34 ecs_id_t id = type_ids[i];
    35
    36 printf("%d: ", i);
    37
    38 if (ECS_HAS_ID_FLAG(id, PAIR)) { // See relationships
    39 ecs_entity_t rel = ecs_pair_first(ecs, id);
    40 ecs_entity_t tgt = ecs_pair_second(ecs, id);
    41 printf("rel: %s, tgt: %s",
    42 ecs_get_name(ecs, rel), ecs_get_name(ecs, tgt));
    43 } else {
    44 ecs_entity_t comp = id & ECS_COMPONENT_MASK;
    45 printf("entity: %s", ecs_get_name(ecs, comp));
    46 }
    47
    48 printf("\n");
    49 }
    50
    51 printf("\n\n");
    52}
    53
    54int main(int argc, char *argv[]) {
    55 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    56
    57 // Ordinary components
    58 ECS_COMPONENT(ecs, Position);
    59 ECS_COMPONENT(ecs, Velocity);
    60
    61 // A tag
    62 ECS_TAG(ecs, Human);
    63
    64 // Two tags used to create a pair
    65 ECS_TAG(ecs, Eats);
    66 ECS_TAG(ecs, Apples);
    67
    68 // Create an entity which all of the above
    69 ecs_entity_t Bob = ecs_new(ecs);
    70
    71 ecs_set(ecs, Bob, Position, {10, 20});
    72 ecs_set(ecs, Bob, Velocity, {1, 1});
    73 ecs_add(ecs, Bob, Human);
    74 ecs_add_pair(ecs, Bob, Eats, Apples);
    75
    76 // Iterate & components of Bob
    77 printf("Bob's components:\n");
    78 iterate_components(ecs, Bob);
    79
    80 // We can use the same function to iterate the components of a component
    81 printf("Position's components:\n");
    82 iterate_components(ecs, ecs_id(Position));
    83
    84 return ecs_fini(ecs);
    85}
    86
    87// Output:
    88
    89// Bob's components:
    90// ecs_type_str: Position,Velocity,Human,(Eats,Apples)
    91
    92// 0: Position
    93// 1: Velocity
    94// 2: Human
    95// 3: (Eats,Apples)
    96
    97// 0: entity: Position
    98// 1: entity: Velocity
    99// 2: entity: Human
    100// 3: rel: Eats, tgt: Eats
    101
    102
    103// Position's components:
    104// ecs_type_str: EcsComponent,(Identifier,Name),(Identifier,Symbol),(OnDelete,Panic)
    105
    106// 0: Component
    107// 1: (Identifier,Name)
    108// 2: (Identifier,Symbol)
    109// 3: (OnDelete,Panic)
    110
    111// 0: entity: Component
    112// 1: rel: Identifier, tgt: Identifier
    113// 2: rel: Identifier, tgt: Identifier
    114// 3: rel: OnDelete, tgt: OnDelete
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:395
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    uint64_t ecs_id_t
    IDs are the things that can be added to an entity.
    Definition flecs.h:388
    ecs_entity_t ecs_new(ecs_world_t *world)
    Create new entity ID.
    const ecs_type_t * ecs_get_type(const ecs_world_t *world, ecs_entity_t entity)
    Get the type of an entity.
    char * ecs_type_str(const ecs_world_t *world, const ecs_type_t *type)
    Convert a type to a string.
    #define ecs_add_pair(world, subject, first, second)
    Add a pair to an entity.
    Definition flecs_c.h:275
    #define ecs_add(world, entity, T)
    Add a component to an entity.
    Definition flecs_c.h:271
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ECS_TAG(world, id)
    Declare and define a tag.
    Definition flecs_c.h:59
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    char * ecs_id_str(const ecs_world_t *world, ecs_id_t component)
    Convert a component ID to a string.
    const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
    Get the name of an entity.
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.
    A type is a list of (component) IDs.
    Definition flecs.h:412
    ecs_id_t * array
    Array with IDs.
    Definition flecs.h:413
    int32_t count
    Number of elements in array.
    Definition flecs.h:414

  • C++

    View on GitHub

    1#include <iterate_components.h>
    2#include <iostream>
    3
    4// Ordinary components
    5struct Position {
    6 double x, y;
    7};
    8
    9struct Velocity {
    10 double x, y;
    11};
    12
    13// Tag
    14struct Human { };
    15
    16// Two tags used to create a pair
    17struct Eats { };
    18struct Apples { };
    19
    20void iterate_components(flecs::entity e) {
    21 // 1. The easiest way to print the components is to use type::str
    22 std::cout << e.type().str() << "\n\n";
    23
    24 // 2. To get individual component ids, use entity::each
    25 int32_t i = 0;
    26 e.each([&](flecs::id id) {
    27 std::cout << i++ << ": " << id.str() << "\n";
    28 });
    29 std::cout << "\n";
    30
    31 // 3. we can also inspect and print the ids in our own way. This is a
    32 // bit more complicated as we need to handle the edge cases of what can be
    33 // encoded in an id, but provides the most flexibility.
    34 i = 0;
    35 e.each([&](flecs::id id) {
    36 std::cout << i++ << ": ";
    37
    38 if (id.is_pair()) {
    39 // If id is a pair, extract & print both parts of the pair
    40 flecs::entity rel = id.first();
    41 flecs::entity tgt = id.second();
    42 std::cout << "rel: " << rel.name() << ", " << "tgt: " << tgt.name();
    43 } else {
    44 // Id contains a regular entity. Strip role before printing.
    45 flecs::entity comp = id.entity();
    46 std::cout << "entity: " << comp.name();
    47 }
    48
    49 std::cout << "\n";
    50 });
    51
    52 std::cout << "\n\n";
    53}
    54
    55int main(int, char *[]) {
    56 flecs::world ecs;
    57
    58 flecs::entity bob = ecs.entity()
    59 .set<Position>({10, 20})
    60 .set<Velocity>({1, 1})
    61 .add<Human>()
    62 .add<Eats, Apples>();
    63
    64 std::cout << "Bob's components:\n";
    65 iterate_components(bob);
    66
    67 // We can use the same function to iterate the components of a component
    68 std::cout << "Position's components:\n";
    69 iterate_components(ecs.component<Position>());
    70}
    71
    72// Output:
    73
    74// Bob's components:
    75// Position, Velocity, Human, (Eats,Apples)
    76//
    77// 0: Position
    78// 1: Velocity
    79// 2: Human
    80// 3: (Eats,Apples)
    81//
    82// 0: entity: Position
    83// 1: entity: Velocity
    84// 2: entity: Human
    85// 3: rel: Eats, tgt: Apples
    86//
    87//
    88// Position's components:
    89// Component, (Identifier,Name), (Identifier,Symbol), (OnDelete,Panic)
    90//
    91// 0: Component
    92// 1: (Identifier,Name)
    93// 2: (Identifier,Symbol)
    94// 3: (OnDelete,Panic)
    95//
    96// 0: entity: Component
    97// 1: rel: Identifier, tgt: Name
    98// 2: rel: Identifier, tgt: Symbol
    99// 3: rel: OnDelete, tgt: Panic
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::type type() const
    Get the entity's type.
    Definition impl.hpp:94
    flecs::string_view name() const
    Return the entity name.
    void each(const Func &func) const
    Iterate (component) IDs of an entity.
    Definition impl.hpp:155
    Entity.
    Definition entity.hpp:30
    entity()
    Default constructor.
    Definition entity.hpp:32
    Class that wraps around a flecs::id_t.
    Definition decl.hpp:27
    flecs::entity second() const
    Get second element from a pair.
    Definition impl.hpp:31
    flecs::entity first() const
    Get first element from a pair.
    Definition impl.hpp:20
    flecs::string str() const
    Convert the type to a comma-separated string.
    Definition type.hpp:38
    The world.
    Definition world.hpp:129