如何在 GDScript 中实现结构?
How do I implement structures in GDScript?
GDScript 中是否有 C# structure/class 的等价物? 例如
struct Player
{
string Name;
int Level;
}
Godot 3.1.1 gdscript
不支持 structs
,但使用 classes
、dict
或 lua style table syntax
http://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_basics.html
GDScript 可以包含多个内部 class,创建一个具有适当属性的内部 class 模仿上面的示例:
class Player:
var Name: String
var Level: int
这是使用该播放器的完整示例 class:
extends Node2D
class Player:
var Name: String
var Level: int
func _ready() -> void:
var player = Player.new()
player.Name = "Hello World"
player.Level = 60
print (player.Name, ", ", player.Level)
#prints out: Hello World, 60
您也可以使用 Lua 样式 Table 语法:
extends Node2D
#Example obtained from the official Godot gdscript_basics.html
var d = {
test22 = "value",
some_key = 2,
other_key = [2, 3, 4],
more_key = "Hello"
}
func _ready() -> void:
print (d.test22)
#prints: value
d.test22 = "HelloLuaStyle"
print (d.test22)
#prints: HelloLuaStyle
细细看官方文档: