Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

GDExtension 是什么?

前言

GDExtension 是 Godot 专有的技术,可以让引擎在运行时与原生共享库交互。你可以通过它来执行原生代码,无需和引擎一同编译。

备注

GDExtension 不是脚本语言,与 GDScript 无关。

GDExtension 与 C++ 模块的区别

GDExtension 和 C++ 模块都可以用于在 Godot 项目中执行 C 或 C++ 代码。

它们都可以用来将第三方库集成进 Godot。选用哪一个取决于你的需求。

警告

我们的长期目标是,面向早先版本的 GDExtension 在后续的小版本中依然能够运作,但反之则不然。例如,一个面向 Godot 4.2 的 GDExtension 应当在 Godot 4.3 中正常运作,但面向 Godot 4.3 的则不能在 Godot 4.2 中运作。

不过,GDExtension 目前仍是 实验性的,这就意味着我们为了修复严重的 bug 或引入重大的特性,可能会牺牲兼容性。例如,为 Godot 4.0 创建的 GDExtension 与 Godot 4.1 并不兼容(参见 将 GDExtension 更新到 4.1 )。

GDExtension 的优点

Unlike modules, GDExtension doesn't require compiling the engine's source code, making it easier to distribute your work. It gives you access to most of the API available to GDScript and C#, allowing you to code game logic with full control regarding performance. It's ideal if you need high-performance code you'd like to distribute as an add-on in the asset library.

并且:

  • 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 run-time.

  • GDExtension only requires you to compile your library, not the whole engine. That's unlike C++ modules, which are statically compiled into the engine. Every time you change a module, you need to recompile the engine. Even with incremental builds, this process is slower than using GDExtension.

C++ 模块的优点

如果 GDExtension 无法满足要求,我们推荐使用 C++ 模块

  • C++ 模块提供了与引擎更深层次的集成。GDExtension 的访问能力不如静态模块那般深入。

  • 使用 C++ 模块为项目提供额外功能,就可以免去携带原生库文件的麻烦。同样也适用于导出的项目。

备注

If you notice that specific systems are not accessible via GDExtension but are via custom modules, feel free to open an issue on the godot-cpp repository to discuss implementation options for exposing the missing functionality.

支持的语言

Godot 开发者官方支持以下 GDExtension 语言绑定:

备注

There are no plans to support additional languages with GDExtension officially. That said, the community offers several bindings for other languages (see below).

以下绑定是由社区开发并维护的:

备注

这里列出的绑定并不都能用于生产。请在新项目中使用前先做足调查。同时,请确定该绑定与你所使用的 Godot 版本兼容。

版本兼容性

为特定版本 Godot 编译的 GDExtension 插件只保证能够在相同次要版本上可用。例如,为 Godot 4.0 版本编译的 GDExtension 插件只能用于 Godot 4.0、4.0.1、4.0.2……另外 GDExtension 与 Godot 3.x 不兼容。

GDExtension add-ons are also only compatible with engine builds that use the level of floating-point precision the extension was compiled for. This means that if you use a engine build with double-precision floats, the extension must also be compiled for double-precision floats. See 大世界坐标 for details.