DestroyInstance() vs OnDestroy()

0 favourites
  • 8 posts
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • 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

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I had discussed at this thread.

    Yes, it had worked fine previously, now is broken. I workaround by overwrite acts.Destroy.

  • 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".

  • 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.

  • 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.

    	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)
    	
    	}[/code:oi9u5h7j]
  • 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.

  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)