pkdawson / imgui-godot

Dear ImGui plugin for Godot 4
MIT License
318 stars 18 forks source link

Add support for running ImGui in the Editor ([Tool]) #31

Closed c-schembri closed 2 months ago

c-schembri commented 1 year ago

Currently, if you run any ImGuiNET code in the editor Godot will crash, and you will have to delete the offending code and rebuild to stop the crashing.

I'm not 100% sure if the issue is with this plugin or ImGuiNET generally, but I thought it was worth bringing to this project's attention.

Minimal example (and attach to a Node in the scene):

using Godot;
using ImGuiNET;

namespace Example;

[Tool]
public sealed partial class MyTool : Node
{
    public override void _Process(double delta)
    {
        ImGui.Text("Hello from the editor!");
    }
}
pkdawson commented 1 year ago

Right, the plugin's autoload singleton isn't loaded by the editor, so ImGui isn't initialized. I'll see if I can find a good way to make that an option.

pkdawson commented 1 year ago

I'm having a problem where an in-editor ImGui window won't respond to input - unless I add a GD.Print() call every frame, then it works fine. Very confusing. I'll try to figure it out.

pkdawson commented 1 year ago

Oh ok, it's just the editor trying to avoid constantly redrawing itself. It works if I turn on Update Continuously. I'll either document that or find a workaround.

pkdawson commented 1 year ago

The main difficulty here is that imgui-godot is part of the main project assembly, so whenever you compile, everything needs to be cleanly reloaded. I can get that mostly working, but it's buggy enough that it's not really usable. And I suspect that even if I can fix all the bugs, future Godot releases will be likely to break it.

The solution would be to move imgui-godot to its own GDExtension, which I was planning to do whenever C# GDExtension support is available. I could also make a C++ GDExtension, which has a number of other advantages.

c-schembri commented 1 year ago

I guess we can just leave this issue open for now. Whether you want to add support for this feature or not is completely up to you, of course. This request is not a super important one (if really needed one can just make an ImGui standalone executable). But I appreciate you looking into this and updating the issue constantly. It's not everyday you get to find OSS that has a reliable owner! So thank you!

pkdawson commented 1 year ago

I'm always happy to see people actually using my addon, so I like getting feature requests.

I've been working on the C++ GDExtension, I expect to have a beta ready in 3-4 weeks. Running in-editor is still a little tricky, but far more stable than trying to do it in pure C#.

c-schembri commented 1 year ago

Appreciate it. If I ever get around to releasing my game (a tall order, as every game developer knows), I will definitely credit you for this work.

pkdawson commented 10 months ago

Sorry for the delay, it took me forever to get around to writing even basic docs.

I still have some work to do on other features, but the in-editor GUI stuff should be stable.

If you feel like trying out the beta, I'd very much welcome any feedback about usability, bug reports, or questions.

https://github.com/pkdawson/imgui-godot/releases/tag/v5.0.0-beta1

https://github.com/pkdawson/imgui-godot/wiki/In%E2%80%90editor-GUI

c-schembri commented 10 months ago

No problem. Thanks for the work you've put into this! I greatly appreciate it.

It seems that this release required your project to be using Godot 4.1, is that correct? I'm still using 4.0.3 so I have to update my project when I work on it next. Afterwards, I'll give the new release a go and let you know if I notice any issues or bugs or have any questions.

Again, thank you for your work on this.

pkdawson commented 10 months ago

Yeah there's a new GDExtension API in Godot 4.1, so that's the minimum version.

c-schembri commented 10 months ago

I've been trying to change the font of the ImGui interface but nothing seems to be working. I've tried the ImGuiGD.AddFont method as well as the method in the README with the ImGuiConfig.tres configuration--but neither are working. Any advice?

pkdawson commented 10 months ago

To configure the native plugin, you can set the config resource on the ImGuiGodot node in res://addons/imgui-godot-native/ImGuiGodot.tscn. I need to document that.

I originally intended to have it use the configuration from the C# node as a fallback, but there were some technical complications. I'll see if I can figure out how to get that working.

The ImGuiGD.AddFont stuff should work. It's the same as before: ResetFonts, then AddFont, then RebuildFontAtlas (in an _EnterTree or _Ready method).

https://github.com/pkdawson/imgui-godot/blob/782946e5b33233721f206c63b2b2aae6ae1b67f9/src/MySecondNode.cs#L39-L47

Admittedly not the nicest API, but I figured not many people would want to use it anyway.

c-schembri commented 10 months ago

Thanks for that information. I got the fonts working through the ImGuiGD class. One major issue I am experiencing is that when I rebuild the [Tool] which uses ImGui code, I often get the error found at https://github.com/godotengine/godot/issues/78513. This error forces me to re-open the project entirely, making it very slow to get any work done.

I don't know if you can do anything about it, because it seems like an issue outside of this domain. But I just wanted to report my experience.

c-schembri commented 10 months ago

Here's the actual error reported by the Godot Editor:

  modules/mono/mono_gd/gd_mono.cpp:513 - .NET: Failed to unload assemblies. Please check https://github.com/godotengine/godot/issues/78513 for more information. (User)
  modules/mono/mono_gd/gd_mono.cpp:513 - .NET: Failed to unload assemblies. Please check https://github.com/godotengine/godot/issues/78513 for more information. (User)
  modules/mono/mono_gd/gd_mono.cpp:513 - .NET: Failed to unload assemblies. Please check https://github.com/godotengine/godot/issues/78513 for more information. (User)
  modules/mono/mono_gd/gd_mono.cpp:497 - .NET: Giving up on assembly reloading. Please restart the editor if unloading was failing. (User)

At the same time, all [Tool] scripts are disabled until the editor is restarted.

pkdawson commented 10 months ago

That sounds like it's probably a problem with my code. But I can't reproduce it on Windows 11 or macOS 14, with .NET 7.

Which OS and .NET SDK are you using?

Can you reproduce it with the demo project (included in the .zip with the native binaries)? Open the project, enable the native plugin, restart the editor, then try to change MyTool.cs and rebuild.

c-schembri commented 9 months ago

After some testing I don't believe it's this library's fault that I am running into the above issue. I believe it's other libraries. I could not reproduce the issue using a barebones solution with just this library.

lihaochen910 commented 4 months ago

Guys, I made some changes to get ImGui running in the editor. If you are interested in my modifications, please let me know.

imgui_in_editor
pkdawson commented 4 months ago

@lihaochen910 Sure, I'd love to hear any details, or see the code if you want to share it.

I'm still working on a rewrite of the native GDExtension, which would support an in-editor GUI. I've had very little time in the past couple months, but I should get back to it soon.

lihaochen910 commented 4 months ago

@pkdawson Regarding this feature, I have opened a Pull Request. Tested in Godot version 4.2.1.

pkdawson commented 2 months ago

Implemented in v5.0.0