SwiftGodotKit Documentation

repository·main·Indexed 18 days ago

https://github.com/migueldeicaza/swiftgodotkit

SwiftGodotKit allows developers to embed the Godot engine into existing Swift applications on iOS and macOS. It enables driving Godot from Swift and displaying full games or individual Godot UI elements within native Swift/SwiftUI apps using types like GodotApp and GodotAppView.

Tokens
1.1K
Snippets
4
Records
5
Agent score
14%

What's inside SwiftGodotKit

  1. Ensure API compatibility between SwiftGodot and libgodot

    main

    When building from source or using specific branches, you must ensure that the SwiftGodot bindings and the godot engine sources are API-compatible. Mismatched branches can lead to runtime failures with gdextension_classdb_get_method_bind errors.

    For the current embeddable system, use these specific branches:

    • SwiftGodot branch: swiftgodotkit
    • godot branch: swiftgodotkit-4.6
  2. Build libgodot locally for macOS and iOS

    main

    If you need to build the libgodot engine sources locally to create the xcframework artifacts, follow these steps using scons from the godot checkout.

    macOS (Metal-only, no MoltenVK)

    scons platform=macos arch=arm64 target=template_release library_type=shared_library vulkan=no metal=yes disable_path_overrides=no
    scons platform=macos arch=x86_64 target=template_release library_type=shared_library vulkan=no metal=yes disable_path_overrides=no

    iOS (Metal-only runtime)

    scons platform=ios arch=arm64 simulator=no target=template_release vulkan=no metal=yes disable_path_overrides=no
    scons platform=ios arch=arm64 simulator=yes target=template_release vulkan=no metal=yes disable_path_overrides=no
    scons platform=ios arch=x86_64 simulator=yes target=template_release vulkan=no metal=yes disable_path_overrides=no

    After building, use the make zip command in the SwiftGodotKit/scripts directory to package the artifacts into the expected .xcframework and .zip formats.

    # Package everything after building
    cd SwiftGodotKit/scripts
    make zip
  3. Embed Godot in a SwiftUI application

    main

    To embed a Godot game or UI elements into an existing SwiftUI project, use the GodotApp and GodotAppView types.

    1. Add your Godot PCK file to your Xcode project.
    2. Initialize a GodotApp with the path to your .pck file.
    3. Use GodotAppView() within your SwiftUI view hierarchy.
    4. Inject the GodotApp instance into the environment using the .environment(\.godotApp, app) modifier.

    Note: There can only be one GodotApp instance per application, but you can reference different scenes from it.

    import SwiftUI
    import SwiftGodot
    import SwiftGodotKit
    
    struct ContentView: View {
        @State var app = GodotApp(packFile: "game.pck")
    
        var body: some View {
            VStack {
                Text("Game is below:")
                GodotAppView()
                    .padding()
            }
            .environment(\.godotApp, app)
        }
    }
  4. Use SwiftGodotKit in a standalone Swift Package

    main

    You can integrate SwiftGodotKit into a standalone Swift Package by initializing a new package and extending its Package.swift to reference the SwiftGodotKit dependency.

    To run the standalone example, you must ensure the libgodot.dylib library is present in the current directory before executing the run command.

    # Initialize the package
    swift package init -n StandaloneExample
    
    # Prepare the environment by copying the Godot library
    cp .build/*/*/libgodot.dylib .
    
    # Run the application
    swift run
  5. Install SwiftGodotKit via Swift Package Manager

    main

    Add SwiftGodotKit to your Package.swift file by referencing the GitHub URL and specifying the exact version tag. You can then depend on the SwiftGodotKit product.

    SwiftPM will automatically handle downloading the correct libgodot binary target (libgodot-macos.xcframework.zip or libgodot-ios.xcframework.zip) based on your target platform.

    .package(url: "https://github.com/migueldeicaza/SwiftGodotKit", exact: "<SwiftGodotKit tag>")
    
    // and depend on the product
    .product(name: "SwiftGodotKit", package: "SwiftGodotKit")