Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
GDScript 参考¶
GDScript 是一种高级面向对象的指令式编程语言,使用渐进类型,专为 Godot 构建。GDScript 的语法基于缩进,与 Python 等语言类似。它的目标是针对 Godot 引擎优化,与 Godot 引擎紧密集成,从而为内容的创建与继承提供灵活的手段。
GDScript 是完全独立于 Python 存在的,没有继承或扩展关系。
历史¶
备注
关于 GDScript 历史的文档已移至常见问题。
GDScript 示例¶
有些人了解语法后可以更好地学习,所以这里给出一个GDScript的简单示例。
# Everything after "#" is a comment.
# A file is a class!
# (optional) icon to show in the editor dialogs:
@icon("res://path/to/optional/icon.svg")
# (optional) class definition:
class_name MyClass
# Inheritance:
extends BaseClass
# Member variables.
var a = 5
var s = "Hello"
var arr = [1, 2, 3]
var dict = {"key": "value", 2: 3}
var other_dict = {key = "value", other_key = 2}
var typed_var: int
var inferred_type := "String"
# Constants.
const ANSWER = 42
const THE_NAME = "Charly"
# Enums.
enum {UNIT_NEUTRAL, UNIT_ENEMY, UNIT_ALLY}
enum Named {THING_1, THING_2, ANOTHER_THING = -1}
# Built-in vector types.
var v2 = Vector2(1, 2)
var v3 = Vector3(1, 2, 3)
# Functions.
func some_function(param1, param2, param3):
const local_const = 5
if param1 < local_const:
print(param1)
elif param2 > 5:
print(param2)
else:
print("Fail!")
for i in range(20):
print(i)
while param2 != 0:
param2 -= 1
match param3:
3:
print("param3 is 3!")
_:
print("param3 is not 3!")
var local_var = param1 + 3
return local_var
# Functions override functions with the same name on the base/super class.
# If you still want to call them, use "super":
func something(p1, p2):
super(p1, p2)
# It's also possible to call another function in the super class:
func other_something(p1, p2):
super.something(p1, p2)
# Inner class
class Something:
var a = 10
# Constructor
func _init():
print("Constructed!")
var lv = Something.new()
print(lv.a)
如果你以前有使用C, C++或C#之类的静态类型语言的经验, 但以前从未使用过动态类型的语言, 建议你阅读此教程: GDScript:动态语言入门.
语言¶
下面是 GDScript 的一些概述。有关 GDScript 更为详细的信息,如哪些方法可用于数组或其他对象,可以在链接的类描述中查找到。
标识符¶
任何仅限于字母字符( a
到 z
和 A
到 Z
), 数字( 0
到 9
)和 _
的字符串都可以作为标识符. 此外, 标识符不能以数字开头. 标识符区分大小写( foo
和 FOO
是不同的).
标识符也允许包含 UAX#31 所提供的部分 Unicode 字符,也就是说,你也可以将非英文字符作为标识符使用,但 Unicode 字符中易与 ASCII 字符混淆的字符以及颜文字则是无法作为标识符使用的。
关键字¶
以下是该语言支持的关键字列表。关键字由于是保留字(记号),因此不能用作标识符。操作符(如 in
、not
、and
、or
)及下列内置类型的名称也是保留字。
如果你想深入了解关键字,则可在 GDScript 词法分析器中找到对于关键字的定义。
关键字 |
描述 |
---|---|
if |
见 if/else/elif. |
elif |
见 if/else/elif. |
else |
见 if/else/elif. |
for |
见 for. |
while |
见 while. |
match |
见 match. |
break |
退出当前 |
continue |
立即跳到 |
pass |
语法上要求在不希望执行代码的语句中使用,例如在空函数中使用。 |
return |
从函数返回一个值. |
class |
Defines an inner class. See Inner classes. |
class_name |
Defines the script as a globally accessible class with the specified name. See Registering named classes. |
extends |
定义当前类继承什么类。 |
is |
检测变量是否继承自给定的类,或检测该变量是否为给定的内置类型。 |
in |
Tests whether a value is within a string, array, range, dictionary, or node. When used with |
as |
如果可能,则将值转换为给定类型。 |
self |
引用当前类实例。 |
signal |
定义信号。 |
func |
定义函数。 |
static |
将一个函数声明为静态函数,或将一个成员变量声明为静态成员变量。 |
const |
定义常量。 |
enum |
定义枚举。 |
var |
定义变量。 |
breakpoint |
用来设置脚本编辑器辅助调试断点的关键字。与在脚本编辑器每行最左侧点击红点创建断点不同, |
preload |
预加载一个类或变量. 请参见 类作为资源. |
await |
等待信号或协程完成。见等待信号和协程。 |
yield |
以前的版本中用于协程,现保留为关键字,方便迁移。 |
assert |
断言条件,如果失败则记录错误。在非调试版本中被忽略。参见 Assert 关键字。 |
void |
用于代表函数不返回任何值。 |
PI |
PI 常量。 |
TAU |
TAU 常量。 |
INF |
无穷常量。用于比较和计算结果。 |
NAN |
NAN(非数)常量,用作计算后不可能得到的结果。 |
运算符¶
下面是支持的运算符列表及其优先级(越上面越高).
运算符 |
描述 |
---|---|
|
分组(优先级最高) 括号其实不是运算符,但是能够让你显式指定运算的优先级。 |
|
下标 |
|
属性引用 |
|
函数调用 |
|
|
|
类型检查 另见 is_instance_of() 函数。 |
|
幂 将 注意:在 GDScript 中, |
|
按位取反 |
+x -x |
一致 / 求负 |
x * y x / y x % y |
乘法/除法/余数
注意:这些运算符的行为与 C++ 一致,对于来自 Python、JavaScript 等语言的用户可能存在意外的行为。详情见表后。 |
x + y x - y |
加法(或连接)/减法 |
x << y x >> y |
位移位 |
|
按位与 |
|
按位异或 |
|
按位或 |
x == |