Topic was automatically imported from the old Question2Answer platform.
Asked By
h06y
Hi, i’ve a singleton in which i have a function that when is called it calls a function in a certain GDscript attached to a node (a map). I tried so many ways but i can’t get the code that i need, basically i need something like this:
get_node(“The node of the map”).callMyFunction()
but it always says my node is NULL, is there any way to call a certain node ignoring where it is on the project?
MapaDePrueba.tscn is not a Node, It’s a PackedScene. The screenshot you showed is your file system, which contains a bunch of Resource, which haven’t been add in your SceneTree. Scene tree is where the actual game happens in real time.
Also, Godot doesn’t care where you put your scripts in the file system. It only cares which node has your scripts.
When you try to get a node, you use node_path and node.name. *.tscn is just file name. These below are node names. Node paths are more complex, just read the docs I linked above.
If you want to get a node from singleton. You first var map = load("res://Scenes/Maps/MapaDePrueba.tscn").instance(), then add_child(map). Then you have it, map is your node. But in this way, the map is your singleton’s child.
If you want to get a node from anywhere, just get_node("/root/path_to_the_node"), this is the standard way of doing it.
If you want to get a node without knowing the path. just get_tree().get_root().find_node("node_name"). This is not the best practice, because it’s relativey slow.