C++ bindings for the Godot script API
 
 
 
 
 
 
Go to file
Bastiaan Olij 75036c5f05 Fixed up registering tool and updated godot_headers 2018-07-26 21:46:59 +10:00
godot_headers@32aa216bab Fixed up registering tool and updated godot_headers 2018-07-26 21:46:59 +10:00
include/core Fixed up registering tool and updated godot_headers 2018-07-26 21:46:59 +10:00
src/core use typeid() for type tags now 2018-07-25 14:11:40 +02:00
.gitattributes Getting this to compile on mac os x 2017-06-16 23:50:48 +10:00
.gitignore fix gitignore 2018-07-25 14:32:27 +02:00
.gitmodules fix submodule link 2018-06-04 21:56:49 +10:00
LICENSE Initial commit 2017-03-02 23:50:13 +01:00
Makefile included godot_headers as submodule 2018-05-16 02:11:41 +02:00
README.md Fix misleading typo regarding generate_bindings 2018-07-20 23:23:49 +01:00
SConstruct use typeid() for type tags now 2018-07-25 14:11:40 +02:00
binding_generator.py fix indentation in bindings generator 2018-07-25 14:29:17 +02:00
godot_api.json Added godot_api.json from official 3.0.3 release 2018-06-15 23:12:51 +10:00

README.md

godot-cpp

C++ bindings for the Godot script API

Creating a GDNative library (Linux)

Create a directory named SimpleLibrary with subdirectories lib, src

Getting latest godot-cpp and godot_headers

$ git clone https://github.com/GodotNativeTools/godot-cpp
$ git clone https://github.com/GodotNativeTools/godot_headers

right now our directory structure should look like this:

godot-cpp
godot_headers
SimpleLibrary
├── lib/
└── src/

Now to generate cpp bindings

$ cd godot-cpp
$ scons godotbinpath="../godot_fork/bin/godot_binary" p=linux
$ cd ..

resulting libraries will be placed under bin/ and the generated headers will be placed under include/*

Note:

generate_bindings=yes is used to force regenerating C++ bindings (godot_api.json - Godot API)

Include use_llvm=yes for using clang++

You may need to specify headers=../godot_headers if you have compilation issues related to missing include files

And our directory structure will be

godot-cpp
└── bin/libgodot-cpp.a
godot_headers
SimpleLibrary
├── lib/
└── src/

Creating simple class

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

#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");
        }

        Variant method(Variant arg) {
            Variant ret;
            ret = arg;

            return ret;
        }

        static void _register_methods() {
           register_method("method", &SimpleClass::method);
	   
	   /**
	    * How to register exports like gdscript
	    * export var _name = "SimpleClass"
	    **/
	   register_property((char *)"base/name", &SimpleClass::_name, String("SimpleClass"));

           /** For registering signal **/
           // register_signal<SimpleClass>("signal_name");
           // register_signal<SimpleClass>("signal_name", "string_argument", GODOT_VARIANT_TYPE_STRING)
        }
	
	String _name;
};

/** GDNative Initialize **/
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o)
{
    godot::Godot::gdnative_init(o);
}

/** GDNative Terminate **/
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o)
{
    godot::Godot::gdnative_terminate(o);
}

/** NativeScript Initialize **/
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
{
    godot::Godot::nativescript_init(handle);

    godot::register_class<SimpleClass>();
}

Compiling

$ cd SimpleLibrary
$ clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14 -I../godot-cpp/include -Igodot_headers
$ clang -o lib/libtest.so -shared src/init.os -L../godot-cpp/lib -lgodot-cpp

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.

Creating .gdns file

follow godot_header/README.md to create the .gdns

Implementing with gdscript

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