Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: add entity destruction cleanup hooks in Registry

esengine6c220685cbf2
diff --git a/src/esengine/ecs/Registry.hpp b/src/esengine/ecs/Registry.hpp
index 28679c848..fdbae7991 100644
--- a/src/esengine/ecs/Registry.hpp
+++ b/src/esengine/ecs/Registry.hpp
@@ -158,6 +158,10 @@ public:
void destroy(Entity entity) {
if (!valid(entity)) return;
+ for (auto& callback : on_destroy_callbacks_) {
+ callback(entity);
+ }
+
u64 mask = component_masks_[entity];
while (mask != 0) {
u32 bit = static_cast<u32>(__builtin_ctzll(mask));
@@ -182,6 +186,26 @@ public:
ES_LOG_TRACE("Destroyed entity {}", entity);
}
+ using DestroyCallback = std::function<void(Entity)>;
+
+ u32 onDestroy(DestroyCallback callback) {
+ u32 id = next_callback_id_++;
+ on_destroy_callbacks_.emplace_back(std::move(callback));
+ on_destroy_callback_ids_.push_back(id);
+ return id;
+ }
+
+ void removeOnDestroy(u32 callbackId) {
+ for (usize i = 0; i < on_destroy_callback_ids_.size(); ++i) {
+ if (on_destroy_callback_ids_[i] == callbackId) {
+ auto pos = static_cast<std::ptrdiff_t>(i);
+ on_destroy_callbacks_.erase(on_destroy_callbacks_.begin() + pos);
+ on_destroy_callback_ids_.erase(on_destroy_callback_ids_.begin() + pos);
+ return;
+ }
+ }
+ }
+
/**
* @brief Checks if an entity is currently valid
* @param entity The entity to check
@@ -629,6 +653,11 @@ private:
/** @brief Schema component registry for script-defined components */
SchemaRegistry schemaRegistry_;
+
+ /** @brief Entity destruction callbacks */
+ std::vector<DestroyCallback> on_destroy_callbacks_;
+ std::vector<u32> on_destroy_callback_ids_;
+ u32 next_callback_id_ = 0;
};
} // namespace esengine::ecs
diff --git a/src/esengine/particle/ParticleSystem.cpp b/src/esengine/particle/ParticleSystem.cpp
index f9f2a86dd..6e56b184e 100644
--- a/src/esengine/particle/ParticleSystem.cpp
+++ b/src/esengine/particle/ParticleSystem.cpp
@@ -10,7 +10,11 @@ ParticleSystem::ParticleSystem()
}
void ParticleSystem::update(ecs::Registry& registry, f32 dt) {
- cleanupDeadEntities(registry);
+ if (destroy_callback_id_ == 0) {
+ destroy_callback_id_ = registry.onDestroy([this](Entity entity) {
+ states_.erase(entity);
+ });
+ }
auto view = registry.view<ecs::Transform, ecs::ParticleEmitter>();
for (auto entity : view) {
@@ -259,17 +263,6 @@ void ParticleSystem::updateParticles(const ecs::ParticleEmitter& emitter,
}
}
-void ParticleSystem::cleanupDeadEntities(ecs::Registry& registry) {
- dead_entities_.clear();
- for (const auto& [entity, _] : states_) {
- if (!registry.valid(entity)) {
- dead_entities_.push_back(entity);
- }
- }
- for (auto entity : dead_entities_) {
- states_.erase(entity);
- }
-}
f32 ParticleSystem::randomRange(f32 min, f32 max) {
if (min >= max) {
diff --git a/src/esengine/particle/ParticleSystem.hpp b/src/esengine/particle/ParticleSystem.hpp
index 445f87f73..442e5f639 100644
--- a/src/esengine/particle/ParticleSystem.hpp
+++ b/src/esengine/particle/ParticleSystem.hpp
@@ -49,8 +49,6 @@ private:
EmitterState& state, u32 count);
void updateParticles(const ecs::ParticleEmitter& emitter, EmitterState& state, f32 dt);
- void cleanupDeadEntities(ecs::Registry& registry);
-
f32 randomRange(f32 min, f32 max);
glm::vec2 randomDirection(f32 angleMin, f32 angleMax);
glm::vec2 randomShapeOffset(const ecs::ParticleEmitter& emitter);
@@ -58,7 +56,7 @@ private:
std::unordered_map<Entity, EmitterState> states_;
std::mt19937 rng_;
std::vector<Particle*> dead_particles_;
- std::vector<Entity> dead_entities_;
+ u32 destroy_callback_id_ = 0;
};
} // namespace esengine::particle
diff --git a/src/esengine/spine/SpineSystem.cpp b/src/esengine/spine/SpineSystem.cpp
index 1f6d4d508..d6394b4b1 100644
--- a/src/esengine/spine/SpineSystem.cpp
+++ b/src/esengine/spine/SpineSystem.cpp
@@ -43,14 +43,10 @@ void SpineSystem::update(ecs::Registry& registry, f32 deltaTime) {
}
}
- pending_remove_.clear();
- for (auto& [entity, instance] : instances_) {
- if (!registry.valid(entity) || !registry.has<ecs::SpineAnimation>(entity)) {
- pending_remove_.push_back(entity);
- }
- }
- for (Entity e : pending_remove_) {
- instances_.erase(e);
+ if (destroy_callback_id_ == 0) {
+ destroy_callback_id_ = registry.onDestroy([this](Entity entity) {
+ instances_.erase(entity);
+ });
}
}
diff --git a/src/esengine/spine/SpineSystem.hpp b/src/esengine/spine/SpineSystem.hpp
index d6e8ce63b..6dffa2226 100644
--- a/src/esengine/spine/SpineSystem.hpp
+++ b/src/esengine/spine/SpineSystem.hpp
@@ -176,7 +176,7 @@ private:
SpineResourceManager& resource_manager_;
std::unordered_map<Entity, SpineInstance> instances_;
- std::vector<Entity> pending_remove_;
+ u32 destroy_callback_id_ = 0;
};
} // namespace esengine::spine