• Stars
    star
    376
  • Rank 113,810 (Top 3 %)
  • Language
    C
  • License
    MIT License
  • Created over 7 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Headers for the Godot API supplied by the GDNative module.

godot-headers

This repository contains C headers for Godot Engine's GDNative API, which can be used to write NativeScripts.

GDNative enables the use of dynamically linked libraries inside of Godot.

NativeScript uses GDNative to implement scripts backed by native code.

Versioning

This repositories follows the same branch versioning as the main Godot Engine repository:

  • master tracks the current development branch. As this is a moving target, the headers in this repository may not always be fully in sync with upstream. See Updating Headers if you need to bring them up to date.
  • 3.x tracks the development of the next 3.x minor release. Like master, it might not always be fully up-to-date with upstream.
  • Other versioned branches (e.g. 3.3, 3.2) track the latest stable release in the corresponding branch.

Stable releases are also tagged on this repository: Tags.

For any project built against a stable release of Godot, we recommend using this repository as a Git submodule, checking out the specific tag matching your Godot version.

Getting Started

Build latest version of Godot GitHub Docs

Clone godot-headers into Library

Clone godot-headers under SimpleLibrary/

cd SimpleLibrary
git clone https://github.com/godotengine/godot-headers

Note that the master branch of this repository contains the headers for the latest Godot master branch. See Versioning for details. You can use -b <version> to the above Git clone command to retrieve a specific branch or tag (e.g. -b 3.x or -b godot-3.3.3-stable).

[SimpleLibrary]
  β”œβ”€β”€ lib/
  └── src/

Create Script

Create test.c under SimpleLibrary/src/.

#include <gdnative/gdnative.h>
#include <nativescript/godot_nativescript.h>

#include <stdio.h>

void *test_constructor(godot_object *obj, void *method_data) {
	printf("test.constructor()\n");
	return 0;
}

void test_destructor(godot_object *obj, void *method_data, void *user_data) {
	printf("test.destructor()\n");
}

/** func _ready() **/
godot_variant test_ready(godot_object *obj, void *method_data, void *user_data, int num_args, godot_variant **args) {
	godot_variant ret;
	godot_variant_new_nil(&ret);

	printf("_ready()\n");

	return ret;
}

/** Library entry point **/
void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
}

/** Library de-initialization **/
void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
}

/** Script entry (Registering all the classes and stuff) **/
void GDN_EXPORT godot_nativescript_init(void *desc) {
	printf("nativescript init\n");

	godot_instance_create_func create_func = {
		.create_func = &test_constructor,
		.method_data = 0,
		.free_func   = 0
	};

	godot_instance_destroy_func destroy_func = {
		.destroy_func = &test_destructor,
		.method_data  = 0,
		.free_func    = 0
	};

	godot_nativescript_register_class(desc, "SimpleClass", "Node", create_func, destroy_func);

	{
		godot_instance_method method = {
			.method = &test_ready,
			.method_data = 0,
			.free_func = 0
		};

		godot_method_attributes attr = {
			.rpc_type = GODOT_METHOD_RPC_MODE_DISABLED
		};

		godot_nativescript_register_method(desc, "SimpleClass", "_ready", attr, method);
	}
}

godot_variant GDN_EXPORT some_test_procedure(void *data, godot_array *args) {
	godot_variant ret;
	godot_variant_new_int(&ret, 42);

	godot_string s;
	godot_string_new_with_wide_string(&s, L"Hello World", 11);
	godot_print(&s);

	godot_string_destroy(&s);

	return ret;
}

Expand Details for example code.

Compile Library

On Linux:

clang -g -fPIC -c src/test.c -I/path/to/godot/headers/ -o src/test.os
clang -g -shared src/test.os -o lib/test.so

On MacOS:

clang -g -fPIC -c src/test.c -I/path/to/godot/headers/ -o src/test.os
clang -g -shared -framework Cocoa -Wl,-undefined,dynamic_lookup src/test.os -o lib/test.dylib
  • -g is for debugging information.
  • Use godot_nativescript_* methods only in the nativescript_init() function.

Create GDNativeLibrary Resource

