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.

fix(particle): play() before an emitter's first update isn't dropped

esengine1905f48d56e4
diff --git a/src/esengine/particle/ParticleSystem.cpp b/src/esengine/particle/ParticleSystem.cpp
index 635b6d3b4..b673647cf 100644
--- a/src/esengine/particle/ParticleSystem.cpp
+++ b/src/esengine/particle/ParticleSystem.cpp
@@ -16,6 +16,7 @@ void ParticleSystem::update(ecs::Registry& registry, f32 dt) {
if (!destroyConn_.isConnected()) {
destroyConn_ = registry.onDestroyScoped([this](Entity entity) {
states_.erase(entity);
+ pending_play_.erase(entity);
colorLuts_.erase(entity);
sizeLuts_.erase(entity);
});
@@ -44,6 +45,12 @@ void ParticleSystem::update(ecs::Registry& registry, f32 dt) {
if (emitter.playOnStart) {
it->second.playing = true;
}
+ // Apply a play() that arrived before this first update (playOnStart=false
+ // + an immediate trigger) instead of dropping it.
+ if (pending_play_.erase(entity) > 0) {
+ it->second.playing = true;
+ it->second.elapsed_time = 0.0f;
+ }
}
auto& state = it->second;
@@ -93,10 +100,15 @@ void ParticleSystem::play(Entity entity) {
if (it != states_.end()) {
it->second.playing = true;
it->second.elapsed_time = 0.0f;
+ } else {
+ // State is created lazily on the first update; remember the trigger so it
+ // isn't lost when play() runs before this emitter's first update.
+ pending_play_.insert(entity);
}
}
void ParticleSystem::stop(Entity entity) {
+ pending_play_.erase(entity);
auto it = states_.find(entity);
if (it != states_.end()) {
it->second.playing = false;
@@ -104,6 +116,7 @@ void ParticleSystem::stop(Entity entity) {
}
void ParticleSystem::reset(Entity entity) {
+ pending_play_.erase(entity);
auto it = states_.find(entity);
if (it != states_.end()) {
it->second.pool.clear();
diff --git a/src/esengine/particle/ParticleSystem.hpp b/src/esengine/particle/ParticleSystem.hpp
index e2b38aabf..b3a0af703 100644
--- a/src/esengine/particle/ParticleSystem.hpp
+++ b/src/esengine/particle/ParticleSystem.hpp
@@ -11,6 +11,7 @@
#include "ParticleEasing.hpp"
#include <unordered_map>
+#include <unordered_set>
#include <functional>
#include <random>
#include <array>
@@ -71,6 +72,9 @@ private:
glm::vec2 randomShapeOffset(const ecs::ParticleEmitter& emitter);
std::unordered_map<Entity, EmitterState> states_;
+ // A play() that arrives before the emitter's first update (its state isn't
+ // created yet) is remembered here and applied on state creation, not dropped.
+ std::unordered_set<Entity> pending_play_;
std::unordered_map<Entity, ColorLut> colorLuts_;
std::unordered_map<Entity, SizeLut> sizeLuts_;
std::mt19937 rng_;
diff --git a/tests/particle/test_particle.cpp b/tests/particle/test_particle.cpp
index c37392433..ad16545d7 100644
--- a/tests/particle/test_particle.cpp
+++ b/tests/particle/test_particle.cpp
@@ -142,6 +142,46 @@ TEST_CASE("system_emitter_spawns_particles") {
CHECK(system.totalAliveParticles() > 0u);
}
+TEST_CASE("system_play_before_first_update_is_not_dropped") {
+ // Emitter state is created lazily on the first update. A play() that fires
+ // before that (playOnStart=false + an immediate script trigger) must be
+ // remembered, not lost — otherwise the emitter stays off forever.
+ ecs::Registry registry;
+ ParticleSystem system;
+
+ Entity e = registry.create();
+ registry.emplace<ecs::Transform>(e);
+ auto& emitter = registry.emplace<ecs::ParticleEmitter>(e);
+ emitter.rate = 100.0f;
+ emitter.lifetimeMin = 1.0f;
+ emitter.lifetimeMax = 1.0f;
+ emitter.maxParticles = 1000;
+ emitter.enabled = true;
+ emitter.playOnStart = false; // won't auto-play
+
+ system.play(e); // before any update — state doesn't exist yet
+ system.update(registry, 0.1f);
+ CHECK(system.totalAliveParticles() > 0u);
+}
+
+TEST_CASE("system_no_play_stays_idle_when_playOnStart_false") {
+ ecs::Registry registry;
+ ParticleSystem system;
+
+ Entity e = registry.create();
+ registry.emplace<ecs::Transform>(e);
+ auto& emitter = registry.emplace<ecs::ParticleEmitter>(e);
+ emitter.rate = 100.0f;
+ emitter.lifetimeMin = 1.0f;
+ emitter.lifetimeMax = 1.0f;
+ emitter.maxParticles = 1000;
+ emitter.enabled = true;
+ emitter.playOnStart = false;
+
+ system.update(registry, 0.1f); // no play() → nothing emits
+ CHECK_EQ(system.totalAliveParticles(), 0u);
+}
+
TEST_CASE("system_particles_die_after_lifetime") {
ecs::Registry registry;
ParticleSystem system;