This is a short guide to get going with Rust and Godot 3.2. This is written with Linux in mind but most of the knowledge should be transferable to MacOS and Windows.
- Godot 3.2
- Rust (at the time of writing version 1.41 was used)
Create a new rust project:
cargo new --lib mygame.
Cargo.toml and add the
[lib] crate-type = ["cdylib"]
and update the
[dependencies] section to include gdnative:
[dependencies] gdnative = "0.9.0"
src/lib.rs and add the following code:
cargo build and you should have a
lib<projectname>.so file in
<projectname> is the name of your project)
This file should be copied / moved into your Godot project once that’s crated.
.so, and on Windows it’s
Create a new Godot project and copy the
lib<projectname>.so file into your new
GDNativeLibrary by pressing the “new” button in the inspector:
Select the directory icon to load your dynamic library (your
.so file on
Once the library is selected, save the
GDNativeLibrary, as it is not saved
when you save the scene, and make sure the file extension is
.gdnlib (it will
default to .tres).
This is the final step. Once you have created (and saved!) your
you can finally add a node and attach your
HelloWorld struct to it.
Create a new node of type
If you copied the code from step 4 into
lib.rs you may have noticed the
Node as the
owner type, as well as in
#[derive(gdnative::NativeClass)] #[inherit(gdnative::Node)] struct HelloWorld;
If you want to use another node type, e.g
Node with the new node type.
Attach a new script to the node. In the “Language” section change from “GDScript” to “NativeScript”, and enter “HelloWorld” in the class name (this should be the name of your struct).
Finally you need to load the
gdnlib file from step 5. Select the “Library”
drop-down (currently reading [empty]), select “Load” and finally select your
If you run the project with F6 (or press “Play Scene”) you should see “hello, world.” in the Godot output console.
It will help with figuring out how most things work by combining the
cargo doc --open from your rust project) with the Godot
documentation: https://docs.godotengine.org/en/stable/index.html, e.g seeing the
call signature in Rust docs, and the default values in the Godot documentation.
If you want to see stack traces in the event of a panic and you are building for
release, add the following to your
[profile.release] debug = True