|
|
|
 |
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?
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.
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 