Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

脚本语言

本课将向你介绍 Godot 中可用的脚本语言。你将了解每个选项的优点和缺点。在下一部分,你将使用 GDScript 编写你的第一个脚本。

脚本附加到节点并扩展其行为。这意味着脚本继承所附加节点的全部函数和属性。

例如,以一个 Camera2D 节点跟随一艘船的游戏为例。Camera2D 节点默认跟随其父节点。想象一下,当玩家受到伤害时,您希望相机震动。由于此功能未内置在 Godot 中,因此您可以在该 Camera2D 节点上附加脚本并对抖动进行编程。

../../_images/scripting_camera_shake.gif

可用的脚本语言

Godot offers four gameplay programming languages: GDScript, C#, and, via its GDExtension technology, C and C++. There are more community-supported languages, but these are the official ones.

You can use multiple languages in a single project. For instance, in a team, you could code gameplay logic in GDScript as it's fast to write, and use C# or C++ to implement complex algorithms and maximize their performance. Or you can write everything in GDScript or C#. It's your call.

我们提供这种灵活性以满足不同游戏项目和开发者的需求。

我应该使用哪种语言?

如果你是初学者,我们推荐从 GDScript 入手。这门语言是我们针对 Godot 和游戏开发者的需求制作的。语法简单直白,与 Godot 结合得最为紧密。

../../_images/scripting_gdscript.png

使用 C# 时,你需要使用 VSCode 或 Visual Studio 等外部编辑器。虽然对 C# 支持目前已经成熟,但相对 GDScript 而言,能找到的学习资源会比较少。因此,我们主要推荐已经熟悉 C# 语言的用户去使用 C#。

让我们来看看各个语言的特性,以及优缺点。

GDScript

GDScript 是一门面向对象指令式编程语言,专为 Godot 构建。是游戏开发者为游戏开发者制作的,目的是节省编写游戏代码的时间。它的特性包括:

  • 简单的语法,让文件更短。

  • 极快的编译和加载速度。

  • 紧密的编辑器集成,包括节点、信号、所附加场景更多信息的代码补全。

  • 内置向量和变换类型,让海量线性代数计算更高效,游戏必备。

  • 支持多线程,与静态类型的语言一样高效。

  • 没有垃圾回收,因为最终会影响游戏的开发。引擎会默认进行引用计数,在大多数情况下为你管理内存,但你也可以在需要时自行控制内存。

  • 缓类型。变量默认是动态类型,但你也可以使用类型提示来做强类型检查。

因为 GDScript 用缩进来做代码块的结构化,所以它看上去像 Python,不过实际上这两者的原理截然不同。GDScript 是从 Squirrel、Lua、Python 等诸多语言中得到的灵感。

备注

我们为什么不直接使用 Python 或者 Lua?

很多年前,Godot 使用过 Python,后来也用过 Lua。这两个语言的集成花费了大量精力,并且还存在局限性。例如,在 Python 中做多线程支持是非常大的挑战。

开发专属语言不会花费更多的时间,我们还能针对游戏开发者的需求去量体裁衣。我们现在在做性能优化工作,以及用第三方语言难以实现的特性。

.NET / C#

As Microsoft's C# is a favorite amongst game developers, we officially support it. C# is a mature and flexible language with tons of libraries written for it. We were able to add support for it thanks to a generous donation from Microsoft.

../../_images/scripting_csharp.png

C# 在性能和易用性之间提供了不错的权衡,不过你应该了解它的垃圾回收器。

备注

You must use the .NET edition of the Godot editor to script in C#. You can download it on the Godot website's download page.

Since Godot uses .NET 6, in theory, you can use any third-party .NET library or framework in Godot, as well as any Common Language Infrastructure-compliant programming language, such as F#, Boo, or ClojureCLR. However, C# is the only officially supported .NET option.

备注

GDScript 代码本身的执行是没有 C# 或 C++ 等编译型语言快的。然而,大多数脚本代码都是在调用引擎中的 C++ 代码快速算法。在许多情况下,使用 GDScript、C#、C++ 编写游戏逻辑并不会有显著的性能区别。

注意

Projects written in C# using Godot 4.x currently cannot be exported to Android, iOS and web platforms. To use C# on those platforms, use Godot 3 instead.

C++ via GDExtension

GDExtension allows you to write game code in C++ without needing to recompile Godot.

../../_images/scripting_cpp.png

我们在内部使用 C API 桥接,得益于此,你可以使用任意版本的语言,也可以混用由不同厂牌、不同版本的编译器所生成的共享库。

GDExtension is the best choice for performance. You don't need to use it throughout an entire game, as you can write other parts in GDScript or C#.

When working with GDExtension, the available types, functions, and properties closely resemble Godot's actual C++ API.

总结

脚本是包含附加到节点以扩展其功能的代码文件。

Godot supports four official scripting languages, offering you flexibility between performance and ease of use.

你可以混合语言,例如,用 C 或 C++ 实现高要求的算法,用 GDScript 或 C# 编写大部分的游戏逻辑。