Skip to content
Flecs v4.1
observers/yield_existing

  • C

    View on GitHub

    1#include <yield_existing.h>
    2#include <stdio.h>
    3
    4// Observers can enable a "yield_existing" feature that upon creation of the
    5// observer produces events for all entities that match the observer query. The
    6// feature is only implemented for the builtin EcsOnAdd and EcsOnSet events.
    7
    8typedef struct {
    9 double x, y;
    10} Position;
    11
    12void Observer(ecs_iter_t *it) {
    13 ecs_world_t *ecs = it->stage;
    14
    15 ecs_entity_t event = it->event;
    16 ecs_entity_t event_id = it->event_id;
    17 Position *p = ecs_field(it, Position, 0);
    18
    19 for (int i = 0; i < it->count; i ++) {
    20 ecs_entity_t e = it->entities[i];
    21 printf(" - %s: %s: %s: {%f, %f}\n",
    22 ecs_get_name(ecs, event),
    23 ecs_get_name(ecs, event_id),
    24 ecs_get_name(ecs, e),
    25 p[i].x, p[i].y);
    26 }
    27}
    28
    29int main(int argc, char *argv[]) {
    30 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    31
    32 ECS_COMPONENT(ecs, Position);
    33
    34 // Create existing entities with Position component
    35 ecs_entity_t e1 = ecs_entity(ecs, { .name = "e1" });
    36 ecs_set(ecs, e1, Position, {10, 20});
    37 ecs_entity_t e2 = ecs_entity(ecs, { .name = "e2" });
    38 ecs_set(ecs, e2, Position, {20, 30});
    39
    40 ecs_observer(ecs, {
    41 .query.terms = {{ .id = ecs_id(Position) }},
    42 .events = { EcsOnSet },
    43 .callback = Observer,
    44 .yield_existing = true // Trigger for existing matching entities
    45 });
    46
    47 ecs_fini(ecs);
    48
    49 // Output
    50 // - OnSet: Position: e1: {10.000000, 20.000000}
    51 // - OnSet: Position: e2: {20.000000, 30.000000}
    52}
    const ecs_entity_t EcsOnSet
    Event that triggers when a component is set for an entity.
    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
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ecs_observer(world,...)
    Shorthand for creating an observer with ecs_observer_init().
    Definition flecs_c.h:230
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    #define ecs_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    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.
    Iterator.
    Definition flecs.h:1183
    ecs_entity_t event
    The event (if applicable).
    Definition flecs.h:1209
    ecs_id_t event_id
    The (component) ID for the event.
    Definition flecs.h:1210
    int32_t count
    Number of entities to iterate.
    Definition flecs.h:1189
    const ecs_entity_t * entities
    Entity identifiers.
    Definition flecs.h:1190

  • C++

    View on GitHub

    1#include <yield_existing.h>
    2#include <iostream>
    3
    4// Observers can enable a "yield_existing" feature that upon creation of the
    5// observer produces events for all entities that match the observer query. The
    6// feature is only implemented for the builtin EcsOnAdd and EcsOnSet events.
    7
    8struct Position {
    9 double x, y;
    10};
    11
    12int main(int, char *[]) {
    13 flecs::world ecs;
    14
    15 // Create existing entities with Position component
    16 ecs.entity("e1").set<Position>({10, 20});
    17 ecs.entity("e2").set<Position>({20, 30});
    18
    19 // Create an observer for three events
    20 ecs.observer<Position>()
    21 .event(flecs::OnSet)
    22 .yield_existing() // Trigger for existing matching entities
    23 .each([](flecs::iter& it, size_t i, Position& p) {
    24 std::cout << " - " << it.event().name() << ": "
    25 << it.event_id().str() << ": "
    26 << it.entity(i).name()
    27 << ": {" << p.x << ", " << p.y << "}\n";
    28 });
    29
    30 // Output
    31 // - OnSet: Position: e1: {10, 20}
    32 // - OnSet: Position: e2: {20, 30}
    33}
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::observer observer(flecs::entity e) const
    Observer world mixin.
    Definition impl.hpp:85
    flecs::string_view name() const
    Return the entity name.
    flecs::string str() const
    Convert ID to string.
    Definition decl.hpp:128
    Class for iterating over query results.
    Definition iter.hpp:68
    flecs::entity event() const
    Get the event entity associated with the iterator.
    Definition iter.hpp:17
    flecs::entity entity(size_t row) const
    Obtain a mutable handle to the entity being iterated over.
    Definition iter.hpp:36
    flecs::id event_id() const
    Get the event ID associated with the iterator.
    Definition iter.hpp:22
    The world.
    Definition world.hpp:129
    flecs::event_builder event(flecs::entity_t evt) const
    Create a new event.