n0pe n0pe - 3 months ago 11
YAML Question

Is this a valid method of using YAML?

I am new to snakeyaml and yaml in general. I need it to store information about "rooms" for a MUD.

The entries for the rooms will look something like this:

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!"


Basically, each room has an
id
and some text to be displayed to the player when they enter/search it. It also has an array of "objects" which are themselves declared in the same yaml file.

Is this configuration within my yaml file possible? Also, I would need to extract into arrays each room and each object, so it looks like this:

[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 ... ]]


This configuration makes it easy for me to parse the file and create GenericRoom and GenericObject classes by making one single loop and referencing every value by array position. Is this something SnakeYAML can do for me? I've been playing around with some examples but my lack of knowledge in actual YAML is making it hard for me to get good results.

Answer

With this you have to connect objects to rooms yourself:

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}

or SnakeYAML can benefit from anchors and aliases: (anchors must be defined before aliases are used)

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]

(You can check your documents here: http://www.yaml.org/spec/1.2/spec.html#id2765878)

Comments