The GDNativeLibrary resource contains links to the libraries for each platform.

  1. Create a new resource in memory and edit it.
  2. Select Resource > GDNativeLibrary.
  3. Set the library file for your platform inside the inspector.
  4. Save the edited resource as a .tres

Note: Remember to save GDNativeLibrary as .gdnlib

Expand Details for screenshots.

Using GDNativeLibrary in GDScript

extends Node

func _ready():
	var gdn = GDNative.new()
	gdn.library = load("res://lib/libtest.tres")

	gdn.initialize()

	var res = gdn.call_native("standard_varcall", "some_test_procedure", [])

	print("result: ", res)

	gdn.terminate()

Attaching GDNativeLibrary to a Node

  1. Attach a new script to a node.
  2. In the pop-up dialog, choose NativeScript in the Language menu.
  3. Enable built-in script, or create a .gdn file, which only contains a name.
  4. Specify the Class Name.
  5. Press Create.

The GDNativeLibrary field in a NativeScript is empty by default.

Expand Details for screenshots.

FAQ

What is the difference between GDNative and NativeScript?

GDNative is a new class that can call native functions in libraries. GDScript / VisualScript / C#, etc, are able to use this class.

Godot treats NativeScript as a scripting language, enabling the use of GDNative to implement scripts backed by native code.

Which languages are binding as a NativeScript?

C++, D, Nim

Can you debug NativeScripts?

You must compile the library with debug symbols, and then you can use your debugger as usual.

Can you use one GDNativeLibrary for all NativeScripts?

You can! ✨

What is the reason behind the name "GDNative"?

GDNative was originally named "cscript" because it exposes a C API, but people mistook a relation to C#, which is sometimes abbreviated as "cs". Then named "DLScript", but that brought up some confusion, so we settled with GDNative. πŸ“–

Updating Headers

See Versioning for details on the Godot versions tracked by each branch of this repository.

If the relevant branch is not up-to-date for your needs, or if you want to sync the headers with your own modified version of Godot, here is the update procedure used to sync this repository with upstream releases:

  • Compile Godot Engine at the specific version/commit which you are using.
  • Use the compiled executable to generate the api.json file with: godot --gdnative-generate-json-api api.json
  • Copy the file modules/gdnative/gdnative_api.json to this repository.
  • Copy the files and folders from modules/gdnative/include to this repository, overwriting existing content. (To be sure to be in sync, you can delete the folders of this repository first, then copy the upstream folders in place.) Make sure that you compiled the correct Godot version so that the generated gdnative_api_struct.gen.h is up-to-date.

More Repositories

1

godot

Godot Engine – Multi-platform 2D and 3D game engine
C++
88,512
star
2

awesome-godot

A curated list of free/libre plugins, scripts and add-ons for Godot
6,841
star
3

godot-demo-projects

Demonstration and Template Projects
GDScript
5,567
star
4

godot-docs

Godot Engine official documentation
reStructuredText
3,655
star
5

godot-cpp

C++ bindings for the Godot script API
C++
1,607
star
6

godot-vscode-plugin

Godot development tools for VSCode
TypeScript
1,517
star
7

godot-blender-exporter

Addon for Blender to directly export to a Godot Scene
Python
1,154
star
8

godot-proposals

Godot Improvement Proposals (GIPs)
1,089
star
9

tps-demo

Godot Third Person Shooter with high quality assets and lighting
GDScript
972
star
10

godot-git-plugin

Git implementation of the VCS interface in Godot
C++
669
star
11

collada-exporter

"Better" Collada exporter for Blender, orignally developed by the Godot Engine community
Python
416
star
12

godot-syntax-themes

Syntax themes for the Godot Engine script editor
397
star
13

FBX2glTF

A command-line tool for the conversion of 3D model assets on the FBX file format to the glTF file format.
C++
326
star
14

emacs-gdscript-mode

An Emacs package to get GDScript support and syntax highlighting.
Emacs Lisp
303
star
15

godot-asset-library

PHP frontend for Godot Engine's asset library
PHP
289
star
16

godot-website

The code for the official Godot Engine website. A static site built using Jekyll.
HTML
283
star
17

godot-csharp-visualstudio

