This is my first attempt to make a gdnative. After several trials and errors, I have decided to document what I did in order to make it work.
Note: All steps were done via linux machine.
What you need:
- godot_cpp
- godot_headers
- scons
- clang
You can clone godot_cpp and godot_headers here or use the one provided in this project:
- git clone https://github.com/GodotNativeTools/godot_headers
- git clone https://github.com/GodotNativeTools/godot-cpp
Note: Godot headers content is copied inside godot-cpp\godot_headers
STEPS Compile godot_cpp
- Go to godot_cpp > src
- Execute the following command:
scons generate_bindings="yes" p=linux
- Note: generate_bindings="yes" is used so that it will generate all the necesarry hpp files. It should generate a libgodot-cpp.xxx.xxx.a file on bin folder. This file will be used later when we are making a .so file
- Execute the following command:
Go to project folder
Create .so file. You can do it manually by following the instructions below Note: Replace cpp-name with the name of your project/cpp code. In my project, it is helloworld
- Execute:
clang -fPIC -o src/(cpp-name).os -c src/(cpp-name).cpp -g -O3 -std=c++14 -I../godot-cpp/include -I../godot_headers
It should create (cpp-name).os on src folder
- Execute:
clang -o lib/(cpp-name).so -shared src/(cpp-name).os -L../godot-cpp/include -L../godot-cpp/bin -lgodot-cpp.linux.64
It should create (cpp-name).so on lib folder
- Note: You can also run the prebuild ./clang_me script inside the project folder (which does the same thing),
Using the .so file on Godot Create a Godot Project Create a lib folder on your Godot Project Copy the .so file on your Godot lib folder Make a GDNative Resource on Godot ( + Sign on inspector > Choose GDNative)
- Put the Class name (use the name of the class that you use on your cpp file)
- On Library, Create a new GDNative Library
- It will give you a list of Platforms. Choose the correct platform.
- Point it to the .so file that you generated (inside Godot lib)
- Save the GDNative Library as .gdnlib
- Create a .gdns file. (I am usually creating a temporary node, attaching a GDNative Script, and saving the script in order to generate a gdns file. Put the name of your Class under Class Name and load the gdnlib file that you create on GDNativeLibrary
Load it via script
- load the GDNative file to a variable
var my_class = load("res://HelloWorld.gdns").new();
- You should be able to use the method as
my_class.greet();
my_class.greet_with_name("Name") # Returns a String
I have included a built Godot Project with compiled gdnative included.
References: