Should `@tool` apply to extended scripts?

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I’m developing an editor plugin that has a 'main screen" and a number of custom control panels and dialogs implemented as scenes. The scene scripts necessarily include the @tool annotation. A major problem I’ve encountered is the editor scenes rely on initialization that only occurs when the plugin is enabled. If I attempt to edit one of these scenes with the plugin disabled, numerous errors are output to the console window. In a few cases the editor crashes and some of the data files maintained by the plugin are corrupted. This is not a problem if the plugin is enabled while editing the scenes but live editing plugin code while the plugin is enabled can create similar results. The problem comes down to the @tool scene scripts run when the scenes are opened for editing. That is necessary for custom controls and such to work.

I’ve thought about how to solve the problem. I’ve tried putting checks in the code to detect the difference between the scene being instantiated by the plugin versus the editor but haven’t found a reliable solution.

These are the ideas I’ve considered.

Option 1: Make the @tool annotation apply to extended scripts. This would allow attaching normal scene scripts (no @tool) to allow editing the scenes without the nasty side effects. Then the plugin logic can replace the scene’s script with one that extends the non-tool script and includes the tool annotation. For this to work, the @tool annotation must apply to code contained in the extended script.

Option 2: Provide a way to set a script to ‘tool-mode’ in the plugin code.

Option 3: Provide a way to specify whether the @tool annotation applies during scene editing.

Are there other options?

You can check project settings if the plugin is disabled and disable the tool processing

Actually the method is EditorInterface.is_plugin_enabled() which is available only when running `code in the editor. Code that is needed by both the plugin and the exported game cannot use this method.

That is one of the approaches I’ve taken but it means adding the check to every virtual method in every @tool class.

It does not address the problem when the plugin is enabled and a plugin scene is opened for editing. If changes require editing multiple plugin scenes you have to turn off auto-save so the running plugin code is not applied until all affected scenes are changed. Then a manual code save has to be done.

There is also Engine.is_editor_hint()

If you are talking about an exported game tools will not run anyway. But if the underlying tool class is used in the game that depends on a editor plugin you have an architecture issue.

But if you can avoid code with is_editor_hint() maybe that can help?

I would close all scene tabs if that is the case. If the tool scene tab is open it is running.

Which is fine except when you need to edit the tool scene.

I’ll do more research and trial/error coding. If I can’t find a reasonable work around I 'll submit a change request to make @tool apply to code in extended scripts.