GDNative Tutorial

20 May 2017

With Godot 3.X, Developers (Mainly karroffel) are working on a concept of using shared libraries as scripts which named GDNative (Godot Native).

What is GDNative and why you need to know about it

It is a module for Godot that adds a new “scripting language” (Not exactly).

A “script” in Godot is defined as something that has methods, properties and signals and also offer a way to use calling methods, get and set properties… But Instead of having a text file representing these things (like a GDScript file for example), GDNative uses shared libraries, which can be attached to nodes via Native “scripts”.

In some ways, shared libraries have things in common with scripts (GDScript).

 you can load them
 you can unload them
 you can load function symbols
 you can call functions

A method in a Native script (Library) is just another native machine code. You can call third party libraries as well as you can call GDScript functions.

How to create a GDNative (Script/Library) ?

Creating a GDNative library (Linux)

Create a director named SimpleLibrary with subdirectories lib and src

- SimpleLibrary
  - lib
  - src

Getting latest CPP_bindings and godot_headers

$ cd SimpleLibrary
$ git clone https://github.com/GodotNativeTools/cpp_bindings.git
$ git clone https://github.com/GodotNativeTools/godot_headers.git

right now our file structure should look like this

Generate cpp bindings

Edit SConstruct under SimpleLibrary/cpp_bindings and assign your godot executable path

 Linux line:7 godot_bin_path = "../godot_fork/bin/"**,
 Windows line:10** godot_lib_path <b>= "../godot_fork/bin/"**

now to build core/bindings, .so for linux and .lib for windows (I Guess)

Linux:

> $ scons p=linux
> $ scons p=linux target=bindings generate_bindings=yes

Windows:

> $ scons p=linux
> $ scons p=linux target=bindings generate_bindings=yes

resulting libraries will be placed under** bin/ and generated bindings will be found under **includes/ Note: Use use_llvm=yes to use clang.

Copy Core && Bindings libraries into SimpleLibrary/lib folder and our file structure should be

So far we created our two foundational libraries, now to create our actual script/shared-library

Create init.cpp under SimpleLibrary/src/ and add the following code

To compiling the cpp file use the following clang command line(Linux).

> $ clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14  -Icpp_bindings/include -Igodot_headers -Icpp_bindings/include/core
> $ clang -o lib/libtest.so -shared src/init.os -Llib -lgodot_cpp_core -lgodot_cpp_bindings

This creates the file libtest.so in your SimpleLibrary/lib directory. For windows you need to find out what compiler flags need to be used. As I never used windows I know * about WIN.

Creating .gdn (GDNativeScript) file

Make sure to place your libraries in the same directory

Now to create a GDNativeScript (.gdn) file, Manually create a GDNativeScript resource

And now create a GDNativeLibrary and assign your libraries.

You must specify the name of the class you would like to use under Script Name in GDNativeScript inspector

You can save GDNativeLibrary as .tres also so you can use it again with other script or you can set a default GDNativeLibrary in the project settings

Implementing our library with gdscript

> var simpleclass = load("res://simpleclass.gdn").new();
> simpleclass.method("Test argument");

Other bindings created by the developers ,

Rust: https://github.com/GodotNativeTools/rust_bindings
D Lang: https://github.com/GodotNativeTools/d_bindings

The post will be updated as the development of GDNative and Godot3.X progress, so subscribe.