首页 > 其他分享 >GDExtension-Swift

GDExtension-Swift

时间:2024-04-23 21:13:10浏览次数:16  
标签:bin Godot GDExtension res godot release debug 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

标签:bin,Godot,GDExtension,res,godot,release,debug,Swift
From: https://www.cnblogs.com/Biiigwang/p/18153744/gdextensionswift-z9w1c1

相关文章

  • 集成SwiftLint
    1.pod集成代码pod'SwiftLint'2.添加JS代码如下同时保证将JS放在Sources之前3.cd到项目根目录下,创建yml文件touch.swiftlint.yml4.将规则内容粘贴到yml中#Yamldocreference:#https://github.com/realm/SwiftLint#disable-rules-in-code#https://github.......
  • GDExtension的C++示例
    GDExtension的C++示例本文按照官方文档,进行c++的GDExtension​插件开发,主要进行文档进行复刻,同时对文档中未涉及步骤进行补充什么是GDExtension除了GDScript​和C#​这两种脚本语言外,Godot​引擎可以执行其他编程语言编写的代码。目前有两种方式实现:C++模块与GDExtension简单......
  • swift通过Mirror访问对象所有的存储属性
    Mirror用途:获取到对象的全部属性的key、value。示例:点击查看代码classPerson{privatevarname:Stringprivatevarage:Intvaraddress:StringvarsaySomething:String{return"helloworld"}init(name:String,age:Int,address:Stri......
  • 第一个Swift程序
    要创建第一个Swift项目,请按照以下步骤操作:打开Xcode。如果您没有安装Xcode,可以在AppStore中下载并安装它。在Xcode的欢迎界面上,选择“CreateanewXcodeproject”(创建新Xcode项目)。在模板选择界面上,选择“App”(应用程序)。在应用模板选择界面上,选择“SingleViewApp”(单视......
  • ios 之 netty版本swiftNio(DNS 域名自解析)
    SwiftNio简介用于高性能协议服务器和客户端的事件驱动、无阻塞的网络应用程序框架。SwiftNIO是一个跨平台异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。这就像Netty,但是为Swift写的。Xcode引入swiftNio    在实际写代码前,我......
  • ios 之 netty版本swiftNio(TLSHandler 创建)
    SwiftNio简介用于高性能协议服务器和客户端的事件驱动、无阻塞的网络应用程序框架。SwiftNIO是一个跨平台异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。这就像Netty,但是为Swift写的。Xcode引入swiftNio        在实际写代码前,......
  • swift ui 语法基础
    importSwiftUIstructStudyView:View{@Statevarshow=false//动态改变的变量前面需要声明[@State]varname="字符串123"//静态的变量不需要加[@State]varbody:someView{VStack{Toggle("切换",isOn:$show)//控件......
  • ios 之 netty版本swiftNio(socket创建)
    SwiftNio简介用于高性能协议服务器和客户端的事件驱动、无阻塞的网络应用程序框架。SwiftNIO是一个跨平台异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。这就像Netty,但是为Swift写的。Xcode引入swiftNio        在实际写代码前,......
  • RxSwift的个人笔记
    最近在看电报项目 https://github.com/TelegramMessenger/Telegram-iOS.git发现里面也是使用响应式编程,里面SSignalKit库来做事件触发和监听相关的内容。由于原来主要是用Object-C开发,对于Swift的骚气用法老是消化不良,看了一段时间,有一些明白了些的点需要记录下来。 和RxSwift......
  • 《安富莱嵌入式周报》第335期:大量嵌入式书籍免费下载,CNC电机同步,智能家居比赛作品,EMF2
    周报汇总地址:http://www.armbbs.cn/forum.php?mod=forumdisplay&fid=12&filter=typeid&typeid=104 视频版:https://www.bilibili.com/video/BV151421Q7P4/目录:1、大量嵌入式书籍免费下载,无需注册账号,直接下载2、EMF2024准备的电子胸牌3、CNC电机同步视频4、Hackaday举......