Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoloaded singleton cannot be accessed at early phase during editor plugin start-up during editor start-up #77037

Closed
coderbloke opened this issue May 13, 2023 · 4 comments · Fixed by #94802

Comments

@coderbloke
Copy link

coderbloke commented May 13, 2023

Godot version

v4.0.1.stable.official [cacf499]

System information

Windows 11

Issue description

I have a editor plug-in. In "_enter_tree" it adds and autoload singleton + adds a Control to a dock.
The Control would come from the singleton.

The autoloaded singleton is not yet accessible just right after autoload, when:

  • Godot Editor (project opening) is starting-up, and plug-in is already loaded in project settings
    No matter if autoload is already in the project setting, or added fo the first time by the plug-in
    (Project open = Opening project.godot directly, not from project manager, but I assume it doesn't matter)

Not observed, when:

  • Editor is already running and the disabled plug-in is enabled.
    Error free even if singleton is removed from the autoload before enabling the plug-in.
@tool
class_name DebugInfoPlugin extends EditorPlugin

const AUTOLOAD_NAME = "DebugInfo"

var singleton: DebugInfoSingleton

func _enter_tree():
	add_autoload_singleton(AUTOLOAD_NAME, "res://addons/debug_info_plugin/DebugInfoSingleton.gd")
	singleton = get_node("/root/" + AUTOLOAD_NAME)
	add_control_to_dock(DOCK_SLOT_LEFT_BR, singleton._get_control())

Errors in console (when singleton is already added to the autoload during start-up, i.e. in previous session):

Node not found: "/root/DebugInfo" (absolute path attempted from "/root/@@16580/@@16699").
res://addons/debug_info_plugin/DebugInfoPlugin.gd:19 - Attempt to call function '_get_control' in base 'null instance' on a null instance.

If plug-in is adding the single to autoload for the first time during start-up (i.e. singleton is not yet in autoload list in Project Settings) an additional error message is in the console BEFORE the above two:

Parent node is busy setting up children, `add_child()` failed. Consider using `add_child.call_deferred(child)` instead.

This error with add_child is present, even, if I only have the add_autoload_singleton in the _enter_tree.
(Should I make separate ticket for this?)

Steps to reproduce

Create an editor plug-in similar to the one in description.
Have add_autoload_singleton and get_node in _enter_tree.
Make another @tool script, which will be the singleton to load.

Make the scenarios from description:
a) Open project.godot, with plug-in already enabled to project settings -> Error
b) Open project with disabled plug-in and -> No error
Further vary:
c) Singleton is already enabled in project setting's autoload before a or b
d) Singleton is removed from project setting's autoload before a or b

Minimal reproduction project

N/A

@AThousandShips
Copy link
Member

Related to #69387?

@coderbloke
Copy link
Author

coderbloke commented May 19, 2023

(Ohh sorry. It was a question to me?) #69387 rather seems to be for cases, when someone try to use the Autoload name directly as global variable name from the same plugin script which makes the autoload. For this reason I use the get_node by purpose.

My issue is not depending on whether autoload is added before plug-in start or not, but related to the godot engine start-up. From the error messages I assume not everything is set up yet in the engine, what is called during the plugin start-up.

@jinyangcruise
Copy link

excuse me, have you found a workaround? @coderbloke

@Jowan-Spooner
Copy link

I want to add some information to this. I would like this to get solved, but I understand it is somewhat intended/expected.

The problem

For context, I've been developing a plugin called Dialogic. Dialogic uses an Autoload to access a bunch of stuff. Many scripts within dialogic use that autoload. We add this autoload in _plugin_enabled() automatically.

However as described by others before, all those references result in errors whenever the plugin is installed, but not yet activated. This is expected behaviour, but it is pretty annoying because:

  • We totally know that these scripts CAN never be used without that autoload, that's just how that system is designed.
  • We have (obviously) NO way of adding the autoload BEFORE installing the plugin.
  • Why would godot care about the scripts containing errors when it doesn't even use them?

There would be multiple ways to adress this:
A)
Godot could not import script files in plugins until the plugin is enabled, preferably after _plugin_enabled() was called.

B)
Godot could let us specify that a certain keyword (like the name of an autoload) will be provided and should not cause an error.

Existing workaround

This is relatively easy to ignore. If you just enable the plugin and then restart godot the errors are usually gone. We have highlighted this BIG in our installation guide but we get weekly messages about "A lot of errors when installing" anyways. This is no problem for experienced users but it's pretty annoying for beginners.


Minimal Reproduction Project

I've provided a Minimal Reproduction Project. It contains a simple plugin that adds an autoload when enabled and has a script that uses this autoload. When opening the project you will get an error.
If you enable the plugin and restart godot there will be no error.
PluginMRP.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants