imgui-godot icon indicating copy to clipboard operation
imgui-godot copied to clipboard

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

Open c-schembri opened this issue 2 years ago • 19 comments

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!");
    }
}

c-schembri avatar Apr 12 '23 03:04 c-schembri

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 avatar Apr 12 '23 12:04 pkdawson

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 avatar May 25 '23 23:05 pkdawson

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 avatar May 26 '23 01:05 pkdawson

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.

pkdawson avatar May 28 '23 22:05 pkdawson

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!

c-schembri avatar Jun 29 '23 04:06 c-schembri

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#.

pkdawson avatar Jun 29 '23 19:06 pkdawson

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.

c-schembri avatar Jul 01 '23 13:07 c-schembri

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

pkdawson avatar Aug 28 '23 03:08 pkdawson

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.

c-schembri avatar Aug 28 '23 03:08 c-schembri

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

pkdawson avatar Aug 28 '23 19:08 pkdawson

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?

c-schembri avatar Sep 06 '23 07:09 c-schembri

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.

pkdawson avatar Sep 06 '23 14:09 pkdawson

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 avatar Sep 07 '23 07:09 c-schembri

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.

c-schembri avatar Sep 07 '23 07:09 c-schembri

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.

pkdawson avatar Sep 07 '23 16:09 pkdawson

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.

c-schembri avatar Sep 24 '23 13:09 c-schembri

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

lihaochen910 avatar Mar 04 '24 08:03 lihaochen910

@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.

pkdawson avatar Mar 04 '24 12:03 pkdawson

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

lihaochen910 avatar Mar 05 '24 02:03 lihaochen910

Implemented in v5.0.0

pkdawson avatar May 03 '24 21:05 pkdawson