+2 votes

How do I use the code below in a singleton because the singleton is where in the tree? Like that access to the other nodes of it, I'm trying like this and it does not work:

onready var controlSample = get_node("../SamplePlayer2D")
in Engine by (203 points)

1 Answer

+2 votes
Best answer

Your singleton(s) are siblings of your main_scene.

If the top node of your main_scene is called Game you access it with an absolute path get_node("/root/Game").

if you have a singleton named Global you access it with an absolute path get_node("/root/Global").

But know that singletons call _ready() before the mainscene, so you wont be able to access the mainscene within a singleton's _ready().

You can, however, define a var controlSample in your singleton script. In the main_scene's script which handles this object, you can have it define itself to the singleton in its _ready():

func _ready():
  Global.controlSample = get_node( "SamplePlayer2D" )
by (1,330 points)
selected by

I have recently needed to access the Main node from an autoload singleton file. When I try what is suggested above, I get an error saying I cant use Absolute Paths outside of the SceneTree.
What can I do?

I have the same problem

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.