src/serialization

Source   Edit  

Procs

proc createEntityTable[T: tuple](world: var World; tup: typedesc[T]): Table[
    EntityId, seq[JsonNode]]
Source   Edit  
proc deserializeFromText[T: tuple](text: string; tup: typedesc[T]): World
Deserialize a json string into a world. Use a tuple to specify the components to deserialize, do not include the Meta component. All entities will be given a proper Meta component.

Example:

import examples

let text = """
      { "entities": [
        { "id": 2, "components": [
          { "*component": "Character", "name": "Brom", "class": "" },
          { "*component": "Health", "health": 140, "maxHealth": 140 },
          { "*component": "Weapon", "name": "Battle Axe", "attack": 32 },
          { "*component": "Armor", "name": "Fur Armor", "defense": 15, "buffs": [] }
        ] },
        { "id": 0,
          "components": [
            { "*component": "Character", "name": "Marcus", "class": "" },
            { "*component": "Health", "health": 100, "maxHealth": 100 },
            { "*component": "Weapon", "name": "Sword", "attack": 20 }
        ] },
        { "id": 1,
          "components": [
            { "*component": "Character", "name": "Elena", "class": "" },
            { "*component": "Health", "health": 80, "maxHealth": 80 },
            { "*component": "Amulet", "name": "Arcane Stone", "attack": 0, "magic": [] }
        ] }
      ] }
      """

var w = deserializeFromText(text, (Character, Health, Weapon, Amulet, Armor))
var characters = 0
var weapons = 0
var armors = 0
var amulets = 0

var query: Query[(Character, Health, Opt[Weapon], Opt[Amulet], Opt[Armor])]
for (character, health, weapon, amulet, armor) in w.query(query):
  inc characters

  weapon.isSomething:
    inc weapons

  amulet.isSomething:
    inc amulets

  armor.isSomething:
    inc armors

assert characters == 3
assert weapons == 2
assert armors == 1
assert amulets == 1
Source   Edit  
proc serializeToText[T: tuple](world: var World; tup: typedesc[T]): string
Serialize a world to a json string. Use a tuple to specify the components to serialize, do not include the Meta component.

Example:

import examples

var w = World()
w.add((Character(name: "Marcus"), Health(health: 100, maxHealth: 100)), Immediate)
w.add((Character(name: "Elena"), Health(health: 80, maxHealth: 80)), Immediate)
w.add((Character(name: "Brom"), Health(health: 140, maxHealth: 140)), Immediate)
echo w.serializeToText (Character, Health)
Source   Edit