Module:NpcData: Difference between revisions

Jump to navigation Jump to search
LVL (talk | contribs)
Added `get_nested_var` function for accessing nested data
LVL (talk | contribs)
Fixed get_nested_var to handle numeric-string keys
Line 128: Line 128:
local value = npc
local value = npc
for key in string.gmatch(path, '([^.]+)') do
for key in string.gmatch(path, '([^.]+)') do
if type(value) ~= "table" then
    if type(value) ~= "table" then
return '<span class="error">Error: Path "' .. path .. '" is invalid.</span>'
        return '<span class="error">Error: Path "' .. path .. '" is invalid.</span>'
end
    end
value = value[key]
    -- try first with the raw key, then with a number conversion
if value == nil then
    local next = value[key] or value[tonumber(key)] or value[tostring(key)]
return '<span class="error">Error: Key "' .. key .. '" not found in path.</span>'
    if next == nil then
end
        return '<span class="error">Error: Key "' .. key .. '" not found in path.</span>'
    end
    value = next
end
end