**Disclaimer:** This website is under constant modification. If you find something isn't right then, I might be doing some change in the back-end.

GDNative Tutorial - Part-1

If you dont know what GDNative is I suggest you read this article at GodotEngine (Written by karroffel),

How to create a simple GDNative Script/Library?

First lets see how we can do a simple scrip in C++, and later we’ll lookinto creating a basic character controller.

Lets get started by creating a director named SimpleLibrary with subdirectories lib and src which should look more like

  • SimpleLibrary
    • lib
    • src

Create init.cpp

#include <core/Godot.hpp>
#include <Reference.hpp>

using namespace godot;

class SimpleClass : public GodotScript<Reference> {
        GODOT_CLASS(SimpleClass);
public:
        SimpleClass() { }

        void test_void_method() {
                Godot::print("This is test");
        }

        String method(Variant arg) {
            String ret;
            ret = "Recived from gdnative: " + arg.operator String();

            Godot::print(ret); // print `ret` to output tab

            return ret;
        }

        static void _register_methods() {

           /** Registering methods `method` and `test_void_method` **/
           register_method((char *)"method", &SimpleClass::method);
           register_method((char *)"test_void_method", &SimpleClass::test_void_method);
	   
           /** Registering property `base => name` **/
	   // register_property((char *)"base/name", &SimpleClass::_name, String("SimpleClass"));

           /** registering signal `signal_name` **/
           // register_signal<SimpleClass>("signal_name");
        }
	
	String _name;
};

/** GDNative Initialize **/
GDNATIVE_INIT(godot_gdnative_init_options *options) {

}

/** GDNative Terminate **/
GDNATIVE_TERMINATE(godot_gdnative_terminate_options *options) {

}

/** NativeScript Initialize **/
NATIVESCRIPT_INIT() {
        register_class<SimpleClass>();
}

To compiling the C++

On Linux

clang -g -fPIC -std=c++14 -03 src/init.cpp -I/path/to/godot/headers/ -I/path/to/godot-cpp/ -o src/init.os
clang -g -shared src/init.os -o lib/libinit.so -L/path/to/godot-cpp/bin -lgodot_cpp_bindings

Note

  • -g is for debugging information.
  • Use godot_nativescript_* methods only in the nativescript_init() function.

Now to load the libinit.so file in godot, we have to create a new .res or .tres GDNative library resource file

Create new GdNativeLibrary resource in godot editor and load our library in the Inspector

Don’t forget to save the resource


Now to load the library file in GDScript

extends Node

func _ready():
    var gd_lib = load("new_gdnativelibrary.tres")

    var ns = NativeScript.new()
    ns.class_name = "SimpleClass"
    ns.library = gd_lib

    var simple_class = ns.new()
    simple_class.method("Message from gdnative")

Will print Recived from gdnative: Message from gdnative

Attaching GDNativeLibrary to a Node

  • Attach a new script to a node
  • In the pop-up dialog, choose NativeScript in the Language menu
  • Enable built-in script, or create a .gdn file, which only contains a name
  • Specify the Class Name (e.g, SimpleClass)
  • Press Create

Where to Go From Here?

In the next part, We’ll implement a basic Character Controller in GDNative and how we can use Properties and Signals

© FrogSquare