Skip to content
Flecs v4.1
observers/entity_event

  • C

    View on GitHub

    1#include <entity_event.h>
    2#include <stdio.h>
    3
    4// Entity events are events that are emitted and observed for a specific entity.
    5// They are a thin wrapper around regular observers, which match against queries
    6// instead of single entities. While they work similarly under the hood, entity
    7// events provide a much simpler API.
    8//
    9// An entity event only needs two pieces of data:
    10// - The entity on which to emit the event
    11// - The event to emit
    12//
    13// Any (_) is provided as component, indicating that we're not emitting an event
    14// for a specific component but rather for the entity itself.
    15
    16// Event without payload
    17ECS_TAG_DECLARE(Click);
    18
    19// Event with payload
    20typedef struct {
    21 double width, height;
    22} Resize;
    23
    25
    26void OnClick(ecs_iter_t *it) {
    27 // The event source can be obtained with ecs_field_src(1). This allows the
    28 // same event function to be used for different entities.
    29 char *path = ecs_get_path(it->stage, ecs_field_src(it, 0));
    30 printf("clicked on %s!\n", path);
    31 ecs_os_free(path);
    32}
    33
    34void OnResize(ecs_iter_t *it) {
    35 // Event payload can be obtained from the it->param member
    36 Resize *p = it->param;
    37 char *path = ecs_get_path(it->stage, ecs_field_src(it, 0));
    38 printf("resized %s to {%.0f, %.0f}!\n", path, p->width, p->height);
    39 ecs_os_free(path);
    40}
    41
    42int main(int argc, char *argv[]) {
    43 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    44
    45 ECS_TAG_DEFINE(ecs, Click);
    46 ECS_COMPONENT_DEFINE(ecs, Resize);
    47
    48 // Create a widget entity
    49 ecs_entity_t widget = ecs_entity(ecs, { .name = "MyWidget" });
    50
    51 // Create entity observer. Use EcsAny to indicate we're not interested in
    52 // matching specific components.
    53 ecs_observer(ecs, {
    54 .query.terms = {{ .id = EcsAny, .src.id = widget }},
    55 .events = { Click },
    56 .callback = OnClick
    57 });
    58
    59 // Create another one for the Resize event
    60 ecs_observer(ecs, {
    61 .query.terms = {{ .id = EcsAny, .src.id = widget }},
    62 .events = { ecs_id(Resize) },
    63 .callback = OnResize
    64 });
    65
    66 // Emit the Click event
    68 .event = Click,
    69 .entity = widget
    70 });
    71
    72 // Emit the Resize event
    74 .event = ecs_id(Resize),
    75 .entity = widget,
    76 .param = &(Resize){100, 200} // pass payload
    77 });
    78
    79 ecs_fini(ecs);
    80
    81 // Output
    82 // clicked on MyWidget!
    83 // resized MyWidget to {100, 200}!
    84}
    const ecs_entity_t EcsAny
    Any 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_TAG_DECLARE
    Forward declare a tag.
    Definition flecs_c.h:29
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ECS_COMPONENT_DEFINE(world, id_)
    Define a forward-declared component.
    Definition flecs_c.h:75
    #define ecs_observer(world,...)
    Shorthand for creating an observer with ecs_observer_init().
    Definition flecs_c.h:230
    #define ECS_TAG_DEFINE(world, id_)
    Define a forward-declared tag.
    Definition flecs_c.h:39
    #define ECS_COMPONENT_DECLARE(id)
    Forward declare a component.
    Definition flecs_c.h:65
    #define ecs_get_path(world, child)
    Get path from root.
    Definition flecs_c.h:594
    ecs_entity_t ecs_field_src(const ecs_iter_t *it, int8_t index)
    Return the field source.
    void ecs_emit(ecs_world_t *world, ecs_event_desc_t *desc)
    Send an event.
    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
    void * param
    Param passed to ecs_run().
    Definition flecs.h:1221

  • C++

    View on GitHub

    1#include <iostream>
    2#include <entity_event.h>
    3
    4// Entity events are events that are emitted and observed for a specific entity.
    5// They are a thin wrapper around regular observers, which match against queries
    6// instead of single entities. While they work similarly under the hood, entity
    7// events provide a much simpler API.
    8//
    9// An entity event only needs two pieces of data:
    10// - The entity on which to emit the event
    11// - The event to emit
    12
    13// An event without payload
    14struct Click { };
    15
    16// An event with payload
    17struct Resize {
    18 double width, height;
    19};
    20
    21int main(int, char *[]) {
    22 flecs::world ecs;
    23
    24 // Create a widget entity
    25 flecs::entity widget = ecs.entity("MyWidget");
    26
    27 // Observe the Click event on the widget entity.
    28 widget.observe<Click>([]() {
    29 std::cout << "clicked!\n";
    30 });
    31
    32 // Observers can have an entity argument that holds the event source.
    33 // This allows the same function to be reused for different entities.
    34 widget.observe<Click>([](flecs::entity src) {
    35 std::cout << "clicked on " << src.path() << "!\n";
    36 });
    37
    38 // Observe the Resize event. Events with payload are passed as arguments
    39 // to the observer callback.
    40 widget.observe([](Resize& p) {
    41 std::cout << "resized to {"
    42 << p.width << ", " << p.height << "}!\n";
    43 });
    44
    45 // Emit the Click event
    46 widget.emit<Click>();
    47
    48 // Emit the Resize event
    49 widget.emit<Resize>({100, 200});
    50
    51 // Output
    52 // clicked!
    53 // clicked on ::MyWidget!
    54 // resized to {100, 200}!
    55}
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    const Self & observe(flecs::entity_t evt, Func &&callback) const
    Observe an event on an entity.
    void emit(flecs::entity_t evt) const
    Emit an event for an entity.
    Entity.
    Definition entity.hpp:30
    The world.
    Definition world.hpp:129