Instantiate packed Scene?

Godot Version

4.3

Question

I have been following a tutorial on destructible items in Godot and have run into a problem. The key point in the tutorial is that we create a scene in Godot with the thingy we want to destroy then swap it out for the item being destroyed.

So, I have this:

  • The explosion scene. I have the scene showing the item being destroyed, have run it many times, all good, blows apart the object nicely.
  • I have a scene of the item fully intact.

Now, in the main scene I have put the fully intact item in the scene, then, when the user hits the “ui_accept” button, be it enter/spacebar, it is supposed to instantiate the packed scene (explosion scene) and get blown to smithereens. Except it doesn’t happen. The instantiation process takes place all right, the object to be destroyed appears where I want it to, no problems there, tested it may times, but the:

_ready() function on the instantiated scene does not play at all and I cannot figure it out why.

The code for the Unexploded scene is:

extends Node3D

#will put this in later
@export var broken_model: PackedScene;


#handle input
#user hits the spacebar/enter key
func _unhandled_input(_event: InputEvent) -> void:
	#if enter hit
	if(Input.is_action_pressed("ui_accept")):
		print("Enter hit: ");
		
		#instantiate the broken model as bm_inst
		var bm_inst:Node3D = broken_model.instantiate();
		
		get_parent().add_child(bm_inst);
		
		##to move bm_inst to the right;
		#var my_vector=self.position;
		#my_vector.x += 2
		
		bm_inst.position = position;  #my_vector;
		bm_inst.rotation = rotation;
		
		self.queue_free();	
	
	#end func

The code for the explosion scene is:

extends Node3D

#I think the functions we are going to write are to impart some force to the 
#pieces so they will move when the destruction happens - I hope
#how to I impart this force - don't know yet

@export_category("Expode")
@export var intensity:float = 4.0;
var random_num = RandomNumberGenerator.new()

	
#this version of ready will be the one I post on the forum
func _ready() -> void:
	#on we are going to cycle through all the pieces
	#this should be put into an array worry about that later
	for pieces: RigidBody3D in self.get_children():
		#check it is cycling through
		print("Pieces: " +str(pieces));
		#yep cycling through
		
		#calculate direction from pumpkin center to piece
		var direction:Vector3 = (pieces.global_position - self.global_position).normalized()
		#apply impulse at piece's local center
		pieces.apply_impulse(direction * intensity, Vector3.ZERO)
		
	#wait 10 second	
	await get_tree().create_timer(10).timeout;
	#then destroy pieces
	queue_free();
	#end func

So, what happens is, “enter hit”, the object changes and that’s it. If anyone can help here is a big thankyou in advance!

Cheers.

Can you try changing your _ready() function to explode() and then call it explicitly after instantiating and adding to the scene?
Also try adding some print statements to debug what’s actually happening.
My theory is that _ready() might be too early for that piece of code to run.

1 Like

I never done anything like that before so I tried changing _ready() to explode() and changing the code to

broken_model.explode();

And it said:

  • Invalid call. Nonexistent function ‘explode’ in base ‘PackedScene’

_ready is not being called at all. That’s all I can say everything else seems to be going as expected.

You need to call it on the instantiated instance bm_inst.explode(), not the Packed Scene

Believe me I tried that - and that did’t work at all.

All I got was:

Invalid access to property or key ‘explode’ on a base object of type ‘Node3D’.

At least I got closer the other way :grinning_face_with_smiling_eyes:

Can you share your code again then?
What do you mean it didn’t work at all? Any error? What do your print statements show?

Oh dear, I just figured out what the problem was!

You are NOT going to believe this. Instead of putting the TSCN file in the export field I actually put the “gltf” file in there. I am SOOO incredibly embarrassed.

I am so sorry I wasted your time, I’ll go back and hide under my rock!

I am so incredibly sorry about that!

1 Like

No worries, happens to the best of us :slight_smile: I’m glad it was an easy fix
Keep going strong!