Skip to content
Flecs v4.1
observers/monitor

  • C

    View on GitHub

    1#include <monitor.h>
    2#include <stdio.h>
    3
    4// A monitor observer triggers when an entity starts/stop matching the observer
    5// query. The observer communicates whether an entity is "entering/leaving" the
    6// monitor by setting ecs_iter_t::event to EcsOnAdd (for entering) or
    7// EcsOnRemove (for leaving).
    8//
    9// To specify that an observer is a monitor observer, the EcsMonitor tag must be
    10// provided as event. No additional event kinds should be provided for a monitor
    11// observer.
    12
    13typedef struct {
    14 double x, y;
    15} Position, Velocity;
    16
    17void Observer(ecs_iter_t *it) {
    18 ecs_world_t *ecs = it->stage;
    19
    20 ecs_entity_t event = it->event;
    21 ecs_entity_t event_id = it->event_id;
    22
    23 for (int i = 0; i < it->count; i ++) {
    24 ecs_entity_t e = it->entities[i];
    25
    26 if (event == EcsOnAdd) {
    27 printf(" - Enter: %s: %s\n",
    28 ecs_get_name(ecs, event_id),
    29 ecs_get_name(ecs, e));
    30 } else if (event == EcsOnRemove) {
    31 printf(" - Leave: %s: %s\n",
    32 ecs_get_name(ecs, event_id),
    33 ecs_get_name(ecs, e));
    34 }
    35 }
    36}
    37
    38int main(int argc, char *argv[]) {
    39 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    40
    41 ECS_COMPONENT(ecs, Position);
    42 ECS_COMPONENT(ecs, Velocity);
    43
    44 ecs_observer(ecs, {
    45 .query = { .terms = {{ .id = ecs_id(Position) }, { .id = ecs_id(Velocity) }}},
    46 .events = { EcsMonitor }, // Monitor entities entering/leaving the query
    47 .callback = Observer,
    48 });
    49
    50 // Create entity
    51 ecs_entity_t e = ecs_entity(ecs, { .name = "e" });
    52
    53 // This does not yet trigger the monitor, as the entity does not yet match.
    54 ecs_set(ecs, e, Position, {10, 20});
    55
    56 // This triggers the monitor with EcsOnAdd, as the entity now matches.
    57 ecs_set(ecs, e, Velocity, {1, 2});
    58
    59 // This triggers the monitor with EcsOnRemove, as the entity no longer matches.
    60 ecs_remove(ecs, e, Position);
    61
    62 ecs_fini(ecs);
    63
    64 // Output
    65 // - Enter: Velocity: e
    66 // - Leave: Position: e
    67}
    const ecs_entity_t EcsOnRemove
    Event that triggers when an ID is removed from an entity.
    const ecs_entity_t EcsMonitor
    Event that triggers an observer when an entity starts or stops matching a query.
    const ecs_entity_t EcsOnAdd
    Event that triggers when an ID is added to 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_remove(world, entity, T)
    Remove a component from an entity.
    Definition flecs_c.h:279
    #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
    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 <monitor.h>
    2#include <iostream>
    3
    4// A monitor observer triggers when an entity starts/stop matching the observer
    5// query. The observer communicates whether an entity is "entering/leaving" the
    6// monitor by setting ecs_iter_t::event to EcsOnAdd (for entering) or
    7// EcsOnRemove (for leaving).
    8//
    9// To specify that an observer is a monitor observer, the EcsMonitor tag must be
    10// provided as event. No additional event kinds should be provided for a monitor
    11// observer.
    12
    13struct Position {
    14 double x, y;
    15};
    16
    17struct Velocity {
    18 double x, y;
    19};
    20
    21int main(int, char *[]) {
    22 flecs::world ecs;
    23
    24 // Create observer for custom event
    25 ecs.observer<Position, Velocity>()
    26 .event(flecs::Monitor) // Monitor entities entering/leaving the query
    27 .each([](flecs::iter& it, size_t i, Position&, Velocity&) {
    28 if (it.event() == flecs::OnAdd) {
    29 std::cout << " - Enter: "
    30 << it.event_id().str() << ": "
    31 << it.entity(i).name() << "\n";
    32 } else if (it.event() == flecs::OnRemove) {
    33 std::cout << " - Leave: "
    34 << it.event_id().str() << ": "
    35 << it.entity(i).name() << "\n";
    36 }
    37 });
    38
    39 // Create entity
    40 flecs::entity e = ecs.entity("e");
    41
    42 // This does not yet trigger the monitor, as the entity does not yet match.
    43 e.set<Position>({10, 20});
    44
    45 // This triggers the monitor with EcsOnAdd, as the entity now matches.
    46 e.set<Velocity>({1, 2});
    47
    48 // This triggers the monitor with EcsOnRemove, as the entity no longer matches.
    49 e.remove<Position>();
    50
    51 // Output
    52 // - Enter: Velocity: e
    53 // - Leave: Position: e
    54}
    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.
    Entity.
    Definition entity.hpp:30
    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.