Add ecs::container::detach_finally/destroy_finally helpers

This commit is contained in:
Nikita Lisitsa 2025-10-10 16:01:17 +03:00
parent 4a4f680d0a
commit 424bf5f533
2 changed files with 23 additions and 0 deletions

View file

@ -79,6 +79,9 @@ namespace psemek::ecs
*/ */
void destroy(handle entity); void destroy(handle entity);
/** Same as destroy(), but via finally() */
void destroy_finally(handle entity);
/** Get an accessor for an entity, which provides access to the entity's components. /** Get an accessor for an entity, which provides access to the entity's components.
* It is designed to be a single-use object; it cannot be stored as a reference to * It is designed to be a single-use object; it cannot be stored as a reference to
* the entity. Use handle for that instead. * the entity. Use handle for that instead.
@ -169,6 +172,10 @@ namespace psemek::ecs
template <typename ... Components> template <typename ... Components>
void detach(handle entity); void detach(handle entity);
/** Same as detach(), but via finally() */
template <typename ... Components>
void detach_finally(handle entity);
/** Create a query cache that can be used to speed up `apply()` calls. /** Create a query cache that can be used to speed up `apply()` calls.
* *
* The constness of the component types is ignored. * The constness of the component types is ignored.
@ -611,6 +618,15 @@ namespace psemek::ecs
--method_recursion_depth_; --method_recursion_depth_;
} }
template <typename ... Components>
void container::detach_finally(handle entity)
{
finally([entity](container & world){
if (world.alive(entity))
world.detach<Components...>(entity);
});
}
template <typename ... Components> template <typename ... Components>
query_cache container::cache() query_cache container::cache()
{ {

View file

@ -36,6 +36,13 @@ namespace psemek::ecs
--method_recursion_depth_; --method_recursion_depth_;
} }
void container::destroy_finally(handle entity)
{
finally([entity](container & world){
if (world.alive(entity))
world.destroy(entity);
});
} }
accessor container::get(handle entity) accessor container::get(handle entity)