2017-03-02 22:50:27 +00:00
# cpp_bindings
C++ bindings for the Godot script API
2017-05-18 02:14:18 +00:00
# Creating a GDNative library (Linux)
2017-08-04 21:41:25 +00:00
Create a directory named `SimpleLibrary` with subdirectories `lib, src`
2017-05-18 02:14:18 +00:00
Getting latest `cpp_bindings` and `godot_headers`
```
$ cd SimpleLibrary
$ git clone https://github.com/GodotNativeTools/cpp_bindings
$ git clone https://github.com/GodotNativeTools/godot_headers
```
2017-08-04 21:41:25 +00:00
right now our directory structure should look like this
2017-05-18 02:14:18 +00:00
```
[SimpleLibrary]
├── cpp_bindings/
├── godot_headers/
├── lib/
└── src/
```
Now to generate cpp bindings
```
$ cd cpp_bindings
```
Edit `SConstruct` file and assign your godot executable path at line:7 `godot_bin_path = "../godot_fork/bin/"` ,
2017-07-25 09:54:55 +00:00
Building cpp_bindings
2017-05-18 02:14:18 +00:00
```
2017-12-02 05:27:08 +00:00
$ scons godotbinpath="< GODOT-FORK > /bin/" headers="< GODOT-FORK > /modules/gdnative/include/" p=linux generate_bindings=yes
2017-05-18 02:14:18 +00:00
```
2017-07-25 09:54:55 +00:00
resulting libraries will be placed under `bin/` and the generated headers will be placed under `include/*`
2017-05-18 02:14:18 +00:00
2017-10-03 15:48:47 +00:00
**Note:**
> `generate_bindings=yes` is used to generate C++ bindings (`godot_api.json` - Godot API)
> Include `use_llvm=yes` for using clang++
2017-05-18 02:14:18 +00:00
2017-08-04 21:41:25 +00:00
Copy binding libraries into the `SimpleLibrary/lib` folder
2017-05-18 02:14:18 +00:00
```
$ cd ..
2017-07-25 09:54:55 +00:00
$ cp cpp_bindings/bin/libgodot_cpp_bindings.a lib/
2017-05-18 02:14:18 +00:00
```
2017-08-04 21:41:25 +00:00
And our directory structure will be
2017-05-18 02:14:18 +00:00
```
[SimpleLibrary]
├── cpp_bindings/
├── godot_headers/
├── lib/
2017-07-25 09:54:55 +00:00
│ └──libgodot_cpp_bindings.a
2017-05-18 02:14:18 +00:00
└── src/
```
# Creating simple class
Create `init.cpp` under `SimpleLibrary/src/` and add the following code
2017-07-25 09:54:55 +00:00
```
2017-05-18 02:14:18 +00:00
#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);
2017-07-25 09:54:55 +00:00
/**
* How to register exports like gdscript
* export var _name = "SimpleClass"
** /
register_property((char *)"base/name", & SimpleClass::_name, String("SimpleClass"));
2017-10-03 15:48:47 +00:00
/** For registering signal ** /
// register_signal< SimpleClass > ("signal_name");
2017-05-18 02:14:18 +00:00
}
2017-07-25 09:54:55 +00:00
String _name;
2017-05-18 02:14:18 +00:00
};
2017-07-25 09:54:55 +00:00
/** GDNative Initialize ** /
GDNATIVE_INIT(godot_gdnative_init_options *options) {
}
/** GDNative Terminate ** /
GDNATIVE_TERMINATE(godot_gdnative_terminate_options *options) {
}
/** NativeScript Initialize ** /
NATIVESCRIPT_INIT() {
2017-05-18 02:14:18 +00:00
register_class< SimpleClass > ();
}
```
# Compiling
```
$ cd ..
2017-10-03 15:48:47 +00:00
$ clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14 -Icpp_bindings/include -Igodot_headers
2017-07-25 09:54:55 +00:00
$ clang -o lib/libtest.so -shared src/init.os -Llib -lgodot_cpp_bindings
2017-05-18 02:14:18 +00:00
```
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.
2017-07-25 09:54:55 +00:00
# Creating `.gdns` file
follow [godot_header/README.md ](https://github.com/GodotNativeTools/godot_headers/blob/master/README.md#how-do-i-use-native-scripts-from-the-editor ) to create the `.gdns`
2017-05-18 02:14:18 +00:00
# Implementing with gdscript
2017-05-30 21:13:48 +00:00
```gdscript
2017-07-25 09:54:55 +00:00
var simpleclass = load("res://simpleclass.gdns").new();
2017-05-18 02:14:18 +00:00
simpleclass.method("Test argument");
```
2017-07-25 09:54:55 +00:00