0 votes

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?

asked May 16, 2019 in Engine by KijeviGombooc (137 points)

1 Answer

+2 votes
Best answer

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)
answered May 16, 2019 by kidscancode (17,028 points)
selected Jun 26, 2019 by KijeviGombooc
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.