godotengine / godot-docs

Godot Engine official documentation
https://docs.godotengine.org
Other
3.78k stars 3.07k forks source link

GDScript: Unused EditorPlugin code in hybrid tool / game script doesn't work after export #4082

Open m6502 opened 4 years ago

m6502 commented 4 years ago

I think this is basically the same issue: https://github.com/godotengine/godot/issues/41196

In this case, though, I'm using GDScript instead of C# and there's no way to enclose the offending code in preprocessor directives.

Godot version:

I'm using the official 3.2.2 Godot build.

OS/device including version:

Not related to any specific backend.

Issue description:

One of my scripts contains game functionality plus tool functionality. When in tool mode, at init, I need to create an instance of EditorPlugin so I can reach the editor interface. The code is inside an if statement checking for the editor:

    if(Engine.editor_hint):

        var editor_plugin_temp = EditorPlugin.new() ;
        editor_interface = editor_plugin_temp.get_editor_interface() ;
        editor_plugin_temp.queue_free()

Works as expected and the resulting editor interface is only ever used when in tool mode and always after checking for the editor presence. The problem though is when trying to export the game. EditorPlugin doesn't exist then, and the resulting build doesn't work because the script can't be loaded. Here is an screenshot of the output log with Windows, but this happens with html5 and I suppose any other platform too:

SCRIPT ERROR: GDScript::load_byte_code: Parse Error: The identifier "EditorPlugin" isn't declared in the current scope.

Steps to reproduce:

Try to include the above code in a project or just use the enclosed test project.

Minimal reproduction project:

EditorPluginTroubles.zip

This project only shows one sprite that is marked as not visible in the project tree, but the script makes it visible at _ready(). If the script fails then the sprite remains invisible and only the background is shown.

So this really isn't a bug per se, and I was hesitant about opening an issue or a proposal, but my gut instinct tells me there's something not quite right in this situation that could be improved. If there's a an alternate way to get the editor interface I'm interested in knowing too :-)

TheDuriel commented 4 years ago

Honestly yeah, this is not a bug. You should rebuild your code to actually use an EditorPlugin for this. And then invoke that plugin, instead of instancing a new object whenever you need the editor for something.

A good old case of "you're doing it wrong".

NathanLovato commented 3 years ago

The EditorPlugin class could mention it's only intended to be extended and registered as a plugin in the editor. Moving to the docs repo.