HI, I'm trying to make some objects globally accessible, but I run into problems with what I think is the objects not being fully initialized when I try to access them.
I have a global script (as described in the 'Singleton' / autuload docs which defines a var, for intance like this:
var debug
In the 'debug' Node2D (which is a child if the top node, and is supposed to be displaying labels with messages from various objects) I have this:
onready var global = get_node("/root/global") func _ready(): -- global.debug = self
Now I was hoping to be able to access the debug node from anywhere as global.debug, like this for instance: onready var global = get_node("/root/global") func use_debug(): -- global.debug.out ("Hooray, it's working!!") I get the error "Invalid call. Nonexistent function 'out' in base 'Nil'." So I suppose, there's no debug object there to call yet. I made this experiment (in the global script) <code>var debug = Dummy.new() class Dummy: -- func out(a, b): ----- print ("Dummy.out called")
and confirmed that Dummy.out was called once before being replaced with the actual debug object.
I also tried waiting for the debug node to be initialized, but that had the effect of freezing the game (i e, waiting for the other object to be ready blocked the thread responsible for initializing the object). Like so: while global.debug==null: -- pass
So after all this, I wonder 1. Are nodes initialized in a particular order, so that this deadlock might be prevented somehow (i e initialize 'debug' node first) 2. Is this a stupid approach? I know I could get the 'debug' through it's URL since I know where in the scene tree it's located, but there might be other cases where I want global objects (such as player) or objects that persist through scene changes. Still, perhaps it's wiser to use the global singleton just to store values, and initialize objects anew in each scene. How do you handle this?