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.