0 votes

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
in Engine by (72 points)

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.

1 Answer

+3 votes
Best answer

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.

by (697 points)
selected by

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?

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.

Sure.


This is Scene1 which tries to acces the Menu Node:

http://imgur.com/a/3IlMt

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:

http://imgur.com/a/X0ygH

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


The file system of my project looks like this:

http://imgur.com/a/49tWz

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

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.

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

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.