How to add a scene with all it's children using `new()` method?

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

I know about PackedScene and that we get it using load()/preload()and it works. However, I don’t like to have to provide a path to the file of a scene because I may want to move it at some point. So I tried adding a class_name and wrote add_child( but only the top-level node appeared in the tree. No children.

Question is, how do I properly instance a scene without using a path to it?

:bust_in_silhouette: Reply From: rossunger

You can’t! the class is just a script file, and it doesn’t know anything about a whole scene.

You could use export variables if you’d prefer to set the scene via the inspector

export (PackedScene) var sceneToInstance

:bust_in_silhouette: Reply From: rossunger

Another option is to add the scene to your Autoloads… in project settings… when you want to duplicate it you call yourScene.duplicate( ) instead of instancing it

but then it will always be loaded and instanced…