📅  最后修改于: 2021-01-02 09:58:54             🧑  作者: Mango
这些是该语言支持的键盘列表。关键字是保留字(令牌),它们不能用作标识符。也保留了以下各节中列出的运算符(例如in,not和and或in)和内置类型的名称。
键盘在GDScript标记程序中定义,如下所示:
Keyword | Description |
---|---|
If | See if/else/elif. |
else | See if/else/elif. |
elif | See if/else/elif. |
For | See for. |
do | Reserved for future implementation of do…while loops. |
match | See match. |
while | See while. |
case | It booked for the next applications. |
switch | It reserved for future implementation. |
break | It exits the execution of the current for or while loop. |
continue | It skips immediately skips to the next iteration of for or while loop. |
pass | It used where a statement is required syntactically, but the execution of code is undesired, e.g., in empty functions. |
return | Returns a value form a service. |
class | Defines a class. |
is | Tests whether a variable extends with the current quality. |
extends | Explains what class to reach with the current class. |
self | This refers to the current class instance. |
tool | It executes the script in the editor. |
signal | It defines a sign. |
func | It represents a function. |
static | It defines a static function, and Static member variables are not allowed. |
const | Defines a constant. |
enum | It establishes an enum. |
var | It represents a variable. |
on ready | Initializes variables once the Node the script is attached to and its children are modifiable in the editor. |
export | It saves a variable along with the resource it’s connected to and makes it visible and modifiable in the editor. |
set get | It defines setter and getter functions for a variable. |
preload | Preloads a class or variable. |
breakpoint | Editor helper for debugger breakpoints. |
yield | Co-routine support. |
assert | Asserts a condition, logs error on failure. Ignored in non-debug builds. |
remote | Networking RPC annotation. |
master | Networking RPC annotation. |
slave | Networking RPC annotation. |
sync | Networking RPC annotation. |
TAU | TAU constant. |
INF | Infinity is constant. Used for comparisons. |
NAN | NAN (not a number) constant. Used for comparisons. |
PI | PI constant. |
以下是受支持的运算符及其优先级的列表。
Operator | Description |
---|---|
x[index] | Subscription, Highest priority |
x.attribute | Attribute reference |
is | Instance Type Checker |
~ | Bitwise NOT |
-x | Negative |
*,/,% | Multiplication/Division/Remainder Note: The result of these operations depends upon the types of the operands. If both are Integers, then the result will be an Integer. That means 1/10 returns 0 instead of 0.1. If at least one of the operands is a float, then the result is a float: float (1)/10 or 1.0/10 return both 0.1. |
+,- | Addition/Subtraction |
<<,>> | Bit shifting |
& | Bitwise AND |
^ | Bitwise XOR |
| | Bitwise OR |
<,>,==,!=,>=,<= | Comparisons |
in | Content Test |
!, not | Boolean NOT |
and,&& | Boolean AND |
or,|| | Boolean OR |
If x else | Ternary if/else |
=,+=,-=,*=,/=,%=,&=,|= | Assignment, Lowest Priority |
Literal | Type |
---|---|
45 | Base 10 integer |
0x8F51 | Base 16 (hex) integer |
3.14,58.1e-10 | Floating-point number(real) |
“Hello”,” Hi” | Strings |
“”” Hello””” | Multiline string |
@” Node/Label” | Node path or StringName |
从#(哈希)到行尾的所有内容都将被忽略,并被视为注释。
# This is a comment.
可以在文本块的开头和结尾使用“”(多行三引号)来创建多行注释,请注意,这会创建一个字符串;因此,在编译脚本时不会删除它。
""" Everything on
these lines is considered
a comment."""
内置类型是堆栈分配的,它们作为值传递。这意味着将在每个分配上或在将其作为函数的参数移动时创建一个副本。唯一的例外是Array和Dictionaries ,它们是通过引用给出的,因此它们是共享的。 (尽管不像PoolByteArray这样的PoolArray,它们也作为值传递,所以在决定使用哪个时要考虑这一点!)
GDScript中的变量可以分配给几种内置类型。
空值
它是一个空数据类型,不包含任何信息,也不能分配任何其他值。
布尔
布尔数据类型仅包含true或false 。
整数
此数据类型只能包含整数(负数和正数)。
浮动
它用于提供浮点值(实数)。
Unicode格式的字符序列。字符串可以包含标准的C转义序列。 GDScript支持格式字符串printf功能。
矢量2
包含x和y字段的2D向量类型。它也可以作为数组访问。
矩形2
包含两个向量字段的2D向量类型: position和size 。或者包含一个end字段,它是position + size 。
矢量3
包含x , y和z字段的3D矢量类型。也可以将其作为数组进行访问。
Transfrom2D
在2D中,使用3×2变换矩阵。
飞机
归一化形式的3D平面类型,包含标准矢量场和d标量距离。
夸脱
四元数是用于表示3D旋转的数据类型。对于内插旋转很有用。
AABB
轴对齐的边界框(或3D框)包含两个向量字段: position和size 。或者包含一个end字段,该字段为正+ size 。
基础
用于3D旋转和缩放的3×3矩阵。它包含三个向量字段( x , y和z ),也可以作为3D向量的数组进行访问。
转变
3D变换包含基础字段基础和Vector3字段原点。
数据类型包含r,g,b和字段。也可以将其作为h , s和v进行访问以获取色相/饱和度/值。
到主要在场景系统中使用的节点的编译路径。可以轻松地将其分配给字符串或从中分配。
资源ID( RID )。服务器使用通用RID引用不透明数据。
任何非内置类型的基类。
从索引索引0开始对数组进行索引。从Godot 2.1开始,索引可能像Python一样为负,从末尾开始计数。任意对象类型的通用序列,包括其他数组或字典。该数组可以动态调整大小。
var arr = []
arr = [1, 2, 3]
var b = arr[1] # This is 2.
var c = arr[arr.size() - 1] # This is 3.
var d = arr[-1] # Same as the previous line, but shorter.
arr[0] = "Hi!" # Replacing value 1 with "Hello".
arr.append(4) # Array is now ["Hello", 2, 3, 4].
GDScript数组在内存中线性分配以提高速度。但是,包含数万个元素的大型数组可能会导致内存碎片。如果有问题,可以使用特定类型的阵列。这些仅接受单个数据类型。它们避免了内存碎片,还使用了更少的内存,但是它们是原子的,并且运行速度比通用数组慢。建议将它们用于大型数据集。
关联容器,其中包含唯一键引用的值。
var d = {4: 5, "A key": "A value", 28: [1, 2, 3]}
d["Hi!"] = 0
d = {
22: "value",
"some_key": 2,
"other_key": [2, 3, 4],
"more_key": "Hello"
}
还支持Lua样式的表语法。 Lua风格使用=代替:,并且不使用引号标记字符串键(这样写起来稍少)。以这种形式写的键不能以数字开头。
var d = {
test22 = "value",
some_key = 2,
other_key = [2, 3, 4],
more_key = "Hello"
}
要将键添加到现有字典,请像现有键一样对其进行访问并分配给它:
var d = {} # Create an empty Dictionary.
d.waiting = 14 # Add String "Waiting" as a key and assign the value 14 to it.
d[4] = "hello" # Add integer 4 as a key and assign the String "hello" as its value.
d["Godot"] = 3.01 # Add String "Godot" as a key and assign the value 3.01 to it.
变量可以作为类成员存在,也可以作为函数局部存在。它们是使用var键盘创建的,可以选择在初始化时为其分配值。
var a # Data type is 'null' .
var b = 5
var c = 3.8
var d = b + c # Variables are always initialized here.
常量类似于变量,但必须是常量或常量表达式,并且必须在初始化时分配。
const H = 5
const G = Vector2(20, 20)
const C = 10 + 20 # Constant expression.
const D = Vector2(20, 30).x # Constant expression: 20
const E = [1, 2, 3, 4][0] # Constant expression
const F = sin(20) # sin() is used in constant expressions.
const G = x + 20 # Invalid; It is not a constant expression!
枚举是常量的简写,如果我们要为某个常量分配连续的整数,则枚举非常有用。
如果我们将名称传递给枚举,则还将所有值放入名称的常量字典中。
enum {TILE_FLOOR, TILE_BRICK, TILE_SPIKE, TILE_TELEPORT}
# Is the same as:
const TILE_FLOOR = 0
const TILE_BRICK = 3
const TILE_SPIKE = 4
const TILE_TELEPORT = 5
enum State {STATE_JUMP, STATE_IDLE = 5, STATE_SHOOT}
# Is the same as:
const STATE_ JUMP= 0
const STATE_IDLE = 7
const STATE_SHOOT = 8
const State = {STATE_IDLE = 0, STATE_JUMP = 7, STATE_SHOOT = 8}
函数始终属于一个类。变量查找的作用域优先级:local-class member-global。自变量始终可用,并且作为访问类成员的选项提供,但并非总是必需的(与Python不同,它不能作为函数的第一个参数发送)。
func my_function(x, y):
print(x)
print(y)
return x + y # Return is optional; without it 'null' is returned.
它可以随时返回。默认返回值为null 。