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

Autoload scenes with a @tool script in the top node get loaded into the editor as a child of the root #71373

Open
miintTeaa opened this issue Jan 14, 2023 · 5 comments

Comments

@miintTeaa
Copy link

Godot version

3.5.1.stable, v4.0.beta10 (d0398f6)

System information

Linux Mint 21.1

Issue description

If you mark a scene with a tool script in the root node as an autoload, it will actually be loaded into the editor. This happens as soon as the scene is marked as an autoload, and also every time you open the editor.

If there's an error in the script, the editor will crash every time it opens, requiring you to edit the script with an external editor to fix it.

It looks like this:
image

Tested in both 3.5.1.stable and v4.0.beta10 (d0398f62f)

Expected behavior was for the scene to only be loaded when the game was actually running.

Steps to reproduce

  1. Make a new project
  2. Create a scene
  3. Attach a sprite to it so you can see it load in
  4. Attach a tool script to it
  5. Make that scene an Autoload

Minimal reproduction project

project.zip

@KoBeWi
Copy link
Member

KoBeWi commented Jan 14, 2023

Well, tool scripts run inside editor. If you don't want it in the editor, why use tool?

@miintTeaa
Copy link
Author

Well, tool scripts run inside editor. If you don't want it in the editor, why use tool?

The issue is not that it's running in the editor, it's that it's creating an instance of the autoload on the root node (the editor window itself as opposed to normal behavior where tool scrips just run in the editor viewport).

You can see in the example image I sent this results in the node appearing on top of the editor.

This is avoidable by not creating an autoload tool script, but considering it can crash the editor on startup it should probably be looked at.

@Giraaffes
Copy link

Giraaffes commented Mar 22, 2023

A workaround is to add the following code in the _ready()function of the tool script:

if Engine.is_editor_hint() and get_viewport() is Window: 
	get_parent().remove_child(self)
	return

@Kailari
Copy link

Kailari commented Dec 23, 2024

A bit of a necro-bump, but I encountered this issue just recently.

Well, tool scripts run inside editor. If you don't want it in the editor, why use tool?

When scripting in C#, a [Tool] is needed any time you want to use _GetConfigurationWarnings. Furthermore, as outlined in here, the [Tool]-usage in C# cascades to any referenced classes, somewhat increasing chances of bumping into this.

That is, for configuration warnings to work, I need an instance of a [Tool] script within the scene, even though I have no use for a global instance in-editor.

For context, I'm working on a project where we have a autoloaded "persistent scene" where things we wish to persist get reparented before switching scenes. This scene has a script with a number of referenced child nodes. Having custom configuration warnings for ensuring everything is set up correctly is quite useful for this use-case.

However, things get funky when the persistent scene has the default player instance, and the main camera following the player is a child of the default player instance. This breaks the editor when the persistent scene gets initialized as child of the editor root window:
Image

Here, the persistent scene has been set up as [Tool] to get the configuration warnings. This causes a global instance of the autoload to be created as the child of the editor root when the project is loaded. The autoloaded scene contains the default player, which contains the default camera. However, assumingly because the autoloads are always the first children of the root, the camera contained within the persistent scene takes precedence over the default "editor camera", completely breaking the editor window as most of the editor is now outside the active camera view.

I was able to work around this by getting rid of the editor-wide global instance as suggested in the workaround posted above.

EDIT: Now that I'm done debugging what's going on, I think this would have been much less surprising if I had known that autoloaded [Tool] scripts get global instances in-editor. I don't think the behaviour is necessarily wrong, it is just very surprising as the docs for autoloads nor the docs for @tool don't warn about this. To be more exact, the docs claim that autoloads aren't accessible in-editor, at all.

@KoBeWi
Copy link
Member

KoBeWi commented Dec 23, 2024

To be more exact, the docs claim that autoloads aren't accessible in-editor, at all.

Well, that's wrong.

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

No branches or pull requests

4 participants