In a previous post, I took a look at every different way to include Rust code in a Godot game. I concluded that, for the moment, it makes the most sense to create a command-line application and have Godot read from that application’s output. It turns out that I overlooked something.
Now that I’ve taken a break from updating this site, I’ve started to focus on my Ring-Con Project again, and I’ve noticed a problem. In GDScript (assuming that you aren’t using any GDExtensions or custom modules), there’s only two ways to run an external program:
OS.create_process() doesn’t give you access to the program’s output.
OS.execute() does, but according to the Godot Docs,
[…]the complete shell output of the process will be appended as a single String element in If the complete shell output is the only thing that we have access to, then the complete shell output has to exist before we can access any shell output at all. In other words the program has to finish before we can access any shell output at all. It would be possible to create a program that prints the next batch of output every time it’s run, but that’s a lot more complicated than what I had in mind.
Luckily, I found this question on Godot Engine - Q&A. Its answers mention something that I had forgotten about: local networking. I could use any of the networking protocols that Godot supports to communicate with a program running on the local system.
With all of that in mind, here’s my updated plan:
- Create a Rust program that serves the information that I need over UDP.
- Create a Godot asset that provides an API for receiving data from Joy-Cons and Ring-Cons. For the moment, this asset will be written in GDScript. It will run the aforementioned Rust program and parse the packets that it generates.
- If godot-rust gains Godot 4 support, then transition the asset to use godot-rust.
- Eventually, if godot-rust never gains Godot 4 support, turn the asset into a GDExtension extension or a custom module.
I don’t know all that much about networking protocols, so I’m not really sure if UDP is the best option here. The best way to find out is by trying it.