Why is my singleton unaccessable?

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

I have a script that I have added as an AutoLoad in project settings in order for the variables established in the script to be global. However, I cannot access the variables in the script unless I assign the script to some scene in my project, is this intended behavior?

When the script is not assigned to anything, but IS set as an AutoLoad, initially I get an error that the referenced script is not established in the scope of whatever other script I’m trying to reference the global variables from. This error goes away when I restart Godot, but then it throws an error when I try to run my project, telling me the referenced global script is a null instance…

:bust_in_silhouette: Reply From: Inces

There is a known bug in godot 3.2 which loads autoload too slow compared to scenes ready. It happens when total project became large and error happens 30-50 % of times on trying to run the project.
If You can’t run project at all, that means You must have done some mistake regarding setting autoload. Is your autoload simple Node ? Can You show pieces of this code ?

You know what, it’s working now. Not sure what I changed, but yeah, it seems like it was just that glitch and changes I’ve made since the issues started have fixed it, interestingly enough.

YangTegap | 2022-05-25 17:39

This is very frustrating issue, You will repeatedly encounter it every time You work on project over than an hour or just leave Godot open for longer time. Resetting temporarily helps.

Inces | 2022-05-25 20:09

It’s better to not use Autoload ain’t no more, just for now, as the bug is not fixed yet, even on gd4…

thebluetropics | 2023-02-16 06:21

I learned to use Autoload very minimallistically now, and I feel my code is much more ellegant now. The key is to use static coding instead - keep unchangable data and calculations in some class and call it from anywhere in code.

Inces | 2023-02-16 15:48

:bust_in_silhouette: Reply From: CharlesMerriam

I have no answer, only a work-around.

The documentation says a script can be an autoload, and I have used a script as a collection of static functions. The documentation stays silent on what, exactly, is a script as an autoload, what and when variables are evaluated, and what is available to a tool.

The simple work around is to create a new scene with just a node and that attached script. Make the new scene an autoload. Being a scene, it will follow the normal lifecycle and be made ready before any of your written scenes.

This is great information, an awesome suggestions, thank you!

YangTegap | 2022-05-25 18:56