Adding a child to a node from a subscene, instancing nodes

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

Hello, I am working on a level editor in my game and I am stuck on instancing nodes.

This is my node setup

I have created a user interface that I reuse and saved it as a scene. I added it to my level editor scene, but the buttons I use to instance nodes in my level editor are in the backgroundGUI scene

func LoadInstance(path):
var scene = load(path);
var instance = scene.instance();

func _on_Ice_pressed():

func _on_Sand_pressed():

This is the player creates objects.
It is created in the control scene, but I want it under LevelEditor, where PseudoPlayer is. I have searched online and haven’t found a way to do it.
I would be willing to rework this system because it’s kinda clunky and I just want it to work so I can continue with my project. Thanks :wink:

Maybe you could just add the UI as a child of the main scene, then make it invisible using .visibility until you need it again. This way, you can add signals to the LevelEditor, which instances as it’s child

PhantomPixel | 2021-11-04 02:39

:bust_in_silhouette: Reply From: aXu_AP

You need to tell the script who should be the parent. For example, you can expose it as an export variable, so you can link it in the editor. Then you can call add_child on that node instead.

export var parent_path : NodePath
var parent

func _ready():
	if parent_path:
		parent = get_node(parent_path)

func LoadInstance(path):
	var scene = load(path);
	var instance = scene.instance();