Skip to content
Flecs v4.1
systems/mutate_entity_handle

  • C++

    View on GitHub

    1#include <mutate_entity_handle.h>
    2#include <iostream>
    3#include <iomanip>
    4
    5// This example is the same as the mutate_entity example, but instead stores the
    6// handle of the to be deleted entity in a component.
    7
    8struct Timeout {
    9 flecs::entity to_delete;
    10 double value;
    11};
    12
    13struct Tag { };
    14
    15int main(int, char *[]) {
    16 flecs::world ecs;
    17
    18 ecs.system<Timeout>()
    19 .each([](flecs::iter& it, size_t, Timeout& t) {
    20 t.value -= static_cast<double>(it.delta_time());
    21 if (t.value <= 0) {
    22 // Delete the entity
    23
    24 // To make sure the delete operation is enqueued (see
    25 // mutate_entity example for more details) we need to provide it
    26 // with a mutable context (stage) using the mut() function. If
    27 // we don't provide a mutable context, the operation will be
    28 // attempted on the context stored in the flecs::entity object,
    29 // which would throw a readonly error.
    30
    31 // To catch these errors at compile time, replace the type of
    32 // to_delete with flecs::entity_view. This class does not have
    33 // any methods for mutating the entity, which forces the code to
    34 // first call mut().
    35
    36 // The current entity can also be used to provide context. This
    37 // is useful for functions that accept a flecs::entity:
    38 // t.to_delete.mut(it.entity(index)).destruct();
    39 //
    40 // A shortcut is to use the iterator directly:
    41 t.to_delete.mut(it).destruct();
    42 std::cout << "Expire: " << t.to_delete.name() << " deleted!\n";
    43 }
    44 });
    45
    46 ecs.system<const Timeout>()
    47 .each([](const Timeout& t) {
    48 std::cout << "PrintExpire: " << t.to_delete.name() << " has "
    49 << std::fixed << std::setprecision(2) << t.value
    50 << " seconds left\n";
    51 });
    52
    53 ecs.observer<Tag>()
    54 .event(flecs::OnRemove)
    55 .each([](flecs::entity e, Tag) {
    56 std::cout << "Expired: " << e.name() << " actually deleted\n";
    57 });
    58
    59 // Add Tag so we can get notified when entity is actually deleted
    60 flecs::entity to_delete = ecs.entity("ToDelete")
    61 .add<Tag>();
    62
    63 ecs.entity("MyEntity")
    64 .set<Timeout>({to_delete, 3.0});
    65
    66 ecs.set_target_fps(1);
    67
    68 while (ecs.progress()) {
    69 // If entity is no longer alive, exit
    70 if (!to_delete.is_alive()) {
    71 break;
    72 }
    73
    74 std::cout << "Tick...\n";
    75 }
    76
    77 // Output
    78 // PrintExpire: ToDelete has 2.00 seconds left
    79 // Tick...
    80 // PrintExpire: ToDelete has 0.98 seconds left
    81 // Tick...
    82 // Expire: ToDelete deleted!
    83 // PrintExpire: ToDelete has -0.03 seconds left
    84 // Expired: ToDelete actually deleted
    85}
    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
    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
    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.