Skip to content
Flecs v4.1
observers/two_components

  • C

    View on GitHub

    1#include <two_components.h>
    2#include <stdio.h>
    3
    4// An observer can match multiple components/tags. Only entities that match the
    5// entire observer query will be forwarded to the callback. For example, an
    6// observer for Position,Velocity won't match an entity that only has Position.
    7
    8typedef struct {
    9 double x, y;
    10} Position, Velocity;
    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
    18 Position *p = ecs_field(it, Position, 0);
    19 Velocity *v = ecs_field(it, Velocity, 1);
    20
    21 for (int i = 0; i < it->count; i ++) {
    22 ecs_entity_t e = it->entities[i];
    23 printf("%s: %s: %s: p: {%f, %f} v: {%f, %f}\n",
    24 ecs_get_name(ecs, event),
    25 ecs_get_name(ecs, event_id),
    26 ecs_get_name(ecs, e),
    27 p[i].x, p[i].y,
    28 v[i].x, v[i].y);
    29 }
    30}
    31
    32int main(int argc, char *argv[]) {
    33 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    34
    35 ECS_COMPONENT(ecs, Position);
    36 ECS_COMPONENT(ecs, Velocity);
    37
    38 ECS_OBSERVER(ecs, Observer, EcsOnSet, Position, Velocity);
    39
    40 // Create entity
    41 ecs_entity_t e = ecs_entity(ecs, { .name = "e" });
    42
    43 // Set Position (emits EcsOnSet, does not yet match observer)
    44 ecs_set(ecs, e, Position, {10, 20});
    45
    46 // Set Velocity (emits EcsOnSet, matches observer)
    47 ecs_set(ecs, e, Velocity, {1, 2});
    48
    49 return ecs_fini(ecs);
    50
    51 // Output:
    52 // OnSet: Velocity: e: p: {10.000000, 20.000000} v: {1.000000, 2.000000}
    53}
    const ecs_entity_t EcsOnSet
    Event that triggers when a component is set for an entity.
    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, 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
    #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 <two_components.h>
    2#include <iostream>
    3
    4// An observer can match multiple components/tags. Only entities that match the
    5// entire observer query will be forwarded to the callback. For example, an
    6// observer for Position,Velocity won't match an entity that only has Position.
    7
    8struct Position {
    9 double x, y;
    10};
    11
    12struct Velocity {
    13 double x, y;
    14};
    15
    16int main(int, char *[]) {
    17 flecs::world ecs;
    18
    19 // Create observer for custom event
    20 ecs.observer<Position, Velocity>()
    21 .event(flecs::OnSet)
    22 .each([](flecs::iter& it, size_t i, Position& p, Velocity& v) {
    23 std::cout << " - " << it.event().name() << ": "
    24 << it.event_id().str() << ": "
    25 << it.entity(i).name() << ": "
    26 << "p: {" << p.x << ", " << p.y << "} "
    27 << "v: {" << v.x << ", " << v.y << "}\n";
    28 });
    29
    30 // Create entity, set Position (emits EcsOnSet, does not yet match observer)
    31 flecs::entity e = ecs.entity("e")
    32 .set<Position>({10, 20});
    33
    34 // Set Velocity (emits EcsOnSet, matches observer)
    35 e.set<Velocity>({1, 2});
    36
    37 // Output
    38 // - OnSet: Velocity: e: p: {10, 20} v: {1, 2}
    39}
    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.