lua-gdextension icon indicating copy to clipboard operation
lua-gdextension copied to clipboard

C# Support / Documentation

Open JazzGlobal opened this issue 9 months ago • 9 comments

Having trouble understanding how to get started with this in C#, or if it is even supported at this point in time.

JazzGlobal avatar Mar 11 '25 17:03 JazzGlobal

Hey @JazzGlobal, thanks for asking. I'm not a user of C# in Godot, but I took a look and it seems there's no right way to using GDExtension classes in C# yet.

https://github.com/godotengine/godot-proposals/issues/8191 contains a very good discussion in this topic. From what I see there, we have some alternatives, such as:

  • Using the GDExtension classes dynamically with ClassDB + GodotObject (dynamic calls by method name, dynamically getting and setting properties by name)
  • Creating a NuGet package with bindings and asking people to install it
  • Adding an Editor Plugin that generates bindings

I'm not sure what would be a good solution for binding Lua GDExtension classes to C#, I'm open to suggestions if you have any.

gilzoide avatar Mar 11 '25 22:03 gilzoide

By the way, I just tried using Delsin-Yu/CSharp-Wrapper-Generator-for-GDExtension and it almost works. The classes are generated nicely, but the methods are not quite correct (it generates setters for getter-only properties, it uses void on methods that return Variant and it doesn't add default parameters to methods).

gilzoide avatar Mar 12 '25 12:03 gilzoide

I've started writing out an API for my game and exposing it to Lua via NLua. It seems to be working fine for my use case. Thanks!

JazzGlobal avatar Mar 12 '25 15:03 JazzGlobal

Oh, great to hear that! NLua won't have the Godot-specific bindings that Lua GDExtension bring (at least not without you manually binding it), but if your project doesn't need that, it's certainly the easiest way to use Lua in Godot + C# at the moment =] Good call!

gilzoide avatar Mar 13 '25 00:03 gilzoide

Oh, great to hear that! NLua won't have the Godot-specific bindings that Lua GDExtension bring (at least not without you manually binding it), but if your project doesn't need that, it's certainly the easiest way to use Lua in Godot + C# at the moment =] Good call!

Yeah, it's going to be a lot more work than just using your plugin with GDScript. Unfortunately, most of my existing libraries that I've written are in C# and I do not want to port them to GDScript if I'm being honest.

What I ultimate want is to let non-developers on my team, and potentially modders in the future, to script behaviors such as AI / boss battles. So I've just building out which Godot functions will be needed. Even just providing wrappers for Vector2 is a good amount of effort so it might turn into something bigger. Thank you again!

So in short, I don't need all Godot bindings but I'll need at least some of them.

JazzGlobal avatar Mar 13 '25 03:03 JazzGlobal

What I ultimate want is to let non-developers on my team, and potentially modders in the future, to script behaviors such as AI / boss battles.

Nice, I also think Lua is a really good tool for this job.

Even just providing wrappers for Vector2 is a good amount of effort so it might turn into something bigger.

I feel you, it's easy enough binding Vector2 if you have a nice binding library such as Sol2 or NLua, but as the necessity to bind more and more stuff arises it gets cumbersome quite quickly. That's why I decided to bind Variant in Lua GDExtension, it's much easier than binding each variant type separately as I did in Lua PluginScript for Godot 3.


@JazzGlobal Sooo, I got a little obsessed with this "binding C# to GDExtension classes" thing and made an Editor Plugin in GDScript that generates C# wrappers for GDExtension classes. The experiment is in my newest repo gilzoide/godot-csharp-gdextension-bindgen. I only tested it with Lua GDExtension, but as far as my test went, it works quite well =] It fixes some problems of "CSharp-Wrapper-Generator-for-GDExtension" like the support for vararg methods, methods that return Variant and default argument values.

Some things are missing, like binding signals, but Lua GDExtension classes don't declare any, so that's ok for now. If you want to give it a try, you can just copy the C# scripts that I already generated in the repo GDExtensionBindgen folder, or install the addon manually and try the binding generation process yourself. Let me know if you use it and if it works nicely for your case ^^

gilzoide avatar Mar 13 '25 15:03 gilzoide

What I ultimate want is to let non-developers on my team, and potentially modders in the future, to script behaviors such as AI / boss battles.

Nice, I also think Lua is a really good tool for this job.

Even just providing wrappers for Vector2 is a good amount of effort so it might turn into something bigger.

I feel you, it's easy enough binding Vector2 if you have a nice binding library such as Sol2 or NLua, but as the necessity to bind more and more stuff arises it gets cumbersome quite quickly. That's why I decided to bind Variant in Lua GDExtension, it's much easier than binding each variant type separately as I did in Lua PluginScript for Godot 3.

@JazzGlobal Sooo, I got a little obsessed with this "binding C# to GDExtension classes" thing and made an Editor Plugin in GDScript that generates C# wrappers for GDExtension classes. The experiment is in my newest repo gilzoide/godot-csharp-gdextension-bindgen. I only tested it with Lua GDExtension, but as far as my test went, it works quite well =] It fixes some problems of "CSharp-Wrapper-Generator-for-GDExtension" like the support for vararg methods, methods that return Variant and default argument values.

Some things are missing, like binding signals, but Lua GDExtension classes don't declare any, so that's ok for now. If you want to give it a try, you can just copy the C# scripts that I already generated in the repo GDExtensionBindgen folder, or install the addon manually and try the binding generation process yourself. Let me know if you use it and if it works nicely for your case ^^

Oh wow. This is working really well for my use case. Thanks a bunch man! Now I don't have to write a bunch of bindings for specific Godot stuff! You're awesome!

JazzGlobal avatar Mar 13 '25 19:03 JazzGlobal

Oh wow. This is working really well for my use case. Thanks a bunch man! Now I don't have to write a bunch of bindings for specific Godot stuff! You're awesome!

No problem, glad it's working for you! Btw, feel free to sponsor this work in GitHub sponsors or Ko-fi ^^ (no pressure though, just saying)

I'll ask you to keep this issue open while we don't make C# bindings available to this addon directly, or at least easily installable and properly documented.

Cheers \o/

gilzoide avatar Mar 14 '25 21:03 gilzoide

Hi all, the easiest way to support c# bindings rn in Godot is through modules. Addon's usually have this manually generated layer.

Ughuuu avatar Jun 16 '25 09:06 Ughuuu