Issues with Accessing Nodes from 2 Different Scenes

Godot Version

4.2.1

Question

I’m sorry if this question has been answered before, but I’m at my wits end with this problem. I’ve been attempting to get access to a node in a scene from a different scene. For example, if the value of a node in one scene changes, a node from another scene changes. So far this is the closest I’ve gotten to solving it on my own:

#I attempt to preload the scene.
@onready var node = preload("res://scenes/scene.tscn")

#I then attempt to make the node from the scene a callable variable.
@onready var child = node.get_node("Sibling1/TargetNode")

When I run this code, I get an error: "Invalid Call: Nonexistant function “get_node” in base “PackedScene”

I’m completely stumped otherwise. The two scenes that I’m calling are in the same main tree, so I don’t think it’s impossible to do.

Any help would be appreciated, thank you!

Are you familiar with signals? I highly recommend learning about them by reading this guide from the docs, signals are very useful when solving problems like that. Create a signal value_changed(value) that you emit when the value changes in one scene and then connect it to a function in another scene. Sometimes connecting a signal locally can get too complex and you might need an event bus (or signal bus) autoload. There are tutorials on that too.

I think the reason it might not be working is that you are calling get_node on what is just a packedscene reference. I am a rookie myself so this is to the best of my understanding but a packedscene doesn’t actually exist in the tree, it’s just a packed data file that you can use to spawn a scene in the tree from a node.

So what you might want to do is instantiate the Packedscene first and give it a name you can reference in this case I used x (please double check my spelling I wanted to just quickly type it before I went to bed)

var x = node.instantiate()
add_child(x)
x.get_node(“Sibling1/TargetNode”)

And if you already have it in the tree, you need to find a different way to reference that scene because the packedscene will not point to it. Try maybe using a group, a unique name or just using an @export and hardcoding the reference to that scene.

Good Luck and let me know if this helps!

1 Like