0 votes

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?

enter image description here

in Engine by (17 points)

1 Answer

0 votes

I assume you didn't read https://docs.godotengine.org/en/stable/getting_started/step_by_step/scenes_and_nodes.html which covers every aspects of your question.

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.

Quick tips:

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.

by (296 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.