9

GDScript 中是否有等效的 C# 结构/类?例如

struct Player
{
     string Name;
     int Level;
}
4

1 回答 1

16

Godot 3.1.1gdscript不支持structs,但可以使用类似的结果classesdictlua style table syntax

http://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_basics.html

GDScript 可以包含多个内部类,创建一个具有适当属性的内部类,模仿上面的示例:

class Player:
    var Name: String
    var Level: int

这是使用该 Player 类的完整示例:

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 样式表语法:

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

仔细查看官方文档以了解故障:

在此处输入图像描述

于 2019-05-07T18:44:21.527 回答