2018-02-25 15:25:42 +00:00
# godot-cpp
2017-03-02 22:50:27 +00:00
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
2018-02-25 15:25:42 +00:00
Getting latest `godot-cpp` and `godot_headers`
2017-05-18 02:14:18 +00:00
```
2018-02-25 15:25:42 +00:00
$ git clone https://github.com/GodotNativeTools/godot-cpp
2017-05-18 02:14:18 +00:00
$ git clone https://github.com/GodotNativeTools/godot_headers
```
2018-02-25 15:25:42 +00:00
right now our directory structure should look like this:
2017-05-18 02:14:18 +00:00
```
2018-02-25 15:25:42 +00:00
godot-cpp
godot_headers
SimpleLibrary
├── lib/
└── src/
2017-05-18 02:14:18 +00:00
```
Now to generate cpp bindings
```
2018-02-25 15:25:42 +00:00
$ cd godot-cpp
$ scons godotbinpath="../godot_fork/bin/godot_binary" p=linux
$ cd ..
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:**
2018-02-25 15:25:42 +00:00
> `regenerate_bindings=yes` is used to force regenerating C++ bindings (`godot_api.json` - Godot API)
2017-10-03 15:48:47 +00:00
> Include `use_llvm=yes` for using clang++
2017-05-18 02:14:18 +00:00
2018-02-25 15:25:42 +00:00
2017-08-04 21:41:25 +00:00
And our directory structure will be
2017-05-18 02:14:18 +00:00
```
2018-02-25 15:25:42 +00:00
godot-cpp
└── bin/libgodot-cpp.a
godot_headers
SimpleLibrary
├── lib/
└── src/
2017-05-18 02:14:18 +00:00
```
# Creating simple class
Create `init.cpp` under `SimpleLibrary/src/` and add the following code
2018-01-29 22:05:42 +00:00
```cpp
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 ** /
2018-01-24 03:05:19 +00:00
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o)
{
godot::Godot::gdnative_init(o);
2017-07-25 09:54:55 +00:00
}
/** GDNative Terminate ** /
2018-01-24 03:05:19 +00:00
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o)
{
godot::Godot::gdnative_terminate(o);
2017-07-25 09:54:55 +00:00
}
/** NativeScript Initialize ** /
2018-01-24 03:05:19 +00:00
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
{
godot::Godot::nativescript_init(handle);
godot::register_class< SimpleClass > ();
2017-05-18 02:14:18 +00:00
}
```
# Compiling
```
2018-02-25 15:25:42 +00:00
$ 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
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