• Stars
    star
    1,020
  • Rank 45,113 (Top 0.9 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 1 year ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

New Godot bindings for Swift

SwiftGodot

SwiftGodot provides Swift language bindings for the Godot 4.1 game engine using the new GDExtension system.

SwiftGodot can be used to either build an extension that can be added to an existing Godot project, where your code is providing services to the game engine, or it can be used as an API with SwiftGodotKit which embeds Godot as an application that is driven directly from Swift.

Driving Godot from Swift has the advantage that on MacOS you can debug your code from Xcode as well as the Godot code.

You can browse the API documentation, and it can also be edited for local use, if you enable it in the Generator.

Screen.Recording.2023-04-02.at.11.59.18.AM.mov

Consuming SwiftGodot

There are two ways of consuming SwiftGodot, you can either reference this module in SwiftPM by using this address - and it will trigger a complete source code build for you, or to quickly iterate on MacOS, you can use a convenient binary in the peer https://github.com/migueldeicaza/SwiftGodotBinary

Currently this requires Swift 5.9 or Xcode 15.

Working with this Repository

You should be all set by referencing this as a package from SwiftPM but if you want to just work on the binding generator, you may want to open the Generator project and edit the okList variable to trim the build times.

Driving Godot From Swift

To drive Godot from Swift, use the companion SwiftGodotKit module which embeds Godot directly into your application, which allows you to to launch the Godot runtime from your code.

Creating an Extension

Creating an extension that can be used in Godot requires a few components:

  • Your Swift code: this is where you bring the magic
  • A .gdextension file that describes where to find the requires Swift library assets
  • Some Swift registation code and bootstrap code
  • Importing your extension into your project

Your Swift Code

Your Swift code will be compiled into a shared library that Godot will call. To get started, the simplest thing to do is to create a Swift Library Package that references the Swift Godot package, like this:

// swift-tools-version: 5.8
import PackageDescription

let package = Package(
    name: "MyFirstGame",
    products: [
        .library(name: "MyFirstGame", type: .dynamic, targets: ["MyFirstGame"]),
    ],
    dependencies: [
        .package(url: "https://github.com/migueldeicaza/SwiftGodot", branch: "main")
    ],
    targets: [
        .target(
            name: "MyFirstGame",
            dependencies: ["SwiftGodot"],
            swiftSettings: [.unsafeFlags(["-suppress-warnings"])],
            linkerSettings: [.unsafeFlags(
                ["-Xlinker", "-undefined",
                 "-Xlinker", "dynamic_lookup"]
            )]
        )
		            
    ]
)

The above will compile all of SwiftGodot for you - alternatively, if you do not need access to the source, you can use the .binaryTarget feature of SwiftPM and reference an .xcframework that I have conveniently published on GitHub at https://github.com/migueldeicaza/SwiftGodotBinary

The next step is to create your source file with the magic on it, here we declare a spinning cube:

class SpinningCube: Node3D {
    required init () {
        super.init ()
        let meshRender = MeshInstance3D()
        meshRender.mesh = BoxMesh()
        addChild(node: meshRender)
    }

    public override func _process(delta: Double) {
        rotateY(angle: delta)
    }
}

Additionally, you need to write some glue code for your project to be loadable by Godot, you can do it like this:

/// We register our new type when we are told that the scene is being loaded
func setupScene (level: GDExtension.InitializationLevel) {
    if level == .scene {
        register(type: SpinningCube.self)
    }
}

// Export our entry point to Godot:
@_cdecl ("swift_entry_point")
public func swift_entry_point(
    interfacePtr: OpaquePointer?,
    libraryPtr: OpaquePointer?,
    extensionPtr: OpaquePointer?) -> UInt8
{
    print ("SwiftGodot Extension loaded")
    guard let interfacePtr, let libraryPtr, let extensionPtr else {
        print ("Error: some parameters were not provided")
        return 0
    }
    initializeSwiftModule(interfacePtr, libraryPtr, extensionPtr, initHook: setupScene, deInitHook: { x in })
    return 1
}

Bundling Your Extension

To make your extension available to Godot, you will need to build the binaries for all of your target platforms, as well as creating a .gdextension file that lists this payload, along with the entry point you declared above.

You would create something like this in a file called MyFirstGame.gdextension:

[configuration]
entry_symbol = "swift_entry_point"

[libraries]
macos.debug = "res://bin/MyFirstGame"
macos.release = "res://bin/MyFirstGame"
windows.debug.x86_32 = "res://bin/MyFirstGame"
windows.release.x86_32 = "res://bin/MyFirstGame"
windows.debug.x86_64 = "res://bin/MyFirstGame"
windows.release.x86_64 = "res://bin/MyFirstGame"
linux.debug.x86_64 = "res://bin/MyFirstGame"
linux.release.x86_64 = "res://bin/MyFirstGame"
linux.debug.arm64 = "res://bin/MyFirstGame"
linux.release.arm64 = "res://bin/MyFirstGame"
linux.debug.rv64 = "res://bin/MyFirstGame"
linux.release.rv64 = "res://bin/MyFirstGame"
android.debug.x86_64 = "res://bin/MyFirstGame"
android.release.x86_64 = "res://bin/MyFirstGame"
android.debug.arm64 = "res://bin/MyFirstGame"
android.release.arm64 = "res://bin/MyFirstGame"

In the example above, the extension always expects the platform specific payload to be called "MyFirstGame", regarless of the platform. If you want to distribute your extension to other users and have a single payload, you will need to manually set different names for those.

Installing your Extension

You need to copy both the new .gdextension file into an existing project, along with the resources it references.

Once it is there, Godot will load it for you.

Using your Extension

Once you create your extension and have loaded it into Godot, you can reference it from your code by using the "Add Child Node" command in Godot (Command-A on MacOS) and then finding it in the hierarchy.

In our example above, it would appear under Node3D, as it is a Node3D subclass.

More Repositories

1

TensorFlowSharp

TensorFlow API for .NET languages
C#
3,138
star
2

SwiftTerm

Xterm/VT100 Terminal emulator in Swift
Swift
938
star
3

mono-wasm

JavaScript
527
star
4

TermKit

Terminal Kit - Console UI toolkit for Swift applications
Swift
455
star
5

MonoTouch.Dialog

Tools to simplify creating dialogs with the user using MonoTouch
C#
430
star
6

WasmerSharp

.NET Bindings for the Wasmer Runtime
C#
317
star
7

SwiftTermApp

Swift
304
star
8

SwiftGodotKit

Embed Godot into Swift apps
C
217
star
9

PlaticaBot

MacOS, iOS and watchOS ChatGPT client using your own OpenAI key
Swift
189
star
10

TweetStation

MonoTouch based Twitter client
C#
180
star
11

XtermSharp

XTerm emulator as a .NET library
C#
161
star
12

redis-sharp

A C#/.NET binding for the Redis server.
C#
152
star
13

SkiaKit

Swift Bindings to the Skia 2D graphics Library
Swift
134
star
14

TextBufferKit

Swift TextBuffer
Swift
104
star
15

CovidGraphs

Covid Graphs app
Swift
93
star
16

SwiftChatGPT

Simple ChatGPT API
Swift
74
star
17

GodotSwift

Swift bindings for the Godot Game Engine
C
56
star
18

mc

C# based curses file manager
C#
45
star
19

FuchsiaSharp

Bindings to Fuchsia
C#
36
star
20

OpenFlowSharp

Sample CoverFlow implementation for MonoTouch, based on OpenFlow
C#
33
star
21

muget

Command line front end to NuGet that is not overengineered
C#
31
star
22

RealityActions

RealityActions
Swift
27
star
23

monotouch-libs

Various MonoTouch bindings and libraries
C#
25
star
24

TldExtract

.NET Library to extracts the root domain, subdomain name, and top level domain from a host name using the Public Suffix List
C#
22
star
25

SwiftNavigation

C++
19
star
26

SwiftGodotBinary

SwiftGodot - binaries for ease of consumption
Swift
18
star
27

libgodot

Godot modified to be embeddable as a library, for use in SwiftGodotKit
C++
18
star
28

mono-wasm-libc

C
15
star
29

TurboSwift

Swift
14
star
30

mono-wasm-mono

C#
14
star
31

FluidInterfaces

Port of Nathan Gitter's Fluid Interfaces code
C#
14
star
32

MaterialSharp

Material design components for iOS applications written in C#
C#
13
star
33

NetCatNetwork

NetCat implementation using the new Apple Network framework, C# port of the C sample
C#
11
star
34

CovidExtractor

My Covid Extractor
Swift
9
star
35

SwiftGodotDocs

9
star
36

CocoaDriver

Xamarin.Mac-based Cocoa driver for Mono's System.Windows.Forms
C#
8
star
37

Darwin.CopyFile

Bindings for Darwin's CopyFile API
C#
8
star
38

SwiftNavigationDemo

Demo of the SwiftNavigation API
Swift
8
star
39

MonoTouch.Bindings

Some bindings for third party libraries
6
star
40

migueldeicaza

Repository for my README
6
star
41

gtk

Fork of Gtk branch 2-24 with all Xamarin patches from github.com/mono/bockbuild applied
C
5
star
42

KeyboardButton

Simple keyboard button in SwiftUI eventually for use in the Terminal Emulator
Swift
5
star
43

GodotSwiftLink

Binding module, temporary, while I move it to Godot.
C
4
star
44

Mac-SceneKit-Globe-Test

Sample F# Mac app showing SceneKit in action.
F#
4
star
45

PhotoPanner

C# version of Facebooks Photo Panner
C#
4
star
46

Mono.VFS

C# based Virtual File System API.
4
star
47

SimpleCollage

Simple picture collage with SkiaSharp
C#
4
star
48

til

4
star
49

MapWrapper

SwiftUI wrapper around MapKit
Swift
4
star
50

SwiftApiExtractIssue

Shows a bug with swift-api-extract
Swift
3
star
51

RosettaMaker

Creates the Rosetta HTML page that shows the C#/Objective-C/C mappings
C#
3
star
52

KitCore.NET

.NET bindings to KitCore - the ultimate core application framework
3
star
53

SplashView

C# Port of SplashView https://github.com/callumboddy/CBZSplashView
C#
3
star
54

NailedIt

My Google Glasses app to "Capture Moments" and "Nailed it"
Java
3
star
55

FileProvider-ios-crashing

Swift
2
star
56

monotouch-samples

MonoTouch Sample Programs
2
star
57

SwiftSH.binaries

Binary for the SwiftSH framework
C
2
star
58

SkiaKit.binaries

Precompiled binaries for various platforms of the runtime used by SkiaKit (SkiaSharp)
2
star
59

CovidExtractorPM

SwiftPM version of CovidExtractor
Swift
2
star
60

paint-mono

Automatically exported from code.google.com/p/paint-mono
C#
2
star
61

SkiaKitPayloads

Binary payloads directory for the SkiaKit library
Shell
1
star
62

SwiftOnnxMnist

Mnist sample using OnnxRuntime in Swift
C++
1
star
63

TreemapWeb

C#
1
star
64

iptoloc

IP to Location Tools
C#
1
star
65

mlist

Gtk# Mlist widget - managed implementation of a list view (2005 era)
C#
1
star
66

mono-soc-2007

Automatically exported from code.google.com/p/mono-soc-2007
C#
1
star
67

blog-monomac

MonoMac Blog
1
star
68

SwiftDocCPlusPlusBug

Sample of how Swift Docc fails if you use the new Swift C++ interop
Swift
1
star
69

GodotSnippetEditor

A tool to edit Godot documentation code snippets
Swift
1
star
70

SwiftTermDocs

Placeholder to publish the API docs for SwiftTerm
1
star
71

esctest

Mirror from https://gitlab.freedesktop.org/terminal-wg/esctest.git
Python
1
star
72

CoreKitSharp

Bindings to the Ultimate Framework Platform
1
star