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”)