High-level questions about using GDNative for porting libraries

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By crotron

I want to use GDNative to allow me to use a C++ linear algebra library from within Godot. I’m new to GDNative and fairly rusty with C++, so I’d like to ask some high-level questions before I start digging deeply into it so I know what I’m getting myself into.

If the library has some dependencies, I’m assuming I will need to worry about those. If I’m targeting multiple platforms but the dependencies don’t support all of them, then I assume that I’ll only be able to use the library on the platforms that support it? What if the library is entirely in C++ with no external dependencies other than things like STL? Are there any other issues I can expect to have to deal with when using GDNative for multiple platforms?

I will probably need to sometimes call functions on objects from within Godot (i.e. multiplying two matrices). I’m guessing that the most efficient way to do multiple operations like this would be to copy only the C++ pointers over to Godot as integers and back in order to avoid unnecessarily sending over all of the object data? It seems like there are some nuances to doing this even for a pure C++ application (https://stackoverflow.com/questions/153065/converting-a-pointer-into-an-integer). Is it possible to do this within Godot?

:bust_in_silhouette: Reply From: Zylann

GDNative is not different than just developing your own library, so all things you know about this development cycle will apply (dependencies, supported platforms etc). The fact Godot is able to load libraries with a GDNative API isn’t magically changing anything.

The main things to know about is how GDNative lets you expose your functionalities to Godot. You can register classes as if they were scripts (a script = a class), and you can register methods on them.
You also have to register methods using types that are exclusively available within Godot. So for example, you won’t be able to directly bind a function receiving a std::string, because Godot doesn’t use STL, so neither GDScript. So just like Godot does internally with its own third party libraries, you will need to create “wrapper objects” which are Godot-friendly, and then you access your library stuff in their private implementation.

Binding a linear algebra library is possible, however keep in mind you can only expose classes (aka reference types that are created using new in GDScript).
Also, due to what I explained above, you will have to map Godot types to it. So if your library expects a 3-component vector as a glmVector3 for example, you will need a wrapper method that accepts a Vector3 from Godot and convert it to a glmVector3 in your C++ code. You could also bind the said type as a full-fledged class but it would be awkward to do it given the fact Godot already has such a type.

You should not need to convert pointers to integer, it’s not good to expose that to the script API. To call your functions or to call Godot functions, there is a bunch of void* being passed, if you use GDNative C API directly. However, using a binding library (like the C++ one made by Karroffel) you should not have to worry about that.