如何在GDScript中实现结构?

时间:2019-05-07 12:25:46

标签: godot gdscript

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

struct Player
{
     string Name;
     int Level;
}

1 个答案:

答案 0 :(得分:2)

Godot 3.1.1 gdscript不支持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

仔细查看官方文档以了解详细信息:

enter image description here