get_node from singleton

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Tentamens

How would I called a node in a scene from a singleton?

what I am trying to use right now:

  get_node('UI/UI_background/Ui_background/VBoxContainer/TextureButton').queue_free()

I just get a null tho

:bust_in_silhouette: Reply From: matthewjw

I am not an expert, but I think you might need to load the scene, then instance it:

var TextureButton = load("UI/UI_background/Ui_background/VBoxContainer/TextureButton")

func _ready():
	var instance = TextureButton.instance()
	instance.queue_free()
:bust_in_silhouette: Reply From: Lopy

The instance of your script created thought the autoload mechanism is a direct child of the “root” node in the SceneTree. Your tree looks like this when the game is running:

/root
/root/MyAutoload1
/root/MyAutoload2
/root/MyScene
/root/MyScene/...

To better see what is happening, start your game, then focus the Editor without closing the game, and just above the tree view on your left, select “Remote”. This will show you the tree of your game as it is running. You can also call $"/root".print_tree_pretty() (inside some Node’s _ready()) for a textual representation.


To access your Button, you can use:

  • Direct tree walking, with $“/root/MyScene/UI/UI_background/Ui_background/VBoxContainer/TextureButton”, or get_node() with the same path.
  • Have the Button (or someone that has an easy access to it), register it to your Autoload. That would look like MyAutoload.sacrificial_button = self inside the Button’s script, and if is_instance_valid(sacrificial_button): sacrificial_button.queue_free() inside the Autoload.