Godot Swift provides bindings and a package plugin-powered build system for creating Godot native libraries from Swift frameworks. Godot Swift exposes GDScript APIs in terms of protocols and generics, allowing for seamless Swift-to-Godot interoperability.
Here’s a minimal 35-line example of a Godot Swift nativescript:
final
class MySwiftClass:Godot.NativeScript
{
var foo:Int = 5
init(delegate _:Godot.Unmanaged.Spatial)
{
}
func bar(delegate _:Godot.Unmanaged.Spatial, x:Int) -> Int
{
self.foo * x
}
@Interface
static var interface:Interface
{
Interface.properties
{
\.foo <- "foo"
}
Interface.methods
{
bar(delegate:x:) <- "bar"
}
}
}
extension Godot.Library
{
@Interface
static var interface:Interface
{
MySwiftClass.self <- "MyExportedSwiftClass"
}
}
Godot Swift comes with a resource installation script that automates creating and installing .gdnlib and .gdns resource files for its build products. While Godot Swift itself is written is pure Swift, and should work on all platforms, the resource installation script currently only works on Linux. You can help port it to Windows and MacOS! (This should only require updating a few file extension patterns.)
Beyond that, Godot Swift could always use testers and tutorial writers. Feel free to open a Github issue for any questions or concerns.
I did some bindings for Godot myself, but for the unstable release, rather than the stable one:
While they work, I did struggle with a way of wiring up Swift definitions automatically, without having to manually register classes and methods. Sadly, Swift reflection capabilities seem quite limited in this regard - your two-step compilation might help.
What I would love to get to is the same place the C# binding is, where merely overriding a method will be registered with Godot as a registered method.
I am afraid that perhaps the only option for this is to use something like the Language Server Protocol to parse the code, and determine which overrides exist.
The two-stage compilation is mainly used for variadic generics, it prints the function types of the registered methods, and then parses them to determine the templates it needs to generate. Godot Swift never sees the source files or any syntax-level representation of them.
I want to be able to determine that MyClass needs to be registered with the Godot runtime, and furthermore that the “SomeBaseGodotMethod” needs to be registered for this class, so Godot invokes it when necessary.
For my runtime, I got this working, but users need to manually register their classes, and they manually need to register each function.
@taylorswift Have you heard anything from the Godot community? I'm curious whether this is something that may get traction inside that community, and grow the wider Swift community.
i announced it on reddit a few days ago, but i think the potential audience is still small as most of the Godot community prefers C++ and Rust. it might grow over time though