Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Join the Playtest on Steam Now: SpiritVale

Module:GameData/doc: Difference between revisions

From SpiritVale Wiki
No edit summary
No edit summary
Line 21: Line 21:
Each JSON page is expected to have the following top-level structure:
Each JSON page is expected to have the following top-level structure:


<syntaxhighlight lang="json">
{
{
  "version": "SpiritVale-0.8.2",
  "version": "SpiritVale-0.8.2",
  "schema_version": 1,
  "schema_version": 1,
  "generated_at": "2025-12-12T17:24:05.807675+00:00",
  "generated_at": "2025-12-12T17:24:05.807675+00:00",
  "records": [
  "records": [
    {
    {
      "Name": "Some Skill",
      "Name": "Some Skill",
      "Internal Name": "SomeSkillInternalId",
      "Internal Name": "SomeSkillInternalId",
      "...": "other fields specific to this type"
      "...": "other fields specific to this type"
    },
    },
    {
    {
      "Name": "Another Skill",
      "Name": "Another Skill",
      "Internal Name": "AnotherSkill",
      "Internal Name": "AnotherSkill",
      "...": "more data"
      "...": "more data"
    }
    }
  ]
  ]
}
}
</syntaxhighlight>


Key points:
Key points:
Line 57: Line 55:
Every <code>load*</code> function returns the same dataset structure:
Every <code>load*</code> function returns the same dataset structure:


<syntaxhighlight lang="lua">
local dataset = {
local dataset = {
  meta    = {
  meta    = {
    version        = "SpiritVale-0.8.2",
    version        = "SpiritVale-0.8.2",
    schema_version = 1,
    schema_version = 1,
    generated_at  = "2025-12-12T17:24:05.807675+00:00",
    generated_at  = "2025-12-12T17:24:05.807675+00:00",
  },
  },
  records = { ... },  -- array of all records from the JSON
  records = { ... },  -- array of all records from the JSON
  byId    = {        -- lookup table by "Internal Name"
  byId    = {        -- lookup table by "Internal Name"
    ["SomeSkillInternalId"] = { ...record... },
    ["SomeSkillInternalId"] = { ...record... },
    ["AnotherSkill"]        = { ...record... },
    ["AnotherSkill"]        = { ...record... },
    -- etc.
    -- etc.
  },
  },
}
}
</syntaxhighlight>


This makes it easy to either:
This makes it easy to either:
Line 86: Line 82:
Reads [[Data:skills.json]] and returns a dataset for all active skills.
Reads [[Data:skills.json]] and returns a dataset for all active skills.


<syntaxhighlight lang="lua">
local GameData = require("Module:GameData")
local GameData = require("Module:GameData")
local skills = GameData.loadSkills()
local skills = GameData.loadSkills()
 
-- Access metadata
-- Access metadata
local meta = skills.meta
local meta = skills.meta
 
-- Iterate all skills
-- Iterate all skills
for _, skill in ipairs(skills.records) do
for _, skill in ipairs(skills.records) do
    -- do something
    -- do something
end
end
 
-- Lookup by Internal Name
-- Lookup by Internal Name
local bash = skills.byId["Bash"]
local bash = skills.byId["Bash"]
</syntaxhighlight>


=== <code>loadPassives()</code> ===
=== <code>loadPassives()</code> ===
Line 106: Line 100:
Reads [[Data:passives.json]] and returns a dataset for all passive skills.
Reads [[Data:passives.json]] and returns a dataset for all passive skills.


<syntaxhighlight lang="lua">
local GameData = require("Module:GameData")
local GameData = require("Module:GameData")
local passives = GameData.loadPassives()
local passives = GameData.loadPassives()
</syntaxhighlight>


=== <code>loadSummons()</code> ===
=== <code>loadSummons()</code> ===
Line 115: Line 107:
Reads [[Data:summons.json]] and returns a dataset for all summons.
Reads [[Data:summons.json]] and returns a dataset for all summons.


<syntaxhighlight lang="lua">
local GameData = require("Module:GameData")
local GameData = require("Module:GameData")
local summons = GameData.loadSummons()
local summons = GameData.loadSummons()
</syntaxhighlight>


=== <code>loadEffects()</code> ===
=== <code>loadEffects()</code> ===
Line 124: Line 114:
Reads [[Data:effects.json]] and returns a dataset for all status effects / effects.
Reads [[Data:effects.json]] and returns a dataset for all status effects / effects.


<syntaxhighlight lang="lua">
local GameData = require("Module:GameData")
local GameData = require("Module:GameData")
local effects = GameData.loadEffects()
local effects = GameData.loadEffects()
</syntaxhighlight>


----
----
Line 142: Line 130:
Example (skills):
Example (skills):


<syntaxhighlight lang="lua">
local GameData = require("Module:GameData")
local GameData = require("Module:GameData")
local p = {}
 
local p = {}
function p.infobox(frame)
 
    local id = frame.args.id
function p.infobox(frame)
    local skills = GameData.loadSkills()
    local id = frame.args.id
    local rec = skills.byId[id]
    local skills = GameData.loadSkills()
    -- build and return HTML using 'rec'
    local rec = skills.byId[id]
end
 
    -- build and return HTML using 'rec'
return p
end
 
return p
</syntaxhighlight>


This keeps all JSON loading and parsing logic in one place, and makes it easy to update the data files each patch without changing the Lua code.
This keeps all JSON loading and parsing logic in one place, and makes it easy to update the data files each patch without changing the Lua code.