C++ bindings for the Godot script API
 
 
 
 
 
 
Go to file
Thomas Herzog e4fb5ca2a5
Merge pull request #277 from RameshRavone/patch-2
Fix #272
2019-04-22 09:10:31 +02:00
godot_headers@489db2761c Synced with Godot 3.1 stable 2019-04-07 17:59:53 +10:00
include Fix #272 2019-04-21 06:55:49 +05:30
misc/hooks Implementing clang-format and applying it to all classes 2018-11-24 09:09:41 +11:00
src Fix #272 2019-04-21 06:55:49 +05:30
.clang-format Implementing clang-format and applying it to all classes 2018-11-24 09:09:41 +11:00
.gitattributes Getting this to compile on mac os x 2017-06-16 23:50:48 +10:00
.gitignore Nativescript 1.1 2018-11-07 21:23:08 +11:00
.gitmodules fix submodule link 2018-06-04 21:56:49 +10:00
CMakeLists.txt Update CMakeLists.txt 2019-04-12 23:29:56 +10:00
LICENSE.md Synced with Godot 3.1 stable 2019-04-07 17:59:53 +10:00
Makefile Fix Makefile platform and bits arguments passing to scons 2019-03-10 12:38:55 +01:00
README.md Add instructions to use submodules 2019-03-13 15:56:53 +01:00
SConstruct Merge pull request #260 from aqnuep/basis_convention 2019-04-10 21:57:03 +10:00
binding_generator.py Optimizations to the generated bindings 2019-04-10 16:51:51 +02:00

README.md

godot-cpp

C++ bindings for the Godot script API

The instructions below feature the new NativeScript 1.1 class structure and will only work for modules created for Godot 3.1 and later. Use the following branches for older implementations:

Version Branch
Godot 3.0 Nativescript 1.0 3.0
Godot 3.1 Nativescript 1.0 nativescript-1.0

Index:

Contributing

We greatly appreciate help in maintaining and extending this project. If you wish to help out ensure you have an account on Github and create a "fork" of the this repo. Rémi "Akien" Verschelde wrote an excellent bit of documentation for the main Godot project on this: https://docs.godotengine.org/en/3.0/community/contributing/pr_workflow.html

It is advisible to also install clang-format and copy the files in misc/hooks into .git/hooks so format checking is done before your changes are submitted.

Getting Started

Build latest version of Godot GitHub Docs

Setting up a new project

We recommend using git for managing your project and the instructions below assume so. Alternatively you can download the source code directly from GitHub in which case you need to download both godot-cpp and godot_headers.

$ mkdir SimpleLibrary
$ cd SimpleLibrary
$ mkdir bin
$ mkdir src
$ git clone --recursive https://github.com/GodotNativeTools/godot-cpp

Note that if you wish to use a specific branch, add the -b option to the clone command:

$ git clone --recursive https://github.com/GodotNativeTools/godot-cpp -b 3.0

If your project is an existing repository, use git submodule instead:

$ git submodule add https://github.com/GodotNativeTools/godot-cpp
$ git submodule update --init --recursive

Right now our directory structure should look like this:

SimpleLibrary/
├─godot-cpp/
| └─godot_headers/
├─bin/
└─src/

Updating the api.json

Our api.json file contains meta data of all the classes that are part of the Godot core and are needed to generate the C++ binding classes for use in GDNative modules.

This file is supplied with our godot_headers repository for your convinience but if you are running a custom build of Godot and need access to classes that have recent changes a new api.json file must be generated. You do this by starting your Godot executable with the following parameters:

$ godot --gdnative-generate-json-api api.json

Now copy the api.json file into your folder structure so its easy to access. Note the remark below for the extra custom_api_file command line parameter needed to tell scons where to find your file.

Compiling the cpp bindings library

The final step is to compile our cpp bindings library:

$ cd godot-cpp
$ scons platform=<your platform> generate_bindings=yes
$ cd ..

Replace <your platform> with either windows, linux or osx.

Include use_llvm=yes for using clang++

Include target=runtime to build a runtime build (windows only at the moment)

The resulting library will be created in godot-cpp/bin/, take note of its name as it will be different depending on platform.

If you want to use an alternative api.json file add use_custom_api_file=yes custom_api_file=../api.json, be sure to specify the correct location of where you placed your file.

Creating a simple class

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

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

using namespace godot;

class SimpleClass : public Reference {
    GODOT_CLASS(SimpleClass, Reference);
public:
    SimpleClass() { }

    /* _init must exist as it is called by Godot */
    void _init() { }

    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<SimpleClass, String>("base/name", &SimpleClass::_name, String("SimpleClass"));

        /* or alternatively with getter and setter methods */
        register_property<SimpleClass, int>("base/value", &SimpleClass::set_value, &SimpleClass::get_value, 0);

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

    void set_value(int p_value) {
        _value = p_value;
    }

    int get_value() const {
        return _value;
    }
};

/** 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

Linux

$ cd SimpleLibrary
$ clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14 -Igodot-cpp/include -Igodot-cpp/include/core -Igodot-cpp/include/gen -Igodot-cpp/godot_headers
$ clang -o bin/libtest.so -shared src/init.os -Lgodot-cpp/bin -l<name of the godot-cpp>

This creates the file libtest.so in your SimpleLibrary/bin directory.

You will need to replace <name of the godot-cpp> with the file that was created in Compiling the cpp bindings library

Windows

$ cd SimpleLibrary
$ cl /Fosrc/init.obj /c src/init.cpp /nologo -EHsc -DNDEBUG /MDd /Igodot-cpp\include /Igodot-cpp\include\core /Igodot-cpp\include\gen /Igodot-cpp\godot_headers
$ link /nologo /dll /out:bin\libtest.dll /implib:bin\libsimple.lib src\init.obj godot-cpp\bin\<name of the godot-cpp>

This creates the file libtest.dll in your SimpleLibrary/bin directory.

You will need to replace <name of the godot-cpp> with the file that was created in Compiling the cpp bindings library

Finally replace /MDd with /MD if you're generated a runtime build.

macOS For OSX you need to find out what compiler flags need to be used.

Creating .gdnlib and .gdns files

follow godot_header/README.md to create the .gdns

Implementing with gdscript

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