DestroyInstance() vs OnDestroy()

For developers using the Construct 2 Javascript SDK

Post » Fri Mar 20, 2015 3:55 am

I'm trying to wrap my head around how to create a container style destructor.

onDestroy for the top level object should cause children objects to get destroyed by their onDestroy.

Calling DestroyInstance(inst) seems to do nothing, and not remove the objects i called it on, but inst.OnDestroy() gets rid of the objects as i wish. My fear is that using OnDestroy() this way is going to break things somehow, but i'm not sure how? It'd be nice if someone who knew what would happen could help out.

How should I go about this ? Another option i tried was calling DestroyInstance, then ClearDeathRowForSingleInstance(inst,inst.type), but i'm not completely sure this is a good idea either, what are the side effects if any?

I understand the issue is that destroyinstance doesn't work inside an ondestroy for whatever reason, but what is that reason?

Also i remember DestroyInstance(inst) previously working in this context
Last edited by QuaziGNRLnose on Fri Mar 20, 2015 4:21 am, edited 1 time in total.
B
67
S
11
G
8
Posts: 1,966
Reputation: 9,429

Post » Fri Mar 20, 2015 4:21 am

at this point from what i understand:

-OnDestroy is only called at the end of an event block or in special cases to get rid of an object for good.

-DestroyInstance() is not working the same as before the optimization, and is going to break a lot of 3rd party plugins... Calling it in OnDestroy() results in nothing actually happening
B
67
S
11
G
8
Posts: 1,966
Reputation: 9,429

Post » Fri Mar 20, 2015 6:18 am

I had discussed at this thread.
Yes, it had worked fine previously, now is broken. I workaround by overwrite acts.Destroy.
B
107
S
25
G
231
Posts: 4,362
Reputation: 131,414

Post » Fri Mar 20, 2015 6:23 am

Another solution might be -
change the engine, moving OnDestroy() call inside to the body of DestroyInstance() just after "on destroy" trigger. So it would be called like "condition: on destroy".
B
107
S
25
G
231
Posts: 4,362
Reputation: 131,414

Post » Fri Mar 20, 2015 3:28 pm

@rexrainbow

I think i'm going to try moving destroyinstance into itself and modify the function to recursively call itself before it does it's normal task.

I think this will work.
B
67
S
11
G
8
Posts: 1,966
Reputation: 9,429

Post » Fri Mar 20, 2015 4:35 pm

yup it seems to do the trick, though im not 100% sure if it introduces bugs in certain cases, but it shouldn't, since its calling destroy instance at the same time for all hierarchical objects.
Code: Select all
   var runproto = Object.getPrototypeOf(runtime)
   var oldfunc = runproto.DestroyInstance
   
   runproto.DestroyInstance = function(inst){
   
      if(inst.Q3Dobject){
      
         for ( var i = inst.obj.children.length-1; i > 0; i -- ) { //iterate backwards or messes up!
         
            if(inst.obj.children[ i ].userData.inst){
               this.DestroyInstance(inst.obj.children[ i ].userData.inst); //recursively destroy all Q3D type objects
            }

         };
      
      };
   
      oldfunc.call(this,inst)
   
   }
B
67
S
11
G
8
Posts: 1,966
Reputation: 9,429

Post » Sat Mar 21, 2015 12:24 am

Looks good!
It might be better to request @Ashley add another "OnDestroy" inside "runtime.DestroyInstance", so that 3 part plugin does not need to inject their own OnDestroy function again.
B
107
S
25
G
231
Posts: 4,362
Reputation: 131,414

Post » Sat Mar 21, 2015 4:30 am

@rexrainbow

from what i understand looking at the source, onDestroy is only called in cleanup on cleardeathrow, so it'd be smarter to do it the way i did and call destroyinstance in destroyinstance for specially flagged plugins. Construct doesn't do any hierarchical destruction, but all destruction is handled by destroyinstance anytime any destroying is needed. This supports recursive stuff too. I'm not sure where it can break. I do agree some official solution or word on this would be nice though
B
67
S
11
G
8
Posts: 1,966
Reputation: 9,429


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests