0 votes

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

Godot version Godot v3.4.4
in Engine by (96 points)

2 Answers

0 votes

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()
by (35 points)
0 votes

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/UIbackground/Uibackground/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.
by (2,634 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 Frequently asked questions and 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 [email protected] with your username.