For clarity: A res://
path does not exist in the scene tree, it exists in the virtual file system. As such, you cannot access it from get_node
. Because get_node
works on the scene tree, thus you need to load and instantiate the scene in the scene tree first.
If you have added the scene to the scene tree in the editor, then it will load it and instantiate it for you, and there will be a path you can put in get_node
... But it will not be a res://
path, because, again, a res://
path exists in the virtual file system.
You give res://
paths to load
(or load_interactive
, see Background loading, or change_scene
), not to get_node
.
Now, either you are trying to target a scene that is loaded or one that isn't.
If your other scene is not loaded, you need to load it (load
or load_interactive
) which gives you a PackedScene
, instantiate it (instance
) which gives you a Node
. At which point you have a reference, go ahead an use it. You probably want to add to the scene tree (e.g. add_child
).
For example, here we attach the new scene to the root (which may or may not be what you want, in particular in the face of changing scenes, but that is another topic):
func _ready():
var packed_scene = load("res://something.tscn")
var scene_node = packed_scene.instance()
var root = get_tree().get_root()
root.add_child(scene_node)
scene_node.visible = false # or whatever
I will suggest to use a PackedScene
export:
export(PackedScene) var packed_scene
onready var scene_node = load(packed_scene).instance()
func _ready():
var root = get_tree().get_root()
root.add_child(scene_node)
scene_node.visible = false # or whatever
Then in the inspector panel you will see a new property "Packed Scene" (Godot auto capitalizes the name) where you can specify the scene from the file system. Plus, if you move scenes in the file system in the editor, the editor will update the path for you.
Note: you may or may not want to add them to get_tree().get_root()
. Other options include adding it to get_tree().get_current_scene()
, get_owner()
, get_parent()
and to self
.
On the other hand, both your scenes are already loaded in the scene tree. In which case, there is a path from one node to the other, which you can put in get_node
. If your target scene is not a child, you probably want to go up a few levels with ..
. However, I would advice against hard-coding such path.
Instead, you will get a lot of leverage of the following:
export(NodePath) var target_scene
onready var scene_node = get_node(target_scene)
Then in the inspector panel you will see a new property "Target Scene" where you can specify where in the scene tree the node you want to access is. Plus, if you move the nodes around in the scene tree in the editor, it will update the path for you.
Then you can do:
func _ready():
scene_node.visible = false # or whatever
Note: If you don't know if the scene is loaded. You might be interested in using find_node
. Control
also provide a get_node_or_null
.