Godot C# extension for Visual Studio
C#
234
star
18

build-containers

Godot engine build containers
Shell
207
star
19

webrtc-native

The official GDNative WebRTC implementation for non-html exports.
C++
205
star
20

godot-design

Visual design specific stuff for the godot engine
202
star
21

gdnative-demos

Demo projects for GDNative
Python
191
star
22

godot-old-gsoc-ideas

Old ideas for Google Summer of Code (no longer relevant)
165
star
23

godot-google-play-billing

Godot Android plugin for the Google Play Billing library
Java
146
star
24

godot-csharp-vscode

Debugger and utilities for working with Godot C# projects in VSCode
TypeScript
141
star
25

godot-ios-plugins

Objective-C++
128
star
26

godot-benchmarks

Collection of benchmarks to test performance of different areas of Godot
GDScript
121
star
27

godot-visual-script

VisualScript as a Godot Engine c++ module
C++
118
star
28

godot-cpp-template

Quickstart template for GDExtension development with Godot
Python
109
star
29

godot-3d-dodge-the-creeps

This project was moved to https://github.com/godotengine/godot-demo-projects/tree/master/3d/squash_the_creeps
GDScript
91
star
30

godot-build-scripts

Build scripts used for official Godot Engine builds with https://github.com/godotengine/build-containers
Shell
79
star
31

godot-mono-builds

Mono build scripts for Godot
Python
56
star
32

godot-builds

Official pre-releases, dev snapshots, and custom builds of the Godot engine.
Python
54
star
33

godot-docs-l10n

Localization of the Godot documentation – Translations should be done on Weblate (see link)
Shell
51
star
34

godot-3d-platformer-demo

3D platformer, developed as part of the Mozilla Grant 2019
44
star
35

godot-tests

Repository for Godot benchmarks, regression tests, etc.
GDScript
35
star
36

godot-interactive-changelog

An interactive tool to view a changelog for each version of Godot Engine
JavaScript
30
star
37

godot-monodevelop-addin

Godot Add-in for MonoDevelop and Visual Studio for Mac
C#
30
star
38

godot-docs-project-starters

A collection of project templates and assets used by tutorials in the official Godot documentation. https://github.com/godotengine/godot-docs
GDScript
27
star
39

mousse

High quality 3D platform demo, designed to make the best use of Godot 4.0
24
star
40

doc-status

Online Godot class reference status
JavaScript
21
star
41

godot-platform-haiku

Godot Engine platform port for the Haiku operating system // UNMAINTAINED, for reference / forks.
C++
19
star
42

webrtc-actions

A set of github actions to build WebRTC as a single static library.
18
star
43

regression-test-project

Godot engine regression test project
GDScript
17
star
44

godot-team-reports

Browse Godot PR backlog for each maintenance team
JavaScript
17
star
45

godot-editor-l10n

Localization of the Godot editor and class reference – Translations should be done on Weblate (see link)
Python
14
star
46

gdscript-tests

Tests for the GDScript module implementation
HTML
13
star
47

godot-showreel-voting

A Django app to review and vote videos
Python
12
star
48

godot-prs-by-file

JavaScript
12
star
49

.github

Godot community health files
10
star
50

community-map

Map of regional community locations, submit your community here
9
star
51

issue-bot

Issuebot for chat.godotengine.org
Python
8
star
52

godot-commit-artifacts

A tool providing quick links to latest CI builds of development branches
JavaScript
8
star
53

godot-nir-static

C++
8
star
54

godot-benchmarks-results

Website to display godot-benchmarks results
6
star
55

godot-website-cover-generator

JavaScript
5
star
56

moltenvk-osxcross

Custom MoltenVK builds with `-fno-objc-msgsend-selector-stubs` to be compatible with osxcross
4
star
57

godot-question2answer

A repository hosting the current platform used at https://ask.godotengine.org
PHP
4
star
58

godot-docs-user-notes

User notes for the Godot Engine official documentation
4
star
59

issue-stats

Gather hardware and software information based on Godot GitHub issue reports
Python
3
star
60

discourse-theme

A custom Godot styled theme for discourse
SCSS
2
star