Swift language support for the Godot game engine

Swift for Godot is here!

https://github.com/kelvin13/godot-swift

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"
    }
}

tutorials and example programs

  1. basic usage (sources)
  2. advanced methods (sources)
  3. advanced properties (sources)
  4. signals (sources)
  5. life cycle management (sources)
  6. using custom types (sources)

api reference

math library reference

How you can help!

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.

Have fun!

24 Likes

This is a dream come true!

4 Likes

This is something beyond awesome. I'll have a deeper look later

1 Like

Fascinating.

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.

4 Likes

Take a look at ArgumentParser subcommand autodiscovery! theres some interesting Runtime Reflection that might be helpful in your explorations

3 Likes

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.

1 Like

Take a look at ArgumentParser subcommand autodiscovery! 18 theres some interesting Runtime Reflection that might be helpful in your explorations

Thanks for the reference, I will check it out. Generally, I want to find which methods for a base class have been overwritten by the user, like this:

class MyClass: SomeGodotBadeClass {
   override func SomeBaseGodotMethod() {
       Stuff()
       super.SomeBaseGodotMethod()
       MoreStuff()
    }

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.

I have no idea what Godot is, but I guess people have long been waiting for this

5 Likes

It’s an open-source game engine that’s been gathering a lot of interest :wink: https://godotengine.org/

1 Like

one could say they were waiting for Godot Swift

7 Likes

@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

Well done! I was working on some Raylib bindings a few months ago, but I may have to check this out and see if I can get it compiling on macOS. :smiley:

3 Likes

For anyone interested in Godot with Swift, there is a new project for this

3 Likes