How to add a new child node to a specific node inside the current scene via gdscript?

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

I currently have a node structure like this:
World > YSort > Player > Camera2D

When a certain input is made, an inventory shall open, that follows the player around. To create this effect I wrote the following function inside my global singleton:

func open_ui():
var scene = ResourceLoader.load("res://UserInterface.tscn")
current_scene = scene.instance()
get_tree().get_root().get_node("YSort/Player/Camera2D").add_child(current_scene)
get_tree().set_current_scene(current_scene)

The problem occurs in line 4. The debugger says: Attempt to call function ‘add_child()’ in base null instance on a null instance.
I copied the Node path YSort/Player/Camera2D from right click on Camera2D and choosing: Copy node path.
I figure that I am not adding the inventory scene in the right way, but I could not find a way to make it work.
In general I have had issues with finding/ referring to nodes even though I read many tutorials on it.

:bust_in_silhouette: Reply From: p7f

I think you are missing the World in your path…

get_tree().get_root().get_node("World/YSort/Player/Camera2D").add_child(current_scene)

get_tree().get_root() returns the root viewport of the scene.

Also, on a second note, i think you shouldnt add user interface as a child of a camera, but instead place the user interface on a canvas layer.

And lastly, why are you setting the user inteerface, added as child of the camera, as the tree current scene?

Thanks a lot!

I want an inventory that moves with the player. To accomplish this, I am playing around with several ideas. One of them was to make it child of the camera - maybe a bit too naive. But up until now I have only created static inventories which pause the game while the user does things inside it.

You mentioned a canvas layer would be a better way to do this. Where should I put it best inside my project structure? Are there best practices for it?

Ciavarie | 2020-09-03 13:39

Ah, i thought it was more like an HUD.

About the canvas layer, you can just put it into the scene as child of world. But is your UserInterface.tscn inheriting from a control node? Can you share the structure of that scene? You want something to appear in the middle of the screen? and why instancing it on runtime instead of makint it always present, and just changing visibility?

p7f | 2020-09-03 13:57

Basically it is a grid of boxes, that shows up when a button is pressed and disappears if it is pressed again. The user interface name is abit far fetched.

Yes, it inherits from a control node. The UI structure is basically this:
Control > TextureRect > VBoxContainer > GridContainer > a bunch of Panesl and texture buttons

I want it to appear in the center of the screen above the player, but the player can still move around.

For the last one: Simply because this possibility did not occur to me - not my brightest moment tbh. I guess this would be much better, performancewise.

Ciavarie | 2020-09-03 14:13

I added the ui to a new canvas layer which I set as child of World. It does not follow the player, viewport or anything. It just stays a static inventory. So I guess i will add it to the Camera and work with changing visibility.

Ciavarie | 2020-09-03 14:28

Thats strange… thats the way i always do user interfaces in all the games, idk why is not working fot you. But whatever works for you, use it! BTW, the original question, about why you couldn’t use add_child was solved?

p7f | 2020-09-03 14:41

Yes, you solved it :slight_smile: A severe case of sudden inability to read paths on my side.

I think, it is strange too, because after reading through some stuff, it should work as yous suggested, but just adding it to the camera works as well. My inventory finally does what it is supposed to do.

Ciavarie | 2020-09-03 15:14

Cool! glad yo help.

Yeah, idk why is not working yo you the canvas layer… but as long as it works your way, i think you are good. The problem is that if you add smooth to the camera, probably the child will also move with it the same way.

p7f | 2020-09-03 15:30

I do not plan on adding any effect to the camera - it is a pixel art rpg so I do not see the point. As long as everything just moves along with the player, I am fine. So thanks a bunch!

Ciavarie | 2020-09-03 15:38