DaVikingCode/Citrus-Engine

Question about object killing

Closed this issue · 5 comments

@alamboley ,

I have a little doubt with the way objects are killed, collected in MediatorState and then destroyed :
https://github.com/DaVikingCode/Citrus-Engine/blob/master/src/citrus/core/MediatorState.as#L94

With removeImmediate which can be helpful, objects can now potentially destroy immediately other objects (good for objects that are "linked")
So this means objects in the garbage could actually already be gone once we get to them... and a risk of reference to null could arise.

So we have this :

n = garbage.length;
var garbageObject:CitrusObject;
for (i = 0; i < n; ++i) {
garbageObject = garbage[i];
_objects.splice(_objects.indexOf(garbageObject), 1);
...
}

Can we not switch to something like :

while(garbage.length > 0) {
garbageObject = garbage[0];
_objects.splice(0, 1);
...
}

which I think is much safer - but I'm not entirely sure so I ask.

Well my commit seems to be working ok - what I'm not sure of is in what order should things be destroyed : the art first and then the object seems to be intuitively right (because the object's destroy could be trying to access art stuff that may disappear if we destroy the art first), but so far its been the other way around (as you can see in the MediatorState's destroy) and there was no issues with it.

I agree it should be the object first and then the art sine we're sure that the art never try to access its object in its destroy method.

Hello,

I got issue when I destroy many box2d objects.

Here my case:

I add 2 Crates (extends Box2DPhysicsObject) on the curent state.

When I destroy one Crate this create is killed (kill : true) and 20 Particles (extends Box2DPhysicsObject) with a for loop are created and the Crate is destroyed correctly.

After a delay all particles are killed (kill = true). But some particles are not destroyed.

After that I can't destroy the second Crate. (kill = true).


If none of Particles are destroyed all Crates can be destroyed correclty.

I will try to create a fresh project with only Crates and particles.

Hi @BenoitFreslon , I believe this topic could be related : http://forum.starling-framework.org/topic/kill-true-causes-other-enemies-to-stop

Have you got the latest version of CE because the latest commits I've made seem to correct that issue according to people experiencing problems with objects not being destroyed correctly.

If you find that even with the latest version you get the error, can you post a simple state so I can try it because I haven't been able to reproduce the issue myself.

Indeed I updated CE and now everything is OK.
Thx.