How can I access functions, variables etc in a node, from a different scene

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

I tried this but didn’t work:

var node = get_tree().get_root().get_node("node path")

E 0:00:08:0714   Node not found: res://Player/Scenes/Player/Player
  <C Source>     scene/main/node.cpp:1382 @ get_node()
  <Stack Trace> @ _on_PowerUp_body_entered()

are your sure the node path is correct?

volzhs | 2019-02-15 12:32

The error you get means at the moment your get_node code runs, there is no node in the tree that matches the path you are providing. Perhaps the path is wrong, or perhaps the node is no longer available because the scene might have changed.

Also, you provided res://Player/Scenes/Player/Player, which is not a node path. Node paths are only valid within the currently loaded scene, and they don’t start with res://.

Did you change the current scene? Can you explain what is the structure of your scenes and what you are really trying to achieve?

Zylann | 2019-02-15 13:47

I know node paths don’t start with res://, it was just the last attempt I did…

In details, I want to modify Player’s abilities when he picks up an object defined in a different scene, but to do that I need the variables in the Player scene

Marco | 2019-02-15 14:55

I tried everything

Marco | 2019-02-15 14:58

Can you explain what your node tree looks like?

I want to modify Player’s abilities when he picks up an object defined in a different scene

I’m asking that because you say that your player is picking an object defined in a different scene, but that implies that this object and the player are both present in the tree (otherwise they could not interact).

Zylann | 2019-02-15 19:51

:bust_in_silhouette: Reply From: TheFamousRat

Hello Marco,

If you really need to do that, I would advise you to store the node’s path in a singleton script. You can do that by creating a script, then going to Project → AutoLoad, where you will add your script and check “Enable” under Singleton.

(For the rest we’ll suppose you named your Script “”, but you can name it something else of course).

Then, in this script, create a NodePath variable containing your node’s path and its setters and getters :

extends Node

var myNodePath : NodePath = ""

func setMyNodePath(newPath : NodePath) -> void:
	myNodePath = newPath

func getMyNodePath() -> NodePath:
	return myNodePath

In the “_ready()” method of the Node you want to access, just run a “Global.setMyNodePath(self.get_path())” somewhere. Conversely, if you want to access said node, just run “get_node(Global.getMyNodePath())”

Hope this helps

Identifier Global is not declared, I guess I’m doing something wrong

Marco | 2019-02-15 16:11

Did you set “Global” to be a singleton ? You can also right click on the script and select “Soft reload”, sometimes Godot needs to reload the scripts, especially if you just created them.

TheFamousRat | 2019-02-15 16:51