How do you access an autoload singleton from an addon script?

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


I was working on an addon for Godot 4.0 and everything was mostly working as intended until I decided to test the addon in another project only to get a syntax error that pop out of nowhere. Turns out once the singleton is loaded, it can be referenced in the addon script. Howerver, this causes the syntax error once the addon is disabled and therefore cannot be re-enabled.

A minimal recreation of what is happening. This error pops up when the addon is unloaded or inserted into another project.

When previously it was working fine.

Is there a way to load and use the singleton in the addon script that doesn’t cause this kind of error?

The addon is a tool script, likely being run in the editor that disabling now means it doesn’t which means its handlers aren’t running the add_autoload_singleton; it isn’t in the tree until executed so SomeAutoload is undefined.

spaceyjase | 2023-05-17 11:49

Would doing a quick null check fix it?

if SomeAutoload:

or are you saying that the issue is the syntax highlighter doesn’t understand where SomeAutoload is coming from?

Tom Mertz | 2023-05-17 18:31

Yes, this is an issue with the syntax highlighter. At the moment, I’m using a workaround where I just use a separate script to run the code that needs the singleton.

Icy Unicorn | 2023-05-22 01:56