Skip to content

Godot Swift Tree - a type-safe Godot node tree representation in Swift.

Notifications You must be signed in to change notification settings

tomwyr/godot-swift-tree

Repository files navigation

Godot Swift Tree

Godot Swift Tree enhances development of Godot games using Swift bindings by generating a statically typed object mapping the Godot project nodes to Swift.

In short, instead of referencing node with a string path and casting:

getNode(NodePath("/root/Path/To/Some/Nested/Area/Node")) as Area2D

the same can be achieved using generated typed fields:

GDTree.Scene.Path.To.Some.Nested.Area.Node

The references are generated automatically based on the project and scenes declarations meaning that any modifications to the node tree structure can be easily tracked in Swift sources. Rebuilding Swift project after node paths change will result in compile-time errors that otherwise could become difficult to debug runtime errors.

For more information about developing Godot games using Swift, head to SwiftGodot repository.

Setup

Swift

Configure plugin in the Package.swift file:

// Add plugin dependency
let package = Package(
    dependencies: [
        .package(url: "https://github.com/tomwyr/godot-swift-tree", from: "1.0.0"),
    ]
)

Godot

No additional setup of the Godot project is needed.

Usage

Create a scene with nodes in Godot Editor and run GenerateNodeTree plugin command:

swift run GenerateNodeTree

Optional configuration:

  • --project-path <project-path>
    Relative path to the directory containing the Godot project.

  • --output-dir <output-dir>
    Relative path to the directory where the node tree code will be generated.

image

The command will scan your Godot project files and generate node tree representing the scene:

class GDTree {
    private init() {}

    static let Main = MainScene("/root")
}

class MainScene: NodeKey<Node> {
    let ColorRect: NodeKey<ColorRect>
    let ColorAnimator: NodeKey<Node2D>
    //...

    init(_ path: String) {
      ColorRect = NodeKey("\(path)/Main/ColorRect", "ColorRect")
      ColorAnimator = NodeKey("\(path)/Main/ColorAnimator", "Node2D")
      //...
    }
}

Reference the generated tree from within Node classes:

import SwiftGodot

@Godot
class ColorAnimator: Node2D {
    @NodeRef(GDTree.Main.ColorRect)
    var colorRect: ColorRect

    override func _process(delta: Double) {
        colorRect.color = calcColorForDelta(delta)
    }

    //...
}

Run project:

demo.mp4

Contributing

Every kind of help aiming to improve quality and add new functionalities is welcome. Feel free to:

  • Open an issue to request new features, report bugs, ask for help.
  • Open a pull request to propose changes, fix bugs, improve documentation.
  • Tell others about this project.

About

Godot Swift Tree - a type-safe Godot node tree representation in Swift.

Resources

Stars

Watchers

Forks