Is it possible to destroy other instances under "onDestroy"?

For developers using the Construct 2 Javascript SDK

Post » Mon Feb 23, 2015 5:11 pm

@Ashley

Destroying under "onDestroy" seems to do something special. (preview.js, line 2606)
Code: Select all
if (this.isInClearDeathRow)
    obj_set.values_cache.push(inst);

(this.isInClearDeathRow will be true)
But these pending destroyed would not be processed (not be destroyed really) in r197.

Is it possible to destroy other instances under "onDestroy"?
B
108
S
26
G
250
Posts: 4,404
Reputation: 141,125

Post » Mon Feb 23, 2015 6:30 pm

I think it changed with the recent destroy objects optimisation, so it may not be cleaned up in the same call (it's difficult to support that without infinite-recursion edge cases), but it is cleaned up at least once a tick, so they should still be destroyed shortly after.
Scirra Founder
B
386
S
229
G
87
Posts: 24,207
Reputation: 191,572

Post » Tue Feb 24, 2015 5:15 am

@Ashley

My goal is: ObjectB will be destroyed when objectA is destroyed, under "onDestroy"(preview.js, line 2805) in ObjectA.
destroy ObjectA -> "onDestroy" in ObjectA -> destroy ObjectB

In my study, function "ClearDeathRowForType"(preview.js line 2661) should be called twice for these two types of destroyed objects, in a tick or during many ticks.
But it only had been called once for ObjectA in this case.

"cnds.OnDestroyed" (preview.js line 2610) of ObjectB had been fired in this case. It might be a solution that hang "onDestroy" function call after "cnds.OnDestroyed" trigger.
B
108
S
26
G
250
Posts: 4,404
Reputation: 141,125

Post » Tue Mar 10, 2015 3:27 am

Bump.

@Ashley

Would you please check if "ClearDeathRowForType" ran for each object destroyed.
I found destroy other instances under "onDestroy" would not fire "ClearDeathRowForType".
Since this behavior did not been used in official plugins, so it is not a obviously bug.
B
108
S
26
G
250
Posts: 4,404
Reputation: 141,125

Post » Tue Mar 10, 2015 12:15 pm

As before, destroying objects in "onDestroy" is difficult to support, since it requires adapting to a recursive/iterative process that isn't guaranteed to terminate. I'd suggest instead adding objects to a "to be destroyed" list in "onDestroy", then after the call finishes destroy the objects in the list.
Scirra Founder
B
386
S
229
G
87
Posts: 24,207
Reputation: 191,572

Post » Tue Mar 10, 2015 3:39 pm

@Ashley

Thanks, I had a workaround by overwrite acts.Destroy -

Code: Select all
pluginProto.onCreate = function ()
{
   pluginProto.acts.Destroy = function ()
   {
      this.runtime.DestroyInstance(this);
      // destroy other objects
   };       
};
B
108
S
26
G
250
Posts: 4,404
Reputation: 141,125


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests