Godot can't find node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Chain
:warning: Old Version Published before Godot 3 was released.

Greetings Godot Community!

I am currently writing a little game and i came accross this problem:

var c = get_node("/root/Menu").get_text_speed()

Godot just can’t find the main node “Menu” and returns:

Attempt to call function 'get_text_speed' in base 'null instance' on a null instance.

The scene which i am trying to access has as it’s root node the node “Menu”

The only thing I can think of is that Godot can’t find the scene because it is in another folder. Foldersystem:

  • Scenes
    - Scene which tries to execute Command
    • UI Folder
      - Menu Scene

Look at the debugger, remote inspector when the game is running (or breaks), it shows you the current tree and you will see the structure, maybe you are missing something.

eons | 2017-08-24 20:14

:bust_in_silhouette: Reply From: quijipixel

Remember that using the node path /root/ gives you the Scene Tree. If Godot cannot find the Menu node, is because is not directly a child of the root node (and by root I mean the root of the whole application). Where are you instancing the Menu node? For that code to work, you have to include the Menu node in the Singleton autoload list in your projects configuration.

Ok, but thats not the way I want to handle this. I just want to access the Menu node to call a function there.
This is normally really easy, but doesn’t work for some reason. How should I access this node?

Chain | 2017-08-24 20:15

It depends on the node structure you have. If you don’t have the Menu in the Scene Tree then definitely /root/Menu is not the way. Can you put here how is your node structure? I need to know which node is trying to access Menu, and how are they organized.

quijipixel | 2017-08-24 20:28

Sure.


This is Scene1 which tries to acces the Menu Node:

Imgur: The magic of the Internet

with this Code (which is wrong):

var c = get_node("/root/Menu").get_text_speed()

This is the Menu Scene (which, well… is the Menu). The root node of this scene is called Menu:

Imgur: The magic of the Internet

This node has a script attached to it, which contains a function get_text_speed()


The file system of my project looks like this:

Imgur: The magic of the Internet

The Menu Scene is in the UI Folder and Scene1 in the Scene folder.

Chain | 2017-08-24 20:46

I see. Ok, the problem here is that there is a confusion between how scenes work and the file system. The get_node function allows you to access nodes, but this nodes must be instanced in the Scene Tree. This means you cannot access the node Menu (which is also a scene) because it is not loaded yet. You must understand that using the /root/ node path gives you access to the root node, and not to the root directory of your project in the filesystem. So the way you organice your directories and files has absolutely nothing to do with how you use the scenes in Godot engine.

To use your Menu node in Scene1 you have several approaches. You can create another scene, lets ctall it GameScene (or whatever) and add the Scene1 and Menu scenes as nodes. Then make this GameScene your main scene. Scene1 will be able to access Menu by using get_node('../Menu'). Your GameScene will function as a sort of Main game handler, and manage global stuff.

If you don’t like that approach, then using the Menu scene as a singleton and adding it to the autoload in your configuration might be what you are looking for. This adds the Menu scene to the Scene Tree when the game starts and makes it accesible to all scenes by using the method you already tried get_node('/root/Menu').

The other way to go about this is probably instance the Menu scene as a node inside the Scene1 node. Then you could call the menu by using get_node('Menu')

I hope this helps you decide how to approach your node design.

quijipixel | 2017-08-24 21:07

Oh thank you so much! Thousand thanks, this helped a lot :slight_smile:

Chain | 2017-08-24 21:15