Skip to content
Flecs v4.1
observers/custom_event

  • C

    View on GitHub

    1#include <custom_event.h>
    2#include <stdio.h>
    3
    4// Observers can be used to match custom events. Custom events can be emitted
    5// using the ecs_emit function. This function is also used by builtin events,
    6// so builtin and custom events use the same rules for matching with observers.
    7//
    8// An event consists out of three pieces of data used to match with observers:
    9// - An single event kind (EcsOnAdd, EcsOnRemove, ...)
    10// - One or more event ids (Position, Velocity, ...)
    11// - A source (either an entity or a table)
    12
    13typedef struct {
    14 double x, y;
    15} Position;
    16
    17void Observer(ecs_iter_t *it) {
    18 ecs_world_t *ecs = it->stage;
    19
    20 // The event kind
    21 ecs_entity_t event = it->event;
    22
    23 // The (component) id for which the event was emitted
    24 ecs_entity_t event_id = it->event_id;
    25
    26 for (int i = 0; i < it->count; i ++) {
    27 ecs_entity_t e = it->entities[i];
    28
    29 printf(" - %s: %s: %s\n",
    30 ecs_get_name(ecs, event),
    31 ecs_get_name(ecs, event_id),
    32 ecs_get_name(ecs, e));
    33 }
    34}
    35
    36int main(int argc, char *argv[]) {
    37 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    38
    39 ECS_COMPONENT(ecs, Position);
    40
    41 // Create custom event
    42 ecs_entity_t MyEvent = ecs_entity(ecs, { .name = "MyEvent" });
    43
    44 // Create observer for custom event
    45 ECS_OBSERVER(ecs, Observer, MyEvent, Position);
    46
    47 // Create entity
    48 ecs_entity_t e = ecs_entity(ecs, { .name = "e" });
    49
    50 // The observer query can be matched against the entity, so make sure it
    51 // has the Position component before emitting the event. This does not
    52 // trigger the observer yet.
    53 ecs_set(ecs, e, Position, {10, 20});
    54
    55 // Emit the custom event
    57 .event = MyEvent,
    58 .ids = &(ecs_type_t){ (ecs_id_t[]){ ecs_id(Position) }, 1 }, // 1 id
    59 .entity = e
    60 });
    61
    62 ecs_fini(ecs);
    63
    64 // Output
    65 // - MyEvent: Position: e
    66}
    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
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ECS_OBSERVER(world, id, kind,...)
    Declare and define an observer.
    Definition flecs_c.h:136
    #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
    void ecs_emit(ecs_world_t *world, ecs_event_desc_t *desc)
    Send an event.
    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.
    Used with ecs_emit().
    Definition flecs.h:1450
    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
    A type is a list of (component) IDs.
    Definition flecs.h:412

  • C++

    View on GitHub

    1#include <custom_event.h>
    2#include <iostream>
    3
    4// Observers can be used to match custom events. Custom events can be emitted
    5// using the ecs_emit function. This function is also used by builtin events,
    6// so builtin and custom events use the same rules for matching with observers.
    7//
    8// An event consists out of three pieces of data used to match with observers:
    9// - An single event kind (EcsOnAdd, EcsOnRemove, ...)
    10// - One or more event ids (Position, Velocity, ...)
    11// - A source (either an entity or a table)
    12
    13struct Position {
    14 double x, y;
    15};
    16
    17// Create tag type to use as event (could also use entity)
    18struct MyEvent { };
    19
    20int main(int, char *[]) {
    21 flecs::world ecs;
    22
    23 // Create observer for custom event
    24 ecs.observer<Position>()
    25 .event<MyEvent>()
    26 .each([](flecs::iter& it, size_t i, Position&) {
    27 std::cout << " - " << it.event().name() << ": "
    28 << it.event_id().str() << ": "
    29 << it.entity(i).name() << "\n";
    30 });
    31
    32 // The observer query can be matched against the entity, so make sure it
    33 // has the Position component before emitting the event. This does not
    34 // trigger the observer yet.
    35 flecs::entity e = ecs.entity("e")
    36 .set<Position>({10, 20});
    37
    38 // Emit the custom event
    39 ecs.event<MyEvent>()
    40 .id<Position>()
    41 .entity(e)
    42 .emit();
    43
    44 // Output
    45 // - MyEvent: Position: e
    46}
    flecs::event_builder event(flecs::entity_t evt) const
    Create a new event.
    Definition impl.hpp:15
    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
    Base & entity(flecs::entity_t e)
    Set the entity for which to emit the event.
    Definition builder.hpp:37
    void emit()
    Emit the event.
    Definition builder.hpp:63
    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
    observer & each(Func &&func)
    Replace the observer's each callback.
    Definition impl.hpp:56
    The world.
    Definition world.hpp:129