https://github.com/gilzoide/high-level-gdnative
Single header GDNative high level API for C/C++
https://github.com/gilzoide/high-level-gdnative
gdnative godot godot-engine single-file single-header single-header-lib
Last synced: 8 months ago
JSON representation
Single header GDNative high level API for C/C++
- Host: GitHub
- URL: https://github.com/gilzoide/high-level-gdnative
- Owner: gilzoide
- License: unlicense
- Created: 2021-06-18T13:22:18.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-12-19T22:14:14.000Z (over 3 years ago)
- Last Synced: 2025-03-17T05:11:24.777Z (over 1 year ago)
- Topics: gdnative, godot, godot-engine, single-file, single-header, single-header-lib
- Language: C++
- Homepage:
- Size: 561 KB
- Stars: 20
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# High level GDNative C/C++ API (HGDN)
Single header [GDNative](https://docs.godotengine.org/en/stable/tutorials/plugins/gdnative/gdnative-c-example.html)
high level API for C/C++.
- Single header: just copy `hgdn.h` to your project, put `#define HGDN_IMPLEMENTATION`
in a single C/C++ source file before `#include`ing it and compile.
- Depends only on [godot-headers](https://github.com/godotengine/godot-headers),
so GDNative libraries can be built with a single compiler invocation.
No need to generate Godot API bindings if you only use core GDNative stuff.
- `hgdn_gdnative_init` fetches all current GDNative APIs.
- Useful definitions for all math types, including Vector2, Vector3 and Color.
- Wrappers around strings and pool arrays with pointer and size available.
- Functions to get values from method arguments or native calls
argument arrays.
- Functions to create Variants, Strings, Arrays, Pool Arrays and Dictionaries
in single calls.
- Overloaded macro/functions to create Variants, available in C11 and C++.
- Macros to assert arguments preconditions, like expected argument count and
(TODO) expected argument types.
## Documentation
Code is documented using [Doxygen](https://www.doxygen.nl) and is available [online here](https://gilzoide.github.io/high-level-gdnative/).
## Usage example
For a working example with full Godot project, check out the
[high-level-gdnative-example](https://github.com/gilzoide/high-level-gdnative-example)
repository.
```c
// example.c
// 1) #define HGDN_IMPLEMENTATION on exactly one C/C++ file and include hgdn.h
// Optionally #define other compile-time options, check out hgdn.h for documentation
#define HGDN_STATIC
#define HGDN_IMPLEMENTATION
#include "hgdn.h"
// 2.a) Declare native functions to be used by script code, if there are any
// Any function with prototype `godot_variant (godot_array *)` can be called in script with
// `gdnative_instance.call_native("standard_varcall", "in_editor ? " in editor" : "");
}
// 3.b) Add `godot_gdnative_terminate`, the function that will be called when Godot
// unloads this GDNativeLibrary, and call `hgdn_gdnative_terminate` from it.
GDN_EXPORT void godot_gdnative_terminate(godot_gdnative_terminate_options *options) {
hgdn_gdnative_terminate(options);
}
// 4) Add `godot_nativescript_init`, the function that will be called when Godot
// initializes a NativeScript with this GDNativeLibrary, and register classes,
// if there are any.
GDN_EXPORT void godot_nativescript_init(void *desc) {
// TODO
}
```
```gdscript
# example.gd
extends Reference
func _ready() -> void:
var example = GDNative.new()
example.library = preload("res://path_to_gdnativelibrary.gdnlib")
example.initialize() # --> "GDNative initialized"
print(example.call_native("standard_varcall", "get_message", [])) # --> "Hello world!"
print(example.call_native("standard_varcall", "square", [5])) # --> 25
print(example.call_native("standard_varcall", "sum_ints", [[1, 2.5, 3]])) # --> 6
```