# GDScript

**GDScript** 是为 [Godot](https://www.edgepedia.org/zh/godot-game-engine) 游戏引擎构建的一种高级、面向对象、命令式且渐进类型的编程语言。它采用类似 [Python](https://www.edgepedia.org/zh/python-programming-language) 的基于缩进的语法，但完全独立于 Python，并非基于 Python。其目标是针对 Godot 进行优化并与之紧密集成，为内容创作和集成提供灵活性。<sup>[1](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 语言类型 | 高级、面向对象、命令式、渐进类型；默认动态类型，可选类型提示<sup>[1](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html)</sup><sup> • </sup><sup>[2](https://docs.godotengine.org/en/4.5/tutorials/scripting/gdscript/static_typing.html)</sup> |
| 语法 | 基于缩进，外观类似 Python；受 Squirrel、Lua 和 Python 启发<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup> |
| 官方地位 | Godot 官方游戏玩法语言之一，与 C# 以及通过 GDExtension 使用的 C/C++ 并列<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup> |
| 静态类型 | 随 Godot 3.1 引入；当操作数类型在编译时已知时，带类型的代码可使用优化的操作码<sup>[4](https://godotengine.org/article/optional-typing-gdscript/)</sup><sup> • </sup><sup>[2](https://docs.godotengine.org/en/4.5/tutorials/scripting/gdscript/static_typing.html)</sup> |
| 内存模型 | 无垃圾回收；引擎默认通过引用计数管理内存<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup> |
| 性能 | 慢于编译型 C# 或 C++，但大多数脚本代码调用的是引擎内以 C++ 编写的函数；初步基准测试表明，在某些简单场景下 C# 约为 GDScript 的 4 倍<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup><sup> • </sup><sup>[5](https://docs.godotengine.org/en/3.2/getting_started/scripting/c_sharp/c_sharp_basics.html)</sup> |
| 标识符 | 接受 Unicode 标准附件 #31 中的大多数字符，包括苏美尔-阿卡德楔形文字；易混淆字符和表情符号被排除在外<sup>[1](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html)</sup> |

## 历史与设计

在 GDScript 形成当前形态之前，Godot 的脚本层经历过更早的设计。多年前引擎使用 Python，后来使用 Lua，两次集成都需要大量工作且存在严重局限；例如，Python 的线程支持就是一大挑战。他们认定，专门构建的脚本语言能更好地利用 Godot 的特定架构，而 GDScript 及其解释器最终被证明比仅仅绑定 Lua 或 Squirrel 所需的代码更为紧凑，同时提供相同的功能。<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup><sup> • </sup><sup>[6](https://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_basics.html?highlight=setget)</sup>

一次重大的语言变革随 Godot 3.1 到来，引入了可选的类型提示。选择这种语法是因为 GDScript 已经与 Python 相似，且这种风格易于集成到语言解析器中，同时保持可选性。<sup>[4](https://godotengine.org/article/optional-typing-gdscript/)</sup>

## 语言特性

一个 GDScript 文件就是一个类。脚本附加到节点上并扩展其行为，继承所附加节点的所有函数和属性；例如，附加在 Camera2D 节点上的脚本可以在玩家受到伤害时添加镜头震动反应。<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup> 该语言提供成员变量、常量、枚举、内部类、`match` 语句、信号，以及用于等待信号或[协程](https://www.edgepedia.org/zh/coroutine)的 `await` 关键字。<sup>[1](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html)</sup>

**内置类型。** 基本类型包括 `bool`、`int`（存储为 64 位值，等价于 C++ 中的 `int64_t`）、`float`（存储为 64 位 `double`）、`String`、`StringName` 和 `NodePath`。Godot 还提供向量和变换类型，如 `Vector2`、`Vector3`、`Quaternion` 和 `Transform3D`，文档称这些类型对于游戏中典型的大量线性代数运算而言是高效的。请注意，`Vector2`、`Vector3` 和 `PackedFloat32Array` 存储的是 32 位单精度浮点数，尽管标量 `float` 类型是 64 位的。基本类型在栈上分配并按值传递；`Object`、`Array`、`Dictionary` 和打包数组按引用传递。<sup>[1](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html)</sup><sup> • </sup><sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup>

**注解。** 以 `@` 开头的行用于修饰声明。`@export` 将值暴露给编辑器检查器，`@onready` 将成员变量的初始化推迟到调用 `_ready()` 时，`@export_range(1, 100, 1, "or_greater")` 用面向编辑器的元数据约束导出值。<sup>[1](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html)</sup>

**运算符细节。** 当 `/` 的两个操作数都是整数时执行整数除法，因此 `5 / 2 == 2`，而非 `2.5`。`%` 运算符仅适用于整数；浮点数使用 `fmod()`，且对于负值两者都使用截断而非向下取整，因此存在 `posmod()` 和 `fposmod()` 用于数学余数。所有二元运算符都是左结合的，包括 `**`，因此 `2 ** 2 ** 3` 等于 `(2 ** 2) ** 3`。<sup>[1](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html)</sup>

## 静态类型

GDScript 默认是动态类型的，但可以为变量、常量、函数、参数和返回类型添加类型提示：`var health: int`、`func sum(a: float = 0.0, b: float = 0.0) -> float`。只写冒号而不写类型，如 `var damage := 10.5`，会让 Godot 推断类型。[静态类型](https://www.edgepedia.org/zh/static-typing)让引擎无需运行代码就能检测更多错误，改进自动补全和文档，并在脚本编辑器中将类型安全的行标记为绿色。<sup>[2](https://docs.godotengine.org/en/4.5/tutorials/scripting/gdscript/static_typing.html)</sup>

带类型的 GDScript 还通过在编译时已知操作数和参数类型时使用优化的操作码来提升性能，文档指出还计划了 JIT/AOT 编译等进一步优化。支持带类型的数组（`Array[int]`），并且自 Godot 4.2 起支持带类型的字典（`Dictionary[String, int]`）和带类型的 `for` 循环变量；不支持嵌套集合类型，如 `Array[Array[int]]`。<sup>[2](https://docs.godotengine.org/en/4.5/tutorials/scripting/gdscript/static_typing.html)</sup>

动态风格和类型化风格可以在同一个项目中共存，但文档建议坚持一种风格以保持一致性。可以启用 `UNTYPED_DECLARATION` 等警告来在整个项目中强制静态类型。<sup>[2](https://docs.godotengine.org/en/4.5/tutorials/scripting/gdscript/static_typing.html)</sup>

## 性能

GDScript 代码本身的执行速度不如编译型 C# 或 C++。引擎自身的指导是，这通常没有看起来那么重要，因为大多数脚本代码调用的是引擎内部以 C++ 实现的快速算法函数，因此在许多情况下，用 GDScript、C# 或 C++ 编写游戏逻辑没有显著的性能影响。<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup> 早期官方文档指出，在某些简单场景下 C# 的性能约为 GDScript 的 4 倍，但仍处于同一数量级。<sup>[5](https://docs.godotengine.org/en/3.2/getting_started/scripting/c_sharp/c_sharp_basics.html)</sup>

社区基准测试表明，情况在很大程度上取决于工作负载。特文特大学的一篇学士论文测量了 GDScript 中动态类型与静态类型在内置类型和用例上的执行时间。<sup>[7](https://essay.utwente.nl/107857/1/Goryachkin_BA_EEMCS.pdf)</sup> 一项关于 Godot 中脚本语言的同行评审比较分析，绘图对比了 GDScript 与 C# 在各项实现任务中的表现。<sup>[8](https://ph.pollub.pl/index.php/jcsi/article/download/5428/4522/25830)</sup>

在速度最重要的地方，Godot 自己的建议是使用 GDExtension，它允许使用 C++ 代码而无需重新编译引擎，被描述为性能方面的最佳选择；项目可以混合使用多种语言，用 C 或 C++ 编写要求高的算法，用 GDScript 或 C# 编写大多数游戏逻辑。<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup>

## 实践中的 GDScript 与 C# 对比

官方文档推荐初学者使用 GDScript：它专为 Godot 打造，语法轻量，提供最紧密的编辑器集成，可为节点和信号提供代码补全，并且编译和加载时间快。C# 主要推荐给已经掌握它的开发者，因为它需要外部编辑器（如 VSCode 或 [Visual Studio](https://www.edgepedia.org/zh/visual-studio)），且 Godot 专属的学习资源较少。<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup>

平台支持是一个具体差异。使用 Godot 4 的 C# 编写的项目无法导出到 Web 平台；Android 和 iOS 支持随 Godot 4.2 到来，但仍属实验性，且存在一些限制。<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup> C# 带来更广泛的 .NET 库生态系统和垃圾回收器，而 GDScript 完全避免垃圾回收，因为它会干扰游戏开发，转而依赖引用计数。<sup>[3](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)</sup>

迭代速度有利于 GDScript。当一位开发者在 Godot 和 Unity 中构建同一款游戏时，Godot 编译脚本耗时 0.31 秒，而 Unity 编译 C# 耗时 15.4 秒，且 Godot 在加载和导出项目方面也更快；两个引擎在完成的游戏中都达到了他设定的 60 fps 目标。<sup>[9](https://www.creativebloq.com/3d/video-game-design/unity-or-godot-this-dev-made-the-same-game-twice-to-test-them-both)</sup>

## Unicode 标识符

GDScript 标识符可以包含 Unicode 标准附件 #31 所涵盖的大多数字符，这允许用英语以外的语言编写标识符名称。被视为与 ASCII 字符易混淆的字符以及表情符号不被允许。在实践中，这延伸到了历史文字：独立开发者 SteinMakesGames 公开指出，苏美尔-阿卡德楔形文字标识符是合法的 GDScript，解析器允许这一特性，而 C# 等语言则不允许；Python、JavaScript ES6+ 和 Rust 提供类似的 Unicode 标识符支持。<sup>[1](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html)</sup><sup> • </sup><sup>[10](https://www.pcgamer.com/gaming-industry/godot-supports-coding-in-sumero-akkadian-cuneiform-if-you-needed-another-reason-to-switch-game-engines/)</sup>

## References

1. [GDScript reference, God Engine (stable) documentation](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html)
2. [Static typing in GDScript, Godot Engine (4.5) documentation](https://docs.godotengine.org/en/4.5/tutorials/scripting/gdscript/static_typing.html)
3. [Scripting languages, Godot Engine (4.6) documentation](https://docs.godotengine.org/en/4.6/getting_started/step_by_step/scripting_languages.html)
4. [Optional typing in GDScript, Godot Engine](https://godotengine.org/article/optional-typing-gdscript/)
5. [C# basics, Godot Engine (3.2) documentation](https://docs.godotengine.org/en/3.2/getting_started/scripting/c_sharp/c_sharp_basics.html)
6. [GDScript, Godot Engine (3.0) documentation](https://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_basics.html?highlight=setget)
7. [Dynamic vs Static Typing Performance for Built-In Types in GDScript in the Godot Game Engine (Goryachkin, University of Twente)](https://essay.utwente.nl/107857/1/Goryachkin_BA_EEMCS.pdf)
8. [Comparative analysis of the implementation performance using selected scripting languages in the Godot game engine, JCSI](https://ph.pollub.pl/index.php/jcsi/article/download/5428/4522/25830)
9. [Unity or Godot Engine? This dev made the same game twice to test both, Creative Bloq](https://www.creativebloq.com/3d/video-game-design/unity-or-godot-this-dev-made-the-same-game-twice-to-test-them-both)
10. [Godot natively supports coding in Sumero-Akkadian Cuneiform, PC Gamer](https://www.pcgamer.com/gaming-industry/godot-supports-coding-in-sumero-akkadian-cuneiform-if-you-needed-another-reason-to-switch-game-engines/)

---
*Initially written Oct 15, 2026 · Edited: Oct 25, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/gdscript

English original: https://www.edgepedia.org/gdscript
