Skip to content
Flecs v4.1
observers/enqueue_entity_event

  • C

    View on GitHub

    1#include <enqueue_entity_event.h>
    2#include <stdio.h>
    3
    4// This is the same example as entity_event, but instead of emit() the example
    5// uses the enqueue() method. Whereas emit() invokes observers synchronously,
    6// enqueue() adds the event to the command queue, which delays invoking
    7// observers until the next time the command queue is flushed.
    8
    9// Event without payload
    10ECS_TAG_DECLARE(Click);
    11
    12// Event with payload
    13typedef struct {
    14 double width, height;
    15} Resize;
    16
    18
    19void OnClick(ecs_iter_t *it) {
    20 // The event source can be obtained with ecs_field_src(1). This allows the
    21 // same event function to be used for different entities.
    22 char *path = ecs_get_path(it->stage, ecs_field_src(it, 0));
    23 printf("clicked on %s!\n", path);
    24 ecs_os_free(path);
    25}
    26
    27void OnResize(ecs_iter_t *it) {
    28 // Event payload can be obtained from the it->param member
    29 Resize *p = it->param;
    30 char *path = ecs_get_path(it->stage, ecs_field_src(it, 0));
    31 printf("resized %s to {%.0f, %.0f}!\n", path, p->width, p->height);
    32 ecs_os_free(path);
    33}
    34
    35int main(int argc, char *argv[]) {
    36 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    37
    38 ECS_TAG_DEFINE(ecs, Click);
    39 ECS_COMPONENT_DEFINE(ecs, Resize);
    40
    41 // Create a widget entity
    42 ecs_entity_t widget = ecs_entity(ecs, { .name = "MyWidget" });
    43
    44 // Create entity observer. Use EcsAny to indicate we're not interested in
    45 // matching specific components.
    46 ecs_observer(ecs, {
    47 .query.terms = {{ .id = EcsAny, .src.id = widget }},
    48 .events = { Click },
    49 .callback = OnClick
    50 });
    51
    52 // Create another one for the Resize event
    53 ecs_observer(ecs, {
    54 .query.terms = {{ .id = EcsAny, .src.id = widget }},
    55 .events = { ecs_id(Resize) },
    56 .callback = OnResize
    57 });
    58
    59 ecs_world_t *stage_1 = ecs_is_stage(ecs) ? ecs : ecs_get_stage(ecs, 0);
    60
    61 // Emit the Click event
    62 ecs_enqueue(stage_1, &(ecs_event_desc_t) {
    63 .event = Click,
    64 .entity = widget
    65 });
    66
    67 // Emit the Resize event
    68 ecs_enqueue(stage_1, &(ecs_event_desc_t) {
    69 .event = ecs_id(Resize),
    70 .entity = widget,
    71 .param = &(Resize){100, 200} // pass payload
    72 });
    73
    74 printf("Events enqueued\n");
    75
    76 // Flushes the queue, and invokes the observer
    77 ecs_merge(stage_1);
    78
    79 ecs_fini(ecs);
    80
    81 // Output
    82 // Events enqueued
    83 // clicked on MyWidget!
    84 // resized MyWidget to {100, 200}!
    85}
    const ecs_entity_t EcsAny
    Any entity ("_").
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    bool ecs_is_stage(const ecs_world_t *world)
    Test whether a world object is a stage.
    void ecs_merge(ecs_world_t *stage)
    Merge a stage.
    ecs_world_t * ecs_get_stage(const ecs_world_t *world, int32_t stage_id)
    Get stage-specific world pointer.
    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_enqueue(ecs_world_t *world, ecs_event_desc_t *desc)
    Enqueue 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 <enqueue_entity_event.h>
    2#include <iostream>
    3
    4// This is the same example as entity_event, but instead of emit() the example
    5// uses the enqueue() method. Whereas emit() invokes observers synchronously,
    6// enqueue() adds the event to the command queue, which delays invoking
    7// observers until the next time the command queue is flushed.
    8
    9// An event without payload
    10struct Click { };
    11
    12// An event with payload
    13struct Resize {
    14 double width, height;
    15};
    16
    17int main(int, char *[]) {
    18 flecs::world ecs;
    19
    20 // Create a widget entity
    21 flecs::entity widget = ecs.entity("MyWidget");
    22
    23 // Observe the Click event on the widget entity.
    24 widget.observe<Click>([]() {
    25 std::cout << "clicked!\n";
    26 });
    27
    28 // Observers can have an entity argument that holds the event source.
    29 // This allows the same function to be reused for different entities.
    30 widget.observe<Click>([](flecs::entity src) {
    31 std::cout << "clicked on " << src.path() << "!\n";
    32 });
    33
    34 // Observe the Resize event. Events with payload are passed as arguments
    35 // to the observer callback.
    36 widget.observe([](Resize& p) {
    37 std::cout << "resized to {"
    38 << p.width << ", " << p.height << "}!\n";
    39 });
    40
    41 flecs::world stage_1 = ecs.get_stage(0);
    42
    43 // Emit the Click event
    44 widget.mut(stage_1).enqueue<Click>();
    45
    46 // Emit the Resize event
    47 widget.mut(stage_1).enqueue<Resize>({100, 200});
    48
    49 std::cout << "Events enqueued!\n";
    50
    51 // Flushes the queue, and invokes the observer
    52 stage_1.merge();
    53
    54 // Output
    55 // Events enqueued!
    56 // clicked!
    57 // clicked on ::MyWidget!
    58 // resized to {100, 200}!
    59}
    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 enqueue(flecs::entity_t evt) const
    Enqueue an event for an entity.
    flecs::entity mut(const flecs::world &stage) const
    Return a mutable entity handle for the current stage.
    Definition impl.hpp:72
    Entity.
    Definition entity.hpp:30
    The world.
    Definition world.hpp:129
    void merge() const
    Merge world or stage.
    Definition world.hpp:335
    flecs::world get_stage(int32_t stage_id) const
    Get stage-specific world pointer.
    Definition world.hpp:353