Skip to content

Yaml Anchors

Cromha edited this page Mar 10, 2024 · 4 revisions

Introduction

Yaml anchors are a feature of the YAML language. They allow us to create dictionaries with default values than be re-used while creating new dictionaries, so we don't repeat same code over and over. This is particularly used while creating upgrade-able weapons items. But it can also be used in other cases like map zones or map points.

Creating & Using Templates

You can create an anchor by doing this:

generic weapon: &generic_weapon
  display name: "Generic Weapon"
  gold: 8.7
  [...]

By doing this, it will create a new key called 'generic weapon', but it will also create an anchor named 'generic_weapon'. You can after re-use this template by creating a new dictionary key like this:

generic weapon upgrade 2:
  <<: *generic_weapon
  new attribute: "this is a new attribute"
  [...]

Now like this, the 'generic weapon upgrade 2' key will have a key 'new attribute' but it will also inherit the 'generic_weapon' anchor keys. So the 'generic weapon upgrade 2' dictionary actually looks like this for the parser:

generic_weapon_upgrade_2 = {
  "new attributes": "this is a new attribute",
  "display name": "Generic Weapon",
  "gold": 8.7
}
Clone this wiki locally