Skip to content
Flecs v4.1
systems/sync_point_delete

  • C

    View on GitHub

    1#include <sync_point_delete.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double x, y;
    6} Position, Velocity;
    7
    8void Move(ecs_iter_t *it) {
    9 Position *p = ecs_field(it, Position, 0);
    10 const Velocity *v = ecs_field(it, const Velocity, 1);
    11
    12 for (int i = 0; i < it->count; i ++) {
    13 p[i].x += v[i].x;
    14 p[i].y += v[i].y;
    15 }
    16}
    17
    18void DeleteEntity(ecs_iter_t *it) {
    19 const Position *p = ecs_field(it, Position, 0);
    20
    21 for (int i = 0; i < it->count; i ++) {
    22 if (p[i].x >= 3) {
    23 ecs_entity_t e = it->entities[i];
    24 printf("Delete entity %s\n", ecs_get_name(it->stage, e));
    25 ecs_delete(it->stage, e);
    26 }
    27 }
    28}
    29
    30void PrintPosition(ecs_iter_t *it) {
    31 const Position *p = ecs_field(it, const Position, 0);
    32
    33 for (int i = 0; i < it->count; i ++) {
    34 printf("%s: {%f, %f}\n", ecs_get_name(it->stage, it->entities[i]),
    35 p[i].x, p[i].y);
    36 }
    37}
    38
    39int main(int argc, char *argv[]) {
    40 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    41
    42 // This example shows how to annotate systems that delete entities, in a way
    43 // that allows the scheduler to correctly insert sync points. See the
    44 // sync_point example for more details on sync points.
    45 //
    46 // While annotating a system for a delete operation follows the same
    47 // design as other operations, one key difference is that a system often
    48 // does not know which components a to be deleted entity has. This makes it
    49 // impossible to annotate the system in advance for specific components.
    50 //
    51 // To ensure the scheduler is still able to insert the correct sync points,
    52 // a system can use a wildcard to indicate that any component could be
    53 // modified by the system, which forces the scheduler to insert a sync.
    54
    55 ECS_COMPONENT(ecs, Position);
    56 ECS_COMPONENT(ecs, Velocity);
    57
    58 // Basic move system.
    59 ECS_SYSTEM(ecs, Move, EcsOnUpdate, Position, [in] Velocity);
    60
    61 // Delete entities when p.x >= 3. Add wildcard annotation to indicate any
    62 // component could be written by the system. Position itself is added as
    63 // const, since inside the system we're only reading it.
    64 //
    65 // Note how the system uses the same annotations as the sync_point example.
    66 ECS_SYSTEM(ecs, DeleteEntity, EcsOnUpdate, [in] Position, [out] *());
    67
    68 // Print resulting Position
    69 ECS_SYSTEM(ecs, PrintPosition, EcsPostUpdate, [in] Position);
    70
    71 // Create a few test entities for a Position, Velocity query
    72 ecs_entity_t e1 = ecs_entity(ecs, { .name = "e1" });
    73 ecs_set(ecs, e1, Position, {0, 0});
    74 ecs_set(ecs, e1, Velocity, {1, 2});
    75
    76 ecs_entity_t e2 = ecs_entity(ecs, { .name = "e2" });
    77 ecs_set(ecs, e2, Position, {1, 2});
    78 ecs_set(ecs, e2, Velocity, {1, 2});
    79
    80 // Run systems. Debug logging enables us to see the generated schedule
    82 while (ecs_progress(ecs, 0.0f)) {
    83 if (!ecs_count(ecs, Position)) {
    84 break; // No more entities left with Position
    85 }
    86 }
    87 ecs_log_set_level(-1); // Restore so we don't get world cleanup logs
    88
    89 // Output:
    90 // info: pipeline rebuild:
    91 // info: | schedule: threading: 0, staging: 1:
    92 // info: | | system Move
    93 // info: | | system DeleteEntity
    94 // info: | | merge
    95 // info: | schedule: threading: 0, staging: 1:
    96 // info: | | system PrintPosition
    97 // info: | | merge
    98 // e1: {1, 2}
    99 // e2: {2, 4}
    100 // Delete entity e2
    101 // e1: {2, 4}
    102 // Delete entity e1
    103 // info: system Move deactivated
    104 // info: system DeleteEntity deactivated
    105 // info: system PrintPosition deactivated
    106 //
    107 // Removing the wildcard annotation from the DeleteEntity system will
    108 // remove the first sync point.
    109 //
    110 // Note how after both entities are deleted, all three systems are
    111 // deactivated. This happens when there are no matching entities for a
    112 // system. A deactivated system is not ran by the scheduler, which reduces
    113 // overhead.
    114 //
    115 // To create the same system with ecs_system_init, do:
    116 // ecs_system_init(ecs, &(ecs_system_desc_t){
    117 // .query.terms = {
    118 // {
    119 // .id = ecs_id(Position),
    120 // .inout = EcsIn
    121 // },
    122 // {
    123 // .id = EcsWildcard,
    124 // .inout = EcsOut,
    125 // .src.id = EcsIsEntity
    126 // }
    127 // },
    128 // .entity = {
    129 // .name = "DeleteEntity"
    130 // },
    131 // .phase = EcsOnUpdate,
    132 // .callback = DeleteEntity
    133 // });
    134
    135 return ecs_fini(ecs);
    136}
    const ecs_entity_t EcsPostUpdate
    PostUpdate pipeline phase.
    const ecs_entity_t EcsOnUpdate
    OnUpdate pipeline phase.
    FLECS_API int ecs_log_set_level(int level)
    Enable or disable log.
    FLECS_API bool ecs_progress(ecs_world_t *world, ecs_ftime_t delta_time)
    Progress a world.
    #define ECS_SYSTEM(world, id, phase,...)
    Declare and define a system.
    Definition system.h:263
    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
    void ecs_delete(ecs_world_t *world, ecs_entity_t entity)
    Delete an entity.
    #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_count(world, type)
    Count entities with a component.
    Definition flecs_c.h:709
    #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 <sync_point_delete.h>
    2#include <iostream>
    3
    4struct Position {
    5 double x, y;
    6};
    7
    8struct Velocity {
    9 double x, y;
    10};
    11
    12int main(int, char *[]) {
    13 flecs::world ecs;
    14
    15 // This example shows how to annotate systems that delete entities, in a way
    16 // that allows the scheduler to correctly insert sync points. See the
    17 // sync_point example for more details on sync points.
    18 //
    19 // While annotating a system for a delete operation follows the same
    20 // design as other operations, one key difference is that a system often
    21 // does not know which components a to be deleted entity has. This makes it
    22 // impossible to annotate the system in advance for specific components.
    23 //
    24 // To ensure the scheduler is still able to insert the correct sync points,
    25 // a system can use a wildcard to indicate that any component could be
    26 // modified by the system, which forces the scheduler to insert a sync.
    27
    28 // Basic move system.
    29 ecs.system<Position, const Velocity>("Move")
    30 .each([](Position& p, const Velocity& v) {
    31 p.x += v.x;
    32 p.y += v.y;
    33 });
    34
    35 // Delete entities when p.x >= 3. Add wildcard annotation to indicate any
    36 // component could be written by the system. Position itself is added as
    37 // const, since inside the system we're only reading it.
    38 ecs.system<const Position>("DeleteEntity")
    39 .write(flecs::Wildcard)
    40 .each([](flecs::entity e, const Position& p) {
    41 if (p.x >= 3) {
    42 std::cout << "Delete entity " << e.name() << "\n";
    43 e.destruct();
    44 }
    45 });
    46
    47 // Print resulting Position. Note that this system will never print entities
    48 // that have been deleted by the previous system.
    49 ecs.system<const Position>("PrintPosition")
    50 .each([](flecs::entity e, const Position& p) {
    51 std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n";
    52 });
    53
    54 // Create a few test entities for a Position, Velocity query
    55 ecs.entity("e1")
    56 .set<Position>({0, 0})
    57 .set<Velocity>({1, 2});
    58
    59 ecs.entity("e2")
    60 .set<Position>({1, 2})
    61 .set<Velocity>({1, 2});
    62
    63 // Run systems. Debug logging enables us to see the generated schedule
    65 while (ecs.progress()) {
    66 if (!ecs.count<Position>()) {
    67 break; // No more entities left with Position
    68 }
    69 }
    70 flecs::log::set_level(-1); // Restore so we don't get world cleanup logs
    71
    72 // Output:
    73 // info: pipeline rebuild:
    74 // info: | schedule: threading: 0, staging: 1:
    75 // info: | | system Move
    76 // info: | | system DeleteEntity
    77 // info: | | merge
    78 // info: | schedule: threading: 0, staging: 1:
    79 // info: | | system PrintPosition
    80 // info: | | merge
    81 // e1: {1, 2}
    82 // e2: {2, 4}
    83 // Delete entity e2
    84 // e1: {2, 4}
    85 // Delete entity e1
    86 // info: system Move deactivated
    87 // info: system DeleteEntity deactivated
    88 // info: system PrintPosition deactivated
    89 //
    90 // Removing the wildcard annotation from the DeleteEntity system will
    91 // remove the first sync point.
    92 //
    93 // Note how after both entities are deleted, all three systems are
    94 // deactivated. This happens when there are no matching entities for a
    95 // system. A deactivated system is not ran by the scheduler, which reduces
    96 // overhead.
    97}
    flecs::system system(flecs::entity e) const
    Upcast an entity to a system.
    Definition impl.hpp:181
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    void set_level(int level)
    Set the log level.
    Definition log.hpp:57
    bool progress(ecs_ftime_t delta_time=0.0) const
    Progress the world one tick.
    Definition impl.hpp:47
    flecs::string_view name() const
    Return the entity name.
    Entity.
    Definition entity.hpp:30
    void destruct() const
    Delete an entity.
    Definition entity.hpp:208
    system & each(Func &&func)
    Replace the system's each callback.
    Definition impl.hpp:119
    The world.
    Definition world.hpp:129
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.