GDExtension 是什么?
前言
GDExtension is a Godot-specific technology that lets the engine interact with native shared libraries at runtime. You can use it to run native code without compiling it with the engine.
备注
GDExtension 不是脚本语言,与 GDScript 无关。
GDExtension 与 C++ 模块的区别
GDExtension 和 C++ 模块都可以用于在 Godot 项目中执行 C 或 C++ 代码。
它们都可以用来将第三方库集成进 Godot。选用哪一个取决于你的需求。
警告
GDExtension is currently experimental, which means that we may break compatibility in order to fix major bugs or include critical features.
GDExtension 的优点
与模块不同,GDExtension 不需要编译引擎源码,可以让你的作品更易于分发。GDExtension 可以让你访问 GDScript、C# 中的大多数 可用的API,让你在编写游戏逻辑时能够完全控制性能,是你在资产库中以插件形式分发高性能代码的理想选择。
并且:
GDExtension 并不仅限于 C 和 C++。得益于第三方绑定,你也可以在很多其他语言中使用。
You can use the same compiled GDExtension library in the editor and exported project. With C++ modules, you have to recompile all the export templates you plan to use if you require its functionality at runtime.
GDExtension 只需编译你自己的库,而不需要编译整个引擎。C++ 模块则与此不同,是会静态编译进引擎的。每修改一次模块,你就需要重新编译一次引擎。即使使用增量构建,这一过程也比编译 GDExtension 要慢。
C++ 模块的优点
如果 GDExtension 无法满足要求,我们推荐使用 C++ 模块:
C++ 模块提供了与引擎更深层次的集成。GDExtension 的访问能力不如静态模块那般深入。
使用 C++ 模块为项目提供额外功能,就可以免去携带原生库文件的麻烦。同样也适用于导出的项目。
备注
如果发现某些特定功能在 GDExtension 中不可用,但在自定义模块中却可以使用,欢迎在 godot-cpp 仓库上提出新议题,探讨如何将这些缺失的功能公开化。
支持的语言
Godot 开发者官方支持以下 GDExtension 语言绑定:
C++ (教程)
备注
目前官方并未计划让 GDExtension 支持更多的编程语言,不过社区已经提供了多种其他语言的绑定(详见下文)。
以下语言绑定是由社区开发与维护的:
备注
这里所列出的绑定并不都能用于生产。在决定采用其中任何一种语言绑定开始项目之前,请务必对这些语言绑定进行全面深入的研究。同时,请确定该绑定与你所使用的 Godot 版本是否兼容。
版本兼容性
Usually, GDExtensions targeting an earlier version of Godot will work in later minor versions, but not vice-versa. For example, a GDExtension targeting Godot 4.2 should work just fine in Godot 4.3, but one targeting Godot 4.3 won't work in Godot 4.2.
For this reason, when creating GDExtensions, you may want to target the lowest version of Godot that has the features you need, not the most recent version of Godot. This can save you from needing to create multiple builds for different versions of Godot.
不过,GDExtension 目前仍是 实验性的,这就意味着我们为了修复严重的 bug 或引入重大的特性,可能会牺牲兼容性。例如,为 Godot 4.0 创建的 GDExtension 与 Godot 4.1 并不兼容(参见《将 GDExtension 更新到 4.1》)。
GDExtensions are also only compatible with engine builds that use the same
level of floating-point precision the extension was compiled for. This means
that if you use an engine build with double-precision floats, the extension must
also be compiled for double-precision floats and use an extension_api.json
file generated by your custom engine build. See 大世界坐标
for details.
Generally speaking, if you build a custom version of Godot, you should generate an
extension_api.json
from it for your GDExtensions, because it may have some differences
from official Godot builds.