Integrating c++ modules

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hungrymonkey
:warning: Old Version Published before Godot 3 was released.

Hello fellow programmers,

I am new to Godot. I am trying to integrate a c++ library that is lgplv3.

I am looking at the new developments to Godot such as GDNative. I am not really sure what to use.

I am just trying to figure out how to write a plugin so I can integrate callback events. I want to be able to rebuild the library as it uses cmake and i want to do modifications to it.

Thank You
Noobie Godot User

You mostly need C++ knowledge here.

It’s possible to integrate a library as a module (statically compiled with the engine then) if you are able to understand how it gets built with CMake (what it links, what it includes, which defines are needed), because it needs to be integrated with the Godot build system (SCons). I’m not aware of any module in Godot that depends on CMake, since I’m able to build the engine without it, so they might have been adapted a little.

GDNative sounds more flexible because it uses dynamic libraries that you can build the way you want, however it’s more recent and a bit more unstable so there are bugs and docs might be lacking. You can ask for help on Discord though, there is a channel dedicated to GDNative and people already managed to bind entire language libraries like Python or D.

Zylann | 2017-10-05 17:47

I got things working by nuking cmake from the library depends. I can build from pure scons. I am trying to figure out how to use signal or other events to stream data right now.

Thanks

hungrymonkey | 2017-10-10 21:32

:bust_in_silhouette: Reply From: hungrymonkey

If you want to create call back events from within godot.

You essentially take advantage that GDscript signals implements the observer pattern

To implement signal you basically add it _bind_methods

 //something like this
 ADD_SIGNAL(MethodInfo("get_pcm", PropertyInfo(Variant::POOL_BYTE_ARRAY, "audio_frame")));

}

If you wish to implement the module on a single thread so that it is basically a godot server.

You must write all of this boilerplate

If you want the server to be visible to GDScript, then you must create a seperate dummy class and register it to the engine. The dummy class will access resources via the get_singleton()->method?() interface

here is the _OS example

here is the example on how to bind the class

the pseudo code is something like this.

 _mymodules * mymodules_instance = nullptr
 register_mymodules_types(){
    mymodules_instance = memnew(_mymodules);
    ClassDB::register_class<_mymodules>();
    Engine::get_singleton()->add_singleton(Engine::Singleton("mymodules", _mymodules::get_singleton()));
 }