GDExtension-Swift

GDExtension-Swift

使用Swift​编写GDExtension​步骤与使用C++​相同,根据官方文档介绍,我们使用一个由社区维护的SwiftGodot​项目。

本文参考复刻社区的教程Meet SwiftGodot,并对遇到的问题进行补充。

资源

原版教程资料:下载实验资料

本例代码仓库:https://github.com/biiigwang/godot-swift-example

**GodotSwift**api文档https://migueldeicaza.github.io/SwiftGodotDocs/documentation/swiftgodot/

GodotSwift​的文档非常详细,遇见问题建议先查看文档

Godot的官方youtube频道有该项目发起者大佬的一个演讲,有兴趣可以观看一下,这个大佬长期使用C#,有好几个商业项目

该大佬还有使用Swift驱动Godot的项目

项目结构

C++​的GDExtension​相同,使用GDExtension​有以下两个核心要素:

  • 不同的语言生成动态库
  • 使用gdextension​声明动态库的入口函数

使用Swift​的GDExtension​的项目结构可以抽象为如下:

godot-swift-project
├── godot-dir
│   ├── bin
│   │   ├── *.gdextension
│   │   ├── ...
│   ├── ...
└── swift-gdextension-dir
    ├── Package.resolved
    ├── Package.swift
    ├── Sources
    └── Tests
  • godot-swift-project​:使用SwiftGodot​项目的Godot​项目根目录

  • godot-dir​:存放Godot​项目及资源

    • bin​:存放GDExtension​的动态库与gdextension​文件
  • swift-gdextension-dir​:存放swift项目

本例使用教程中的资料,结合上面介绍的结构进行对应

抽象目录 实际目录
项目目录 godot-swift-project godot-swift-sample
godot目录 godot-dir SimpleRunner
swift目录 godot-swift-project SimpleRunnerDriver

怎么使用

使用SwiftGodot​需要先编译我们的插件为动态库,再向Godot Editor​声明我们的动态库。
我这里以MacOS​为例进行说明,Linux​,Windows​平台的开发流程相似。

编译

将我们的插件编译为debug​与release​两种版本,debug​库用于调试,release​库用于追踪的游戏使用。

cd SimpleRunnerDriver
# Compile the Swift code in debug mode
# This mode is easy to debug, but it comes with a performance penalty
swift build -c debug
# Compile the Swift code in release mode
swift build -c release

安装动态库与gdextension文件编写

编译好的产物位于SimpleRunnerDriver/.build/debug​与SimpleRunnerDriver/.build/release​,我们需要使用的是两个动态库:

  • libSimpleRunnerDriver.dylib​:我们实际编写的插件
  • libSwiftGodot.dylib​:我们插件所依赖的godot绑定
    动态库的目标位置需要与gdextension文件的编写相对应,关于gdextension的更多内容请参考该文档

现在我们需要编写gdextension​文件
本例中只实际使用了Windows x86_64​,MacOS arm64​两个平台的动态库

注意:Windows平台下处理已上两个库,还需要C:\{your_swift_install_dir}\Swift\runtime-development\usr\bin\*.dll​下其他*.dll

[configuration]
entry_symbol = "swift_entry_point"
compatibility_minimum = 4.2

[libraries]
macos.debug = "res://bin/arm64-apple-macosx-debug/libSimpleRunnerDriver.dylib"
macos.release = "res://bin/arm64-apple-macosx-release/libSimpleRunnerDriver.dylib"
windows.debug.x86_32 = "res://bin/MyFirstGame"
windows.release.x86_32 = "res://bin/MyFirstGame"
windows.debug.x86_64 = "res://bin/x86_64-windows-debug"
windows.release.x86_64 = "res://bin/x86_64-windows-release"
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"



[dependencies]
macos.debug = {"res://bin/arm64-apple-macosx-debug/libSwiftGodot.dylib" : ""}
macos.release = {"res://bin/arm64-apple-macosx-release/libSwiftGodot.dylib" : ""}
windows.debug.x86_64 = {"res://bin/x86_64-windows-debug/libSwiftGodot.dll" : ""}
windows.release.x86_64 = {"res://bin/x86_64-windows-release/libSwiftGodot.dll" : ""}

使用扩展

此刻我们就可以回到Godot​,添加节点时可以搜索到MainLevel​,PlayerController​两个节点了

调试GDExtension

我是用VSCode​进行godot与swift的开发与调试,SwiftGodot​的调试主要参考社区该教程

调试GDExtension​的原理:运行使用godot程序,随后使用lldb​的attach​附加调试模式进行拓展的调试

前置操作(仅限于官网下载的MacOS)

如果你是Windows​,Linux​或者MacOS​下自己编译的Godot​均跳过此步骤

在MacOS中,不能直接调试Godot​需要对签名进行相应的配置。实际上就是修改签名相关配置,对Godot​进行信任,参考该教程Debug in Xcode,假设你是用的Godot​应用位于~/Downloads/Godot.app​,如果不是这个路径你换成自己的

mkdir -p /tmp/Godot
cp -R ~/Downloads/Godot.app /tmp/Godot/
cd /tmp/Godot/
codesign --display --xml --entitlements Godot.entitlements Godot.app
open Godot.entitlements 
# By default Finder will choose Xcode to open .entitlements file.
# You can open and edit it through any other application which can handle xml.
# Add <key>com.apple.security.get-task-allow</key><true/> in the opened Editor Application
# 你可以使用任何文本编辑器打开Godot.entitlements,在其中加入<key>com.apple.security.get-task-allow</key><true/>
codesign -s - --deep --force --options=runtime --entitlements Godot.entitlements Godot.app
cp -Rf ./Godot.app ~/Downloads/Godot.app

上面进行的操作:

  1. 创建临时文件夹​,用于存放我们信任后的Godot.app
  2. 复制你正在使用的Godot.app​至临时文件夹
  3. 对临时文件夹中的Godot.app​进行提权操作
  4. 提权后的Godot.app​替换原版Godot.app

创建调试配置文件launch.json

在项目中创建调试配置文件your/project/path/.vscode/launch.json​,创建调试文件,看起来应该像下面这样

"type": "lldb",
"request": "attach",
"name": "Attach to PID",
"pid": "${command:pickProcess}"

我给出我实际的配置文件作为参考,program​指向godot的可执行文件绝对路径

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Attach",
            "type": "cppdbg",
            "request": "attach",
            "program": "/Applications/Godot.app/Contents/MacOS/Godot",
            "MIMode": "lldb"
        }
    ]
}

当这个文件创建好后,按F5​进入调试会让你选择需要附加进程的PID​,你可以输入godot​进行过滤

  1. 在没有运行在Godot​编辑器中点击Run project​前,在VSCode​中按一下F5​进行调试,过滤godot​记住当前的pid号,这个pid号代表Godot​编辑器进程,我不不附加调试这个
  2. Godot​编辑器中点击Run project
  3. 再次在VSCode​中按一下F5​进行调试,过滤godot​,会有一个新的进程,选择新的进程
  4. 此时你应该可以进入断点了

参考资料

Working in VS Code

Debug in Xcode

Meet SwiftGodot

posted @ 2024-04-23 21:00  biiigwang  阅读(32)  评论(0编辑  收藏  举报