вторник, 10 сентября 2024 г.

Godot Extension (GDExtension) with Swift language

 

 

 

 

 

 

 

 

 

 

 

Swift 5.10.1 Windows toolchain. (www.swift.org)

1. Run:
>>swift package init
(In folder "C:\my_GD_Extensions\myShip")

Edit file: Package.swift

// swift-tools-version: 5.10
import PackageDescription

let package = Package(
    name: "myShip",
    products: [
        .library(
            name: "myShip",
            type: .dynamic,
            targets: ["myShip"]),
    ],
    dependencies: [
        .package(url: "https://github.com/migueldeicaza/SwiftGodot", branch: "main")
    ],
    targets: [
        .target(
            name: "myShip",
            dependencies: [
                "SwiftGodot",
            ]
        ),
        .testTarget(
            name: "myShipTests",
            dependencies: ["myShip"]
        ),
    ]
)

2. Run and wait):
>>swift build

Edit file: C:\my_GD_Extensions\myShip\Sources\myShip.swift

import SwiftGodot
#initSwiftExtension(cdecl: "swift_entry_point", types: [ShipController.self])

Edit file: C:\my_GD_Extensions\myShip\Sources\ShipController.swift

import SwiftGodot
@Godot(.tool)
class ShipController: Node3D {
    public override func _ready () {
        let meshRender = MeshInstance3D()
        meshRender.mesh = BoxMesh()
        addChild(node: meshRender)
    }

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

3. Compile and copy files to Godot project
>>swift build

Make file: res://bin/myShip.gdextensions:

[configuration]
entry_symbol = "swift_entry_point"
compatibility_minimum = 4.2

[icons]
ShipController = "res://bin/my_extension.svg"

[libraries]
windows.debug.x86_64 = "res://bin/myShip.dll"

[dependencies]
windows.debug.x86_64 = "res://bin/SwiftGodot.dll"

Attention!

Copy files from "C:\Swift\Runtimes\5.10.1\usr\bin"
to folder with your extension ("res://bin")
(and copy to game Export folder).

понедельник, 9 сентября 2024 г.

Compilation SwiftGodot on Windows

Swift 5.10.1 Windows toolchain. (www.swift.org)
Download SwiftGodot (v0.45)

Run compilation
(in folder "C:\SwiftGodot-0.45")

- for DEBUG
>> swift build
(C:\SwiftGodot-0.45\.build\debug\SwiftGodot.dll)
(Copy to "res://bin/SwiftGodot_debug.x86_64.dll")

- for RELEASE
>> swift build -c release
(C:\SwiftGodot-0.45\.build\release\SwiftGodot.dll)
(Copy to "res://bin/SwiftGodot_release.x86_64.dll")

Edit file: my_extension.gdextension

[configuration]
entry_symbol = "swift_entry_point"
compatibility_minimum = 4.2
compatibility_maximum = 4.3
reloadable = true

[icons]
MyExtension = "res://bin/my_extension.svg"

[libraries]
windows.debug.x86_64 = "res://bin/my_extension_debug.x86_64.dll"
windows.release.x86_64 = "res://bin/my_extension_release.x86_64.dll"

[dependencies]
windows.debug = {
    "res://bin/SwiftGodot_debug.x86_64.dll" : "",
}

windows.release = {
    "res://bin/SwiftGodot_release.x86_64.dll" : "",
}

Attention!

Copy files from "C:\Swift\Runtimes\5.10.1\usr\bin"
to folder with your extension ("res://bin")
(and put them in [dependencies] section or copy to game Export folder).

воскресенье, 8 сентября 2024 г.

Debugging Swift in VSCode on Windows

Swift 5.10.1 Windows toolchain. (www.swift.org)

Environment Variable: SDKROOT "C:\Swift5\Platforms\5.10.1\Windows.platform\Developer\SDKs\Windows.sdk\"

Debugging in VSCode maybe needs:
    - CodeLLDB Extension
   
- Swift Extension with Setting: Swift › Debugger: Use Debug Adapter From Toolchain - "C:/Swift/Toolchains/5.10.1+Asserts/usr/bin/lldb-vscode.exe"
    - Python 3.9 installed.




воскресенье, 5 июля 2020 г.

Swift, Access Control

Online Swift Playground 

- private
- fileprivate
- internal (default)
- public
- open

//weapon.swift
public protocol Weapon {
    var ammo: Int {get}
}

//gun.swift
internal class Gun: Weapon {
    private(set) var ammo: Int
    fileprivate(set) var isEmpty: Bool

    init(ammo: Int = 3){
        self.ammo = ammo
        self.isEmpty = false
    }

    func fire() {
        guard self.ammo > 0 else {
            print("Out of ammo!")
            self.isEmpty = true
            return
        }
        self.ammo -= 1
        print("Fire!")
    }

    func reload(ammo: Int = 3){
        self.ammo = ammo
        print("Reloading!")
        self.isEmpty = false
    }
}

//main.swift
var g1 = Gun()
print("ammo: \(g1.ammo)")
while !g1.isEmpty {
    g1.fire()
}
//g1.ammo = 3 //error
g1.reload()
print("ammo: \(g1.ammo)")

четверг, 2 июля 2020 г.

Swift, Generics, Constraints

Online Swift Playground 

//Generics
func add<T: AdditiveArithmetic>(a: T, b: T) -> T {
    return a + b
}
let sum = add(a: 5, b: 5)
print("5 + 5 = \(sum)")

class Point<T>{
    var x:T
    var y:T

    init(x:T, y:T){
        self.x = x
        self.y = y
    }
}

let p1 = Point<Int>(x: 1, y: 2)
let p2: Point<Float> = Point(x: 1.0, y: 2.0)
let p3 = Point(x: 1.0, y: 2.0)
print(p3)

//Constraints
protocol Shape{
    func area() -> Double
}

class Rect: Shape{
    var w: Double
    var h: Double

    init(w:Double, h: Double){
        self.w = w
        self.h = h
    }

    func area() -> Double {
        w * h
    }
}

class Figures<T: Shape>{
    var figures: [T]

    init(figures: [T]){
        self.figures = figures
    }
}

extension Array where Element: Rect {
    func area(){
        forEach { print("area: \($0.area())")}
    }
}

let arr1 = [Rect(w:1, h:1), Rect(w:2, h:2)]
let fig1 = Figures(figures: arr1)
fig1.figures.area()

//associatedtype
protocol Drawable {
    associatedtype Sprite where Sprite: Rect
    func draw(sprite: Sprite)
}

//conditional
protocol Updatable{
    func update()
}

extension Int: Updatable{
    func update(){
        print("update")
    }
}

extension Array: Updatable where Element: Updatable {
    func update(){
        forEach { $0.update()}
    }
}

let arr2 = [1, 2, 3]
1.update()
arr2[0].update()
arr2.update() //x3

//Array
let arr0 = [1, 2, 3] //inference
let arr3: [Int] = [1, 2, 3] //sugar
let arr4 = [Int]() //sugar
let arr5: Array<Int> = [1, 2, 3] //generic
let arr6 = Array<Int>() //generic

//Dictionaries
let dic0 = [1 : "Mike", 2 : "Tom"] //inference
let dic1: [Int : String] = [1 : "Mike", 2 : "Tom"] //sugar
let dic2: [Int : String] = [ : ] //sugar
let dic3: Dictionary<Int, String> = [1 : "Mike", 2 : "Tom"] //generic

//Optionals
enum Optional<T> {
    case some(T)
    case none
}

var op1: Optional<Int> = Optional.some(5)
var op2 = Optional<Int>.some(5)
var op3 = Optional.some(5)
//if op3 == Optional<Int>.none{print("nil")}

var op4: Int? = 5
if op4 == nil {print("nil")}