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 專有的技術,可以讓引擎在運作時與原生`共用庫 <https://zh.wikipedia.org/zh-cn/%E5%87%BD%E5%BC%8F%E5%BA%AB#%E5%85%B1%E4%BA%AB%E5%BA%93>`__互動。你可以通過它來執行原生程式碼,無需和引擎一同編譯。

備註

GDExtension *不是*指令碼語言,與 GDScript 無關。

GDExtension 與 C++ 模組的區別

支援將 C++ 模組 靜態連結到引擎二進位檔中。

它們都可以用來將協力廠商庫集成進 Godot。選用哪一個取決於你的需求。

警告

我們的長期目標是針對早期版本 Godot 的 GDExtensions 將在以後的次要版本中工作,但反之則不然。例如,針對 Godot 4.2 的 GDExtension 在 Godot 4.3 中應該可以正常運作,但針對 Godot 4.3 的 GDExtension 在 Godot 4.2 中則無法運作。

然而,GDExtension 目前處於*實驗階段*,這意味著我們可能會破壞相容性以修復主要錯誤或包含關鍵功能。例如,為 Godot 4.0 建立的 GDExtension 與 Godot 4.1 不相容(請參閱:ref:updating_your_gdextension_for_godot_4_1)。

GDExtension 的優點

與模組不同,GDNative 不需要編譯引擎的源碼,可以使你的作品更易於分發。它可以讓你存取 GDScript、C# 使用的大多數 API,讓你在編寫遊戲邏輯時能夠完全控制性能。這是你在:ref:`素材庫 <doc_what_is_assetlib>`中以外掛程式形式分發高性能程式碼的理想選擇。

並且:

  • GDExtension 並不僅限於 C 和 C++。得益於:ref:協力廠商綁定 <doc_what_is_gdnative_third_party_bindings>,你也可以在很多其他語言中使用。

  • 你可以在編輯器和匯出專案中使用相同的編譯後的 GDNative 庫。而對於 C++ 模組,如果你想在運作時使用相關功能,就必須重新編譯所有想要使用的匯出範本。

  • GDNative 只需編譯你自己的庫,而不是整個引擎。C++ 模組則與此不同,是會靜態編譯進引擎的。每修改一次模組,你就需要重新編譯一次引擎。即使使用差異量建構,這一過程也比 GDNative 要慢。

C++ 模組的優點

支援將 C++ 模組 靜態連結到引擎二進位檔中。

  • C++ modules provide deeper integration into the engine. GDExtension's access is not as deep as static modules.

  • 使用 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 語言綁定:

備註

沒有官方支援更多 GDNative 語言的計畫。不過社區提供了不少其他語言的綁定(見下文)。

以下綁定是由社區開發並維護的:

備註

這裡列出的綁定並不都能用於生產。請在新專案中使用前先做足調查。同時,請確定該綁定與你所使用的 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.