Load autoloads in editor

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

Is there a reliable way to load autoload scripts inside the editor?
I’m trying to have my game running in the editor, I was able to activate the physics engine and by adding @tool on top of my scripts meshes are generated and everything works. The problem is “sometimes” autoloads load and “sometimes” they don’t. So for example I add a new variable to an autoload script, when I try to read it in other scripts I get “Invalid get index”, then I close Godot and reopen it and close scene and reopen it and delete autoload and add it again and after doing all these several times it suddenly starts to work. Then I add another variable and I do the above several times but this time it doesn’t work and the first one stops working too, then I rename the variable and after half an hour it starts to work.
My observation is that when it starts working, it doesn’t stop working until I add/rename a variable.
Is there an official clean way to load autoload scripts in the editor in a way that I can access them with their name instead of get_nodeing them?

:bust_in_silhouette: Reply From: sassanh

Update: I just noticed it works in the editor when I add @tool on top of the autoload script.


I did this as an alternative way to have a global instance in the game as well as in the editor:

func _ready() -> void:
  if not owner.get_node_or_null('./globals'):
    var globals = Globals.new()
    globals.set_name('globals')
    owner.call_deferred('add_child', globals)

and whenever I want to read from it I write something like this

owner.get_node('./globals')

I just need to take care of manually setting owner for all the nodes I add to the scene dynamically (nodes added via the editor have the owner set automatically)

I’m still interested in an approach to have autoloads loaded in the editor to avoid the complexity of the approach I described.