godot icon indicating copy to clipboard operation
godot copied to clipboard

LibGodot with GDExtension

Open Faolan-Rad opened this issue 2 years ago • 20 comments

LibGodot is a system to allows Godot to be compiled as a library and connected into it using GDExtensions by giving a function pointer to the entry point of a GDExtension implementation before starting Godot.

LibGodot adds language support for languages that have C support but can not becompiled into a shared library.

LibGodot allows the use of different C# runtimes allowing dotnetAOT compilation, C# android support, and C# web support.

LibGodot also allows Godot to be embedded in other applications like Blender.

Faolan-Rad avatar Feb 08 '23 10:02 Faolan-Rad

Related to #68153 - this provides a means of loading the Godot Engine from a standard .NET 6 android project

lyuma avatar Feb 08 '23 11:02 lyuma

Is this slated for 4.1 or 4.0?

Zireael07 avatar Feb 08 '23 13:02 Zireael07

@Zireael07 You can see the milestone :) Nothing of the sort is going into 4.0 at this point. We're done with features.

YuriSizov avatar Feb 08 '23 13:02 YuriSizov

Ah. Right. Mobile GitHub and my poor eyesight are to blame.

Zireael07 avatar Feb 08 '23 13:02 Zireael07

Does this address https://github.com/godotengine/godot-proposals/issues/4773?

Calinou avatar Feb 08 '23 13:02 Calinou

It does for the most part the thing it doesn't do is allow someone using the Library to manually tell Godot to update

Faolan-Rad avatar Feb 08 '23 13:02 Faolan-Rad

We discussed out-of-band how that @Faolan-Rad should tell us the story of his need for this feature and how this can benefit the wider community of C#, other languages and XR.

TL;DR: pending a new proposal.

fire avatar Feb 10 '23 22:02 fire

The update problem was avoided by having the libgodot user make a gdextension and have it block in the gdextension class like the "_process" method.

Benefits

  1. Avoids stepping the MainLoop.
  2. Avoids handling synchronization when two different functions are called externally.

fire avatar Feb 12 '23 05:02 fire

Regarding C#:

No work has started yet for porting to mobile, so I can't say what will be needed. If we can't find a better solution, we would have to go with a C# project as entry-point and Godot as a shared library. However, there's actually hope that .NET 8 or 9 could allow publishing mobile projects as libraries, which would simplify things for us.

neikeq avatar Feb 12 '23 14:02 neikeq

I have a basic test project that I have put together in C# LibGodotSharp image

Faolan-Rad avatar Feb 13 '23 13:02 Faolan-Rad

I'd really like to voice support for this PR. It enables Godot to be embedded in many more scenarios, and I think would be awesome feature. I also have needs for this and was excited to see this PR in process.

In my case I have a custom application that could not be embedded 'into' Godot as it uses custom build systems, tools, languages, etc - with this approach I could import Godot as a dependency, and continue to use Godot C++ APIs for other functionality. I'm an example of a user that Godot would gain if this type of embedding were possible.

Only minor tweak I would recommend is that the library user 'could' have the option to own the main loop, as delegating the main loop completely is not always ideal. But even if this isn't possible I'd let the PR through and folks like myself could make minor source patches to have more main loop control.

stucotso avatar Feb 17 '23 03:02 stucotso

@Faolan-Rad For Android, Godot is already built and used as a shared library. Would this PR enables that capability for other platforms as well?

m4gr3d avatar Feb 19 '23 19:02 m4gr3d

In LibGodotSharp I have set up android support

Faolan-Rad avatar Feb 20 '23 03:02 Faolan-Rad

FYI

Is Making Advanced GUI Applications with Godot the Future?

Now https://github.com/RhubarbVR/LibGodotSharp works in Desktop (Win) and Android.

GeorgeS2019 avatar Feb 25 '23 13:02 GeorgeS2019

First stable pre releases of LibGodotSharp witch uses this system is out now.

Faolan-Rad avatar Feb 28 '23 12:02 Faolan-Rad

I had to fixed bugs with loading orders and API registering levels with GDextensions that made it where the project settings could not be changed before they are used I have fixed it on my master branch but not in this pull request because it was a problem with GDextensions.

Faolan-Rad avatar Feb 28 '23 12:02 Faolan-Rad

@Faolan-Rad I grabbed your master branch and tried to build. All worked well for static lib. Any chance you can write a doc or example on how to use the static lib from C++ ? I did see the C# example - but I wasn't able to easily map all the logic to C++ to try to get something to work. If you provide info I can help to write an example to show how this works as well. It might help folks understand value of this cool feature.

sferrogoo avatar Mar 07 '23 01:03 sferrogoo

I was puzzling over this.

Here's is the closest api doc. https://github.com/RhubarbVR/LibGodotSharp/blob/main/libgodotsharp/Native.cs

fire avatar Mar 07 '23 07:03 fire

  • [ ] Generate bindings from json
  • [ ] load dll
  • [ ] bind functions to external language
  • [ ] create a new SceneTree
  • [ ] LoadScene(SceneTree scene)
  • [ ] add a MeshInstance3D node to the scene
  • [ ] get the MeshInstance3D node's name

fire avatar Mar 07 '23 07:03 fire

Seems it could be possible to just write c++ as standard Godot module code, not use GDExtension directly. Would be better performance and one could static link for fast compile as well. But I’m new to Godot so maybe expert can comment?

Thanks for this amazing pr! Can’t wait to see it happen!

stucotso avatar Mar 07 '23 11:03 stucotso

@stucotso I used GDExtension for better interrogations in to other languages and if you tried export all functions to a shared library you would hit the linker limits on windows.

Faolan-Rad avatar Mar 23 '23 22:03 Faolan-Rad

Very interested in this pr! I was wondering if it enables us to render to a custom texture or OpenGL context / similar? My use case is to be able to embed the godot game view in the UI of other apps with different UI Frameworks, like Flutter.

Airyzz avatar Mar 24 '23 05:03 Airyzz

@Airyzz Here is TodoList

GeorgeS2019 avatar Mar 28 '23 00:03 GeorgeS2019

Generate bindings from json

//https://github.com/RhubarbVR/LibGodotSharp/blob/main/sharp_generator/Program.cs
[DllImport("libgodot")]
public static extern int godot_main(int argc, string[] args);

 var custom_args = new string[] { "libgodot", "--dump-extension-api", "--verbose", "--headless", "" };
if (godot_main(custom_args.Length - 1, custom_args) != 0)
 {
    throw new Exception("Godot had error");
 }

//output: extension_api.json


//https://github.com/RhubarbVR/LibGodotSharp/blob/main/sharp_generator/GDapi.cs
//var api = JsonSerializer.Deserialize<Api>(file);

var api = Api.Create(pathToGenJson);



var convert = new Convert(api, ginDir, docs, configName);
convert. Emit();

//https://github.com/RhubarbVR/LibGodotSharp/blob/main/sharp_generator/Convert.cs
    public Convert(Api api, string dir, string docDir, string configName)
    {
        this.api = api;
        this.classXml = new XmlSerializer(typeof(Documentation.Class));
        this.builtinXml = new XmlSerializer(typeof(Documentation.BuiltinClass));
        this.dir = dir;
        this.docDir = docDir;
        this.configName = configName;
    }

    public void Emit()
    {
    }

GeorgeS2019 avatar Mar 28 '23 00:03 GeorgeS2019

This is an amazing patch, and would allow many of us that wanted to use Godot as a library to do so, a great feature for those of us that mostly do applications, but want to sprinkle a little Godot here and there.

migueldeicaza avatar Mar 31 '23 02:03 migueldeicaza

@migueldeicaza

We are WIP to use XAML parser to automate 3D UI using this library. Keep coming back to support this IDEA!!! we need you to push this library for 3D control in MAUI

FYI: Everything YOU wish for urhosharp, can NOW be done through LibGodot!!! After xxx years of waiting. :-)

GeorgeS2019 avatar Mar 31 '23 02:03 GeorgeS2019

@Faolan-Rad do you remember why in your fork https://github.com/RhubarbVR/godot-lib/actions/runs/4277032243/jobs/7445556782 is failing because of the use of the new API levels server and scene?

I took it out of my build to attempt to get tests to pass.

fire avatar Jul 05 '23 02:07 fire

I have updated this to align with the latest 'main' branch. Additionally, I've incorporated a C# entry point. The bug fixes related to project settings loading have been extracted and will be presented in a separate pull request at some point.

Faolan-Rad avatar Aug 23 '23 08:08 Faolan-Rad

@dsnopek I do like your suggestion I would like to do it with the a GDExtensionBase and then there would be the normal GDExtension and EmbeddedGDExtension, and the manager itreacts with the base class I would also like to make it where it is not just binding one EmbeddedGDExtension you can have multiple. I would also like to make it assigned able from other GDExtension this would be more for having a GDExtension for lua implementation and then registering the lua code as a GDExtension

Currently, I am prioritizing something else for my game specifically, working on the _mix method for AudioStreamPlayback in C# PR so I can efficiently implement opus for my game. I do want to come back to this soon but it might be a second.

Faolan-Rad avatar Aug 30 '23 07:08 Faolan-Rad

Or, some other way that all the changes for this embedded mode would live separately from the code used when running Godot normally...

Another possible idea: What if instead of GDExtension directly using OS::open_dynamic_library() and OS::get_dynamic_symbol_handle(), it used a new layer (ex GDExtensionLoader?) that could have its implementation replaced when in "LibGodot mode" (maybe via a GDExtensionLoaderEmbedded child class)? This could be used to prevent really loading dynamic libraries, and instead look up the entry point function pointers from a list. I think this would require less code changes than making a GDExtensionEmbedded child class, and it should still be easy to #ifndef away the LibGodot version of the "GDExtension loader layer" when not needed.

dsnopek avatar Aug 30 '23 10:08 dsnopek