【问题标题】:How do I implement structures in GDScript?如何在 GDScript 中实现结构?
【发布时间】:2019-09-25 02:49:40
【问题描述】:

GDScript 中是否存在与 C# 结构/类等效的内容? 例如

struct Player
{
     string Name;
     int Level;
}

【问题讨论】:

  • 我在你回答的那天就接受了,谢谢你的帮助:) 它没有出现在你面前吗?
  • 我太傻了!我正在使用新的浏览器扩展程序将页面更改为“暗模式”,但忘记将其关闭以用于 stackoverflow。复选标记对我来说是灰色的,尽管它实际上是绿色的。我会删除我原来的评论。谢谢!!

标签: godot gdscript


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多