This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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()

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.

in Engine by (57 points)

1 Answer

+2 votes
Best answer

I think you are missing the World in your path...


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?

by (3,505 points)
selected by

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?

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?

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.

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.

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?

Yes, you solved it :) 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.

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.

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!

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.