In my game I had the following problem: the parent group of a vent was removed before all the emitted particles would die. Since vent:stop() only stops the emission of particles, the living particles were still active and belonged to the parent group, which no longer existed, and that caused an error. Here is a solution I came up with:
-
A recursive function to dispatch an event to all objects in a particular group:
local function dispatchToGroup(options) local event = options.event; local parent = options.parent; local function recursion(parent) --if object has children if(parent.numChildren ~= nil and parent.numChildren > 0) then for i = parent.numChildren, 1, -1 do if (parent[i] ~= nil) then recursion(parent[i]); --dispatching event to object’s children end end end parent:dispatchEvent(event) --dispatching event to current child end recursion(parent); --starting recursion end
-
Creating a new vent and subscribing each emited particle to our own custom event (a command to kill all particles)
local vent = CBE.newVent { --creating a new vent preset = “embers”, parentGroup = theParent, onCreation = function§ --subscribing each new particle to our own custom event p:addEventListener(“killEmbers”, function() p._kill(); --killing the particle using its internal method end); end }
-
Adding a vent method that removes all particles
function vent:removeAllParticles() dispatchToGroup({event = {name=“killEmbers”}, parent = theParent}); end
Now if we want to remove a vent and all its particles immediately, we just do this:
vent:stop(); vent:removeAllParticles(); vent = nil;
Please tell me what you think about this approach. Or maybe there is a much better way to do it, like a predefined method in CBEffects that I don’t know about