+2 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?

in Engine by (147 points)

1 Answer

+3 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()
by (21,714 points)
selected by
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.