Skip to content
Flecs v4.1
systems/mutate_entity

  • C

    View on GitHub

    1#include <mutate_entity.h>
    2#include <stdio.h>
    3
    4typedef struct {
    5 double value;
    6} Timeout;
    7
    8// System that deletes an entity after a timeout expires
    9void Expire(ecs_iter_t *it) {
    10 Timeout *t = ecs_field(it, Timeout, 0);
    11
    12 for (int i = 0; i < it->count; i ++) {
    13 t[i].value -= (double)it->delta_time;
    14 if (t[i].value <= 0) {
    15 // When deleting the entity, use the world provided by the iterator.
    16
    17 // To make sure that the storage doesn't change while a system is
    18 // iterating entities, and multiple threads can safely access the
    19 // data, mutations (like a delete) are added to a command queue and
    20 // executed when it's safe to do so.
    21
    22 ecs_delete(it->stage, it->entities[i]);
    23 printf("Expire: %s deleted!\n", ecs_get_name(
    24 it->stage, it->entities[i]));
    25 }
    26 }
    27}
    28
    29// System that prints remaining expiry time
    30void PrintExpire(ecs_iter_t *it) {
    31 Timeout *t = ecs_field(it, Timeout, 0);
    32
    33 for (int i = 0; i < it->count; i ++) {
    34 printf("PrintExpire: %s has %.2f seconds left\n", ecs_get_name(
    35 it->stage, it->entities[i]), t[i].value);
    36 }
    37}
    38
    39// Observer that triggers when the component is actually removed
    40void Expired(ecs_iter_t *it) {
    41 for (int i = 0; i < it->count; i ++) {
    42 printf("Expired: %s actually deleted\n", ecs_get_name(
    43 it->stage, it->entities[i]));
    44 }
    45}
    46
    47int main(int argc, char *argv[]) {
    48 ecs_world_t *world = ecs_init_w_args(argc, argv);
    49
    50 ECS_COMPONENT(world, Timeout);
    51 ECS_SYSTEM(world, Expire, EcsOnUpdate, Timeout);
    52 ECS_SYSTEM(world, PrintExpire, EcsOnUpdate, [in] Timeout);
    53 ECS_OBSERVER(world, Expired, EcsOnRemove, Timeout);
    54
    55 // Create entity that expires after 3 seconds
    56 ecs_entity_t e = ecs_set_name(world, 0, "MyEntity");
    57 ecs_set(world, e, Timeout, { 3.0 });
    58
    59 ecs_set_target_fps(world, 1);
    60
    61 while ( ecs_progress(world, 0)) {
    62 // If entity is no longer alive, exit
    63 if (!ecs_is_alive(world, e)) {
    64 break;
    65 }
    66
    67 printf("Tick...\n");
    68 }
    69
    70 // Output:
    71 // PrintExpire: MyEntity has 2.00 seconds left
    72 // Tick...
    73 // PrintExpire: MyEntity has 0.98 seconds left
    74 // Tick...
    75 // Expire: MyEntity deleted!
    76 // PrintExpire: MyEntity has -0.02 seconds left
    77 // Expired: MyEntity actually deleted
    78
    79 return ecs_fini(world);
    80}
    const ecs_entity_t EcsOnRemove
    Event that triggers when an ID is removed from an entity.
    const ecs_entity_t EcsOnUpdate
    OnUpdate pipeline phase.
    FLECS_API void ecs_set_target_fps(ecs_world_t *world, ecs_ftime_t fps)
    Set target frames per second (FPS) for an application.
    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_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
    bool ecs_is_alive(const ecs_world_t *world, ecs_entity_t e)
    Test whether an entity is alive.
    ecs_entity_t ecs_set_name(ecs_world_t *world, ecs_entity_t entity, const char *name)
    Set the name of an entity.
    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
    float delta_time
    Time elapsed since last frame.
    Definition flecs.h:1228
    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 <mutate_entity.h>
    2#include <iostream>
    3#include <iomanip>
    4
    5struct Timeout {
    6 double value;
    7};
    8
    9int main(int, char *[]) {
    10 flecs::world ecs;
    11
    12 // System that deletes an entity after a timeout expires
    13 ecs.system<Timeout>()
    14 .each([](flecs::iter& it, size_t index, Timeout& t) {
    15 t.value -= static_cast<double>(it.delta_time());
    16 if (t.value <= 0) {
    17 // Delete the entity
    18
    19 // To make sure that the storage doesn't change while a system
    20 // is iterating entities, and multiple threads can safely access
    21 // the data, mutations (like a delete) are added to a command
    22 // queue and executed when it's safe to do so.
    23
    24 // When the entity to be mutated is not the same as the entity
    25 // provided by the system, an additional mut() call is required.
    26 // See the mutate_entity_handle example.
    27 flecs::entity e = it.entity(index);
    28 e.destruct();
    29 std::cout << "Expire: " << e.name() << "deleted!\n";
    30 }
    31 });
    32
    33 // System that prints remaining expiry time
    34 ecs.system<const Timeout>()
    35 .each([](flecs::entity e, const Timeout& t) {
    36 std::cout << "PrintExpire: " << e.name() << " has " << std::fixed
    37 << std::setprecision(2) << t.value << " seconds left\n";
    38 });
    39
    40 // Observer that triggers when entity is actually deleted
    41 ecs.observer<Timeout>()
    42 .event(flecs::OnRemove)
    43 .each([](flecs::entity e, Timeout&) {
    44 std::cout << "Expired: " << e.name() << " actually deleted\n";
    45 });
    46
    47
    48 flecs::entity e = ecs.entity("MyEntity")
    49 .set<Timeout>({3.0});
    50
    51 ecs.set_target_fps(1);
    52
    53 while (ecs.progress()) {
    54 // If entity is no longer alive, exit
    55 if (!e.is_alive()) {
    56 break;
    57 }
    58
    59 std::cout << "Tick...\n";
    60 }
    61
    62 // Output
    63 // PrintExpire: MyEntity has 2.00 seconds left
    64 // Tick...
    65 // PrintExpire: MyEntity has 0.99 seconds left
    66 // Tick...
    67 // Expire: MyEntitydeleted!
    68 // PrintExpire: MyEntity has -0.02 seconds left
    69 // Expired: MyEntity actually deleted
    70}
    void set_target_fps(ecs_ftime_t target_fps) const
    Set the target FPS.
    Definition impl.hpp:37
    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
    flecs::observer observer(flecs::entity e) const
    Observer world mixin.
    Definition impl.hpp:85
    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.
    bool is_alive() const
    Check if entity is alive.
    Entity.
    Definition entity.hpp:30
    void destruct() const
    Delete an entity.
    Definition entity.hpp:208
    Class for iterating over query results.
    Definition iter.hpp:68
    ecs_ftime_t delta_time() const
    Get the time elapsed since the last frame.
    Definition iter.hpp:132
    flecs::entity entity(size_t row) const
    Obtain a mutable handle to the entity being iterated over.
    Definition iter.hpp:36
    The world.
    Definition world.hpp:129
    void each(Func &&func) const
    Iterate over all entities with components in the argument list of the function.
    flecs::event_builder event(flecs::entity_t evt) const
    Create a new event.