godot-cpp/README.md

75 lines
2.1 KiB
Markdown
Raw Normal View History

# godot-cpp
2021-08-19 23:25:36 +00:00
C++ bindings for the Godot extension API.
2021-08-19 23:25:36 +00:00
**Note: this is a work in progress for the extension system included in Godot 4.0**
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
2018-02-11 14:50:01 +00:00
2021-08-19 23:25:36 +00:00
## Stub
2021-09-27 09:48:49 +00:00
2021-08-19 23:25:36 +00:00
Both this whole bindings system and this document are still work in progress and
thus it is still incomplete. It will be improved once the extension API is settled.
2021-09-27 09:48:49 +00:00
2021-08-19 23:25:36 +00:00
## How to use
2021-09-27 09:48:49 +00:00
2021-08-19 23:25:36 +00:00
It's a bit similar to what it was for 3.x but also a bit different.
2021-09-27 09:48:49 +00:00
2021-08-19 23:25:36 +00:00
Compiling this repository generates a static library to be linked with your shared lib,
just like before.
2021-09-27 09:48:49 +00:00
2021-08-19 23:25:36 +00:00
To use the shared lib in your Godot project you'll need a `.gdextension` file, which replaces what was the `.gdnlib`before. Follow the example:
2021-09-27 09:48:49 +00:00
2021-08-19 23:25:36 +00:00
```ini
[configuration]
2021-08-19 23:25:36 +00:00
entry_symbol = "example_library_init"
2021-08-19 23:25:36 +00:00
[libraries]
2021-08-19 23:25:36 +00:00
Linux.64 = "bin/x11/libgdexample.so"
```
2021-08-19 23:25:36 +00:00
The `entry_symbol` is the name of the function that initializes your library. It should be similar to following layout:
2018-01-29 22:05:42 +00:00
```cpp
2021-08-19 23:25:36 +00:00
extern "C" {
GDNativeBool GDN_EXPORT example_library_init(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization) {
GDNativeBool result = godot::GDExtensionBinding::init(p_interface, p_library, r_initialization);
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
2018-02-11 14:50:01 +00:00
2021-08-19 23:25:36 +00:00
if (result) {
register_example_types();
}
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
2018-02-11 14:50:01 +00:00
2021-08-19 23:25:36 +00:00
return result;
2017-07-25 09:54:55 +00:00
}
2017-05-18 02:14:18 +00:00
}
```
2021-08-19 23:25:36 +00:00
The `register_example_types()` should register the classes in ClassDB, very like a Godot module would do.
```cpp
2021-08-19 23:25:36 +00:00
using namespace godot;
void register_example_types() {
ClassDB::register_class<Example>();
}
```
2021-08-19 23:25:36 +00:00
Extension registering has not yet been added to the Godot editor, so to make it recognize your extension you need to add the following section to your `project.godot`:
2021-08-19 23:25:36 +00:00
```ini
[native_extensions]
2021-08-19 23:25:36 +00:00
paths=["res://example.gdextension"]
```
2021-08-19 23:25:36 +00:00
Any node and resource you register will be available in the corresponding `Create...` dialog. Any class will be available to scripting as well.
2021-08-19 23:25:36 +00:00
## Example
2021-08-19 23:25:36 +00:00
Check the project in the `test` folder for an example on how to use and register different things.
2021-08-19 23:25:36 +00:00
This project isn't yet made to run in CI.
2021-08-19 23:25:36 +00:00
## Issues
2021-08-19 23:25:36 +00:00
This really needs to be tested and very likely has things missing that weren't noticed yet. Use at your own risk (and contribute back with reports and fixes if you can).