🕹️ Godot Rust #
Following on from the recent, trying Godot 4 post, here, we look at Godot Rust gdext. In that previous post, I mentioned how Godot not only lets you code in officially supported GDScript, but also lets you create dynamic libraries for your Godot games written in Rust, Swift, and Zig among other languages.
In this post, I run through some resources for getting started with Godot Rust gdext and also highlight some tips that I benefited from.
🧑🏽🎓 gdext Learning Resources #
gdext
provides GDExtension bindings for Godot 4. If you are working
with Godot 4, skip over gdnative
resources, which relate to the older
Godot 3 API.
The best starting point is probably the godot-rust book , which starts by setting up with gdext and runs through a basic code example. The book then move on to more advanced topics, these are really helpful, and you will probably want to keep the book open even if you jump to working on your own game after working through the initial chapters.
The official, online Rust godot docs document APIs. If you have to work offline, cargo lets you open these from a local source in a browser:
Since the gdext APIs mirror GDScript APIs, you will often want to check the GDScript API docs for additional background.
Another fantastic resource is the example game code in the gdext GitHub repo for a Dodge the Creeps game (which will sound familiar if you have followed the official Godot, GDScript-based, tutorial ). You can try building it or just dip into for help to unblock if you get stuck working on your own game.
🧱 What I Built #
After working though the gdext Hello World, I thought a good way to learn more APIs would be to start converting a game I already had from GDScript to Godot Rust gdext. For this, I picked the Knights to See You game I made by following the How to make a Video Game - Godot Beginner Tutorial .
I followed the guidelines in the godot-rust book, but made a small change, in the project file structure. This made it slightly more convenient to work in the Terminal from the project root folder, and also simplified the project CI configuration.
📂 Folder Structure #
The change I made from the godot-rust book, which I alluded to before was to set the project up
using Cargo Workspaces . The rust
folder is exactly as the book recommends, and contains
a Cargo.toml
file.
I, just, added another Cargo.toml
file to the root directory, where
I created a new workspace, adding that rust
directory:
This changes the Rust output target
directory, moving it up a level,
so I had to update the paths listed in my godot/<PROJECT>.gdextension
file:
👀 Rust Hot Reloading and Watching Files #
GDExtension for Godot 4.2 supports hot reloading out of the box. To speed up your coding feedback cycle, use cargo watch to recompile your Rust code automatically when you save a Rust source file. Install cargo watch (if you need to):
Then, you can run:
which will clear the window, run cargo check
, cargo clippy
then, cargo build
each time you save the Rust source. Now you can
hit save then jump straight to Godot Engine to test play the game.
🦀 gdext API Snippets #
I started the switch from GDScript to Godot Rust gdext with the Player scene. In the original
game, the Player Scene was a CharacterBody2D
.
Rust does not easily handle Object-oriented Programming inheritance, and gdext opts for a
composition approach. Following the book, you will see the recommended pattern, here, is to create
a Player
struct in Rust with a base
field of type Base<ICharacterBody2D>
. That base
field then provides access to the character properties that you are familiar with from GDScript.
Here are some API snippets (with equivalent GDScript) to give you a feel for gdext
.
Updating Character Velocity #
Using GDScript:
Using gdext
:
base_mut()
, here, returns a mutable reference to the base
field on Player
, mentioned above. You set and get CharacterBody2D
properties using self.base()
and self.base_mut()
.
Gravity and Project Settings #
To get project default gravity value with GDScript, you can use:
The equivalent in gdext is:
Setting Animations #
The Player scene has an AnimatedSprite2D
node. Typically, you can reference
that node in GDScript with something like this:
In gdext
, you can set the sprite like so:
Full Comparison #
For reference, here is the full code for the Rust Player
class (rust/src/player.rs
):
rust/src/player.rs
— click to expand code.
See the link further down for the full project code.
Again, for reference, here is the previous GDScript code for the Player:
godot/scripts/player.gd (deleted)
— click to expand code.
🤖 Adding Rust Library in Godot Engine #
Once you have coded and compiled the player (or other GDExtension class) to use it in your game, you just need to change the type of its Godot Scene.
Do this by right-clicking on the scene in Godot Engine (Player scene in this case) and selecting Change Type….
Then, you need to search for the name you gave your class in the Rust code. My Rust struct was
also called Player
, and I can see it in the view as a child of CharacterBody2D
. I select this and I am done!
Godot Engine now links to the dynamic shared library I created in Rust.
🗳 Poll #
🙌🏽 Godot Rust gdext: Wrapping Up #
In this post on Godot Rust gdext, we took a look through some resources for getting started with GDExtension for Godot 4. In particular, we looked at:
- resources for getting started with GDExtension and Godot Rust;
- a tip for speeding up the coding feedback cycles; and
- how to use your GDExtension dynamic library in Godot Engine.
I hope you found this useful. As promised, you can get the full project code on the Rodney Lab GitHub repo . I would love to hear from you, if you are also new to Godot video game development. Were there other resources you found useful? Also, let me know what kind of game you are working on!
🙏🏽 Godot Rust gdext: Feedback #
If you have found this post useful, see links below for further related content on this site. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on X, giving me a mention, so I can see what you did. Finally, be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee.
Just dropped a new post on setting up a Godot Rust gdext project. Covering:
— Rodney (@askRodney) July 17, 2024
— resources for getting started with GDExtension;
— adding watch to speed up feedback cycle; and
— adding your lib in Godot Engine.
Hope you find it useful!
https://t.co/Rp9HLWMefQ
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on X (previously Twitter) and also, join the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Game Dev as well as Rust and C++ (among other topics). Also, subscribe to the newsletter to keep up-to-date with our latest projects.