这是使用YAML的有效方法吗?

时间:2011-11-24 23:47:14

标签: arrays parsing yaml snakeyaml mud

我是蛇形虫和yaml的新手。我需要它来存储MUD的“房间”信息。

房间的条目如下所示:

room:
  id: 12
  entry: "Long string"
  description: "Longer more precise string"
  objects:
    ids: 1,23

object:
  id: 1
  name: "chest"
  description: "looks pretty damn old"
  on-text: "the chest has been opened!"
  off-text: "the chest has been closed!"

基本上,每个房间都有id,并且当玩家进入/搜索时会向玩家显示一些文字。它还有一个“对象”数组,它们本身在同一个yaml文件中声明。

我的yaml文件中的配置是否可行?另外,我需要将每个房间和每个对象提取到数组中,所以它看起来像这样:

[12, "long string", "Longer more precise string", [1, "chest", "looks pretty damn old", "the chest has been opened!", "the chest has been closed!"], [ ... item 23 ... ]]

这个配置使我可以轻松地解析文件并通过创建一个循环并通过数组位置引用每个值来创建GenericRoom和GenericObject类。这是SnakeYAML可以为我做的事情吗?我一直在玩一些例子,但是我对实际YAML缺乏了解,这让我很难获得好成绩。

1 个答案:

答案 0 :(得分:2)

有了这个,你必须自己将对象连接到房间:

room:
  id: 12
  entry: "Long string"
  objects: [1, 23]

objects:
  - { id: 1, text: bla bla }
  - { id: 2, text: bla bla 2 }
  - { id: 23, text: bla bla 23}

或SnakeYAML可以从锚点和别名中受益: (必须在使用别名之前定义锚点)

objects:
  - &id001 {id: 1, text: bla bla }
  - &id002 {id: 2, text: bla bla 2 }
  - &id023 {id: 23, text: bla bla 23 }

room:
  id: 12
  entry: "Long string"
  objects: [ *id001, *id023]

(您可以在此处查看您的文件:http://www.yaml.org/spec/1.2/spec.html#id2765878