How to point to a node in another scene

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

I’m going to use a variable from a node in another scene

They are all in the same scene tree.

get_node (“/ root / scene_tree_name / node_name”) I wrote this

However, I get an error that it does not exist.

get_tree (). get_node (“node_name”) I wrote this again, but there is an error in ‘scenetree’ that there is no function ‘get_node’

There are too many errors in this engine that the function does not exist

Obviously, watching the video on the Internet often results in the same error.

Even a variable named ‘.name’ will get invalid get index ‘name’ on base: scene tree

But not in the video I’m watching
: https://www.youtube.com/watch?v=WUARiOGSGKY

I came back to the point

In the same scene, there are two scenes called as instance scenes

I want to write a variable created from a script in another scene in one scene

In the script of the instance scene other than the script in the scene tree

How do I point to another scene at this time?

:bust_in_silhouette: Reply From: wombatstampede

There are two ways.
#1 you can set variables and call functions directly in the instanced scene.
#2 you use a global “Singleton” and set variables there

#2 using has the advantage that the variables “survive” loading/unloading of scenes. So this is recommended if you want to manage scores and player achievements.
Read here for that:

#1 is what you asked.
If you want to access another scene then you (usually) need to watch where they exist in the scene tree and from which context you are calling.

Context: Is the node where the script is attached to. This node is also adressable via the address self but normally, you don’t need to use it. It is used implictly.
So:
print(name)
And:
print(self.name)
Both output the name of the node where the script runs in.

Let’s assume, you have a scene which looks like this:

 * MyRoot
     * Instance1
     * Instance2
         * child1

You can think of the nodes being directories. You can use absolute or relative adressing. But also remember: All node names are case sensitive. So watch your spelling.

Let’s assume you are running the code from a script which is attached to “Instance1”.

var othernode = get_node("../Instance2") will get you the other node. It uses a relative path.
(One “directory” up, then the "directory entry ‘Instance2’)

get_node("../Instance2").somevariable = 20 lets you set a variable in that nodes attached script

get_node("../Instance2/child") wil give you the child node

There’s also another spelling/shortcut: $"../Instance2/child"

If you are calling from a script which is attached to the scenes root node (here: MyRoot) then you can access the nodes like this:
var inst1 = get_node("Instance1") or var inst1 = $Instance1
or
var inst2_child = get_node("Instance2/child")

So if there isn’t a “/” at the path start then the node path is relative. If you use “/” then the adressing will go via the root Viewport with the adress “/root”.

Read about this here:

To make things clear: SceneTree (the thing you get via get_tree()) is no Node. Therefore it has not the functions or variables of any node (neither name nor get_node).

The upmost Node in the SceneTree itself is the root Viewport. This is obtainable via get_tree.get_root() or get_node("/root"). Loaded scenes are usually attached as children of the root Viewport. Godot surely has errors here and there. But the problems which you described are none of these.

I recommend using relative paths when adressing other nodes.

Thank you for answer

I changed from get_tree.get_node (“Instance”), get_node (“/ root / Instance”) to get_node (“…/ Instance”)

The issue has been resolved!

MungMoong | 2019-03-20 11:18