What ways are there to clone a node with its children?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By KijeviGombooc

I have an asteroid node, that has 2 children. The asteroid node has a script, in which i want to spawn two other asteroids when it when dies (descreasing a value on the child so the spawning will stop eventually).
My question is, how can i implement spawning a clone of the node? I did it with using load(“Asteroid.tscn”) but that loads it on every asteroid spawned, if i know well, which isnt efficient i think.
I was thinking maybe i should make a script globally and load the asteroid there, then use it in the asteroid.gd to spawn other asteroids.
Are there any other ways?

:bust_in_silhouette: Reply From: kidscancode

This should be done with Instancing.

Load your saved asteroid scene into a variable at the top of your main scene:

var Asteroid = preload("Asteroid.tscn")

This will be a PackedScene object. Whenever you want to make a new asteroid, call instance() on it:

func spawn_new_asteroid():
    var new_asteroid = Asteroid.instance()
    add_child(new_asteroid)