Skip to content
Flecs v4.1
entities/on_validate

  • C

    View on GitHub

    1#include <on_validate.h>
    2#include <stdio.h>
    3
    4// The on_validate hook is invoked when a component is set, before the on_set
    5// hook and OnSet observers are invoked. When the hook returns false, the
    6// on_set hook and OnSet observers are not invoked for the entity. This can be
    7// used to guard application logic from invalid component values.
    8
    9typedef struct {
    10 double x, y;
    11} Position;
    12
    13// Returns false when the position is outside of the world bounds.
    14bool validate_position(ecs_world_t *world, ecs_entity_t e, void *ptr) {
    15 (void)world; (void)e;
    16 Position *p = ptr;
    17 return p->x >= 0 && p->x <= 100 && p->y >= 0 && p->y <= 100;
    18}
    19
    20void on_set_position(ecs_iter_t *it) {
    21 Position *p = ecs_field(it, Position, 0);
    22
    23 for (int i = 0; i < it->count; i ++) {
    24 printf("%s set to {%.0f, %.0f}\n",
    25 ecs_get_name(it->stage, it->entities[i]), p[i].x, p[i].y);
    26 }
    27}
    28
    29int main(int argc, char *argv[]) {
    30 ecs_world_t *world = ecs_init_w_args(argc, argv);
    31
    32 ECS_COMPONENT(world, Position);
    33
    34 ecs_set_hooks(world, Position, {
    35 .on_validate = validate_position,
    36 .on_set = on_set_position
    37 });
    38
    39 ecs_entity_t e = ecs_entity(world, { .name = "Entity" });
    40
    41 // Position is inside world bounds, on_set is invoked
    42 ecs_set(world, e, Position, {50, 50});
    43
    44 // Position is outside world bounds, on_set is not invoked
    45 ecs_set(world, e, Position, {150, 50});
    46
    47 return ecs_fini(world);
    48
    49 // Output:
    50 // Entity set to {50, 50}
    51}
    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_set_hooks(world, T,...)
    Set component hooks.
    Definition flecs_c.h:623
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #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
    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 <on_validate.h>
    2#include <iostream>
    3
    4// The on_validate hook is invoked when a component is set, before the on_set
    5// hook and OnSet observers are invoked. When the hook returns false, the
    6// on_set hook and OnSet observers are not invoked for the entity. This can be
    7// used to guard application logic from invalid component values.
    8
    9struct Position {
    10 double x, y;
    11};
    12
    13int main(int, char *[]) {
    14 flecs::world ecs;
    15
    16 ecs.component<Position>()
    17 // Returns false when the position is outside of the world bounds.
    18 .on_validate([](flecs::entity, Position& p) {
    19 return p.x >= 0 && p.x <= 100 && p.y >= 0 && p.y <= 100;
    20 })
    21 .on_set([](flecs::entity e, Position& p) {
    22 std::cout << e.name() << " set to {"
    23 << p.x << ", " << p.y << "}" << std::endl;
    24 });
    25
    26 flecs::entity e = ecs.entity("Entity");
    27
    28 // Position is inside world bounds, on_set is invoked
    29 e.set<Position>({50, 50});
    30
    31 // Position is outside world bounds, on_set is not invoked
    32 e.set<Position>({150, 50});
    33
    34 // Output:
    35 // Entity set to {50, 50}
    36}
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::string_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    The world.
    Definition world.hpp:129