godot游戏引擎自学入门笔记--GDScript语言基础,官方文档翻译(九)

声明和控制流程

语句是标准的,可以是赋值、函数调用、控制流结构等(见下面)。; 作为语句分隔符是完全可选的。

if/else/elif

简单的条件是通过使用 if/else/elif 语法创建的。条件的括号是允许的,但不是必需的。考虑到基于表的缩进的性质,可以使用 elif 而不是 else/if 来维持缩进的级别。

if [expression]:
    statement(s)
elif [expression]:
    statement(s)
else:
    statement(s)

简短陈述可以写在与条件相同的行上:

if 1 + 1 == 2: return 2 + 2
else:
    var x = 3 + 3
    return x

有时您可能希望基于布尔表达式分配不同的初始值。在这种情况下,ternary-if表达式派上用场:

var x = [value] if [expression] else [value]
y += 3 if y < 10 else -1
while

简单的循环是使用 while 语法创建的。可以使用 break 来中断循环,或者使用 continue 来继续:

while [expression]:
    statement(s)
for

要遍历一个范围(如数组或表),使用 for 循环。在数组上迭代时,当前数组元素存储在循环变量中。在遍历字典时, index 存储在循环变量中。

for x in [5, 7, 11]:
    statement # Loop iterates 3 times with 'x' as 5, then 7 and finally 11.

var dict = {"a": 0, "b": 1, "c": 2}
for i in dict:
    print(dict[i])

for i in range(3):
    statement # Similar to [0, 1, 2] but does not allocate an array.

for i in range(1,3):
    statement # Similar to [1, 2] but does not allocate an array.

for i in range(2,8,2):
    statement # Similar to [2, 4, 6] but does not allocate an array.

for c in "Hello":
    print(c) # Iterate through all characters in a String, print every letter on new line.
match

match 语句用于转移程序的执行。它相当于在许多其他语言中出现的 switch 语句,但提供了一些附加功能。

match [expression]:
    [pattern](s):
        [block]
    [pattern](s):
        [block]
    [pattern](s):
        [block]

对于熟悉switch语句的人的速成课程:

①将 switch 替换为 match
②删除 case
③Remove any breaks. If you don’t want to break by default, you can use continue for a fallthrough.
④将 default 更改为单个下划线。
条件都是从上到下匹配的。如果模式匹配,则执行相应的块。然后,在 match 语句下面继续执行。如果您希望有一个下降,您可以使用 continue 来停止当前块中的执行,并检查它下面的那些。

  1. 常量原语,如数字和字符串:
match x:
    1:
        print("We are number one!")
    2:
        print("Two are better than one!")
    "test":
        print("Oh snap! It's a string!")
  1. 匹配变量/枚举的内容:
match typeof(x):
    TYPE_REAL:
        print("float")
    TYPE_STRING:
        print("text")
    TYPE_ARRAY:
        print("array")

3.这个模式匹配所有内容。它被写成一个下划线。

在其他语言中,它可以用作 switch 语句中 default 的等效项。

match x:
    1:
        print("It's one!")
    2:
        print("It's one times two!")
    _:
        print("It's not 1 or 2. I don't care tbh.")
  1. 绑定模式引入了一个新变量。与通配符模式类似,它匹配所有内容,并为该值提供一个名称。它在数组和字典模式中特别有用
match x:
    1:
        print("It's one!")
    2:
        print("It's one times two!")
    var new_var:
        print("It's not 1 or 2, it's ", new_var)
  1. 匹配一个数组。数组模式的每个元素本身就是一个模式,因此可以嵌套它们。

首先测试数组的长度,它必须与模式相同大小,否则模式不匹配。

开放式数组: 数组可以通过最后一个子模式 … 来大于模式,每个子模式必须用逗号分隔

match x:
    []:
        print("Empty array")
    [1, 3, "test", null]:
        print("Very specific array")
    [var start, _, "test"]:
        print("First element is ", start, ", and the last is \"test\"")
    [42, ..]:
        print("Open ended array")
  1. 每个键必须是一个常量模式。首先测试字典的大小,它必须与模式的大小相同,否则模式不匹配。

开放式字典: 字典可以通过最后一个子模式 … 来大于模式

每个子模式必须用逗号分隔。

如果不指定值,则只检查键的存在。

值模式与键模式之间用一个 : 分隔开

match x:
    {}:
        print("Empty dict")
    {"name": "Dennis"}:
        print("The name is Dennis")
    {"name": "Dennis", "age": var age}:
        print("Dennis is ", age, " years old.")
    {"name", "age"}:
        print("Has a name and an age, but it's not Dennis :(")
    {"key": "godotisawesome", ..}:
        print("I only checked for one entry and ignored the rest")

你还可以指定由逗号分隔的多个模式。这些模式中不允许有任何绑定

match x:
    1, 2, 3:
        print("It's 1 - 3")
    "Sword", "Splash potion", "Fist":
        print("Yep, you've taken damage")
类型

默认情况下,所有脚本文件都是未命名的类。在这种情况下,只能使用文件的路径引用它们,使用相对路径或绝对路径。例如,如果您将脚本文件命名为 character.gd

# Inherit from Character.gd

extends res://path/to/character.gd

# Load character.gd and create a new node instance from it

var Character = load("res://path/to/character.gd")
var character_node = Character.new()

替代的,你可以给你的类一个名字,把它注册为godot编辑中的一种新类型。为此,您将使用 ‘class_name’ 关键字。您可以向图像添加一个可选逗号和一个路径,以便将其用作图标。然后,您的类将在编辑器中显示它的新图标:

# Item.gd

extends Node

class_name Item, "res://interface/icons/item.png"

godot游戏引擎自学入门笔记--GDScript语言基础,官方文档翻译(九)_第1张图片
这里有一个类文件的示例:

# Saved as a file named 'character.gd'.

class_name Character

var health = 5

func print_health():
    print(health)

func print_this_script_three_times():
    print(get_script())
    print(ResourceLoader.load("res://character.gd"))
    print(Character)

Godot的类语法非常紧凑:它只能包含成员变量或函数。可以使用静态函数,但不能使用静态成员变量。同样,每次创建实例时,引擎都会初始化变量,这包括数组和字典。这是线程安全的精神,因为脚本可以在单独的线程中初始化,而用户并不知道

继承

类(作为文件储存)可以继承
①一个全局的类
②另一类文件
③另一个类文件中的内部类
不允许多重继承,继承使用 extends 关键字

# Inherit/extend a globally available class.
extends SomeClass

# Inherit/extend a named class file.
extends "somefile.gd"

# Inherit/extend an inner class in another file.
extends "somefile.gd".SomeInnerClass

要检查给定实例是否从给定类继承,可以使用 is 关键字:

# Cache the enemy class.
const Enemy = preload("enemy.gd")

# [...]

# Use 'is' to check inheritance.
if (entity is Enemy):
    entity.apply_damage()

要调用 基类 中的函数(即当前类 extend 后的类),请在函数名前面加上.

.basefunc(args)

这特别有用,因为扩展类中的函数会替换基类中同名的函数。所以如果您仍然想调用它们,您可以使用 . ,这就像其他语言中的 super 关键字一样

func some_func(x):
    .some_func(x) # Calls same function on the parent class.

类文件可以包含内部类。内部类使用 class 关键字定义。它们使用 ClassName.new() 函数实例化。

# Inside a class file.

# An inner class in this class file.
class SomeInnerClass:
    var a = 5
    func print_value_of_a():
        print(a)

# This is the constructor of the class file's main class.
func _init():
    var c = SomeInnerClass.new()
    c.print_value_of_a()

以文件形式存储的类被视为 resources。必须从磁盘加载它们,才能在其他类中访问它们。这可以使用 load 或 preload 函数来完成(见下面)。加载类资源的实例化是通过调用类对象上的 new 函数来完成的

# Load the class resource when calling load().
var my_class = load("myclass.gd")

# Preload the class only once at compile time.
const MyClass = preload("myclass.gd")

func _init():
    var a = MyClass.new()
    a.some_function()

你可能感兴趣的:(游戏引擎)