Module:GameData: Difference between revisions

getEntities: Allow passing of table as entityType, and relax checks on data
(getEntityByProperty: Fix if propName is anything other than 'id')
(getEntities: Allow passing of table as entityType, and relax checks on data)
Line 145: Line 145:
local result = {}
local result = {}
local entityCount = 0
local entityCount = 0
if type(entityType) ~= 'string' then
if type(entityType) ~= 'string' and type(entityType) ~= 'table' then
error('Entity type name must be a string', 2)
error('Entity type name must be a string or table', 2)
elseif type(checkFunc) ~= 'function' then
elseif type(checkFunc) ~= 'function' then
error('Check function name must be a function', 2)
error('Check function name must be a function', 2)
end
end
local entData, useCache = nil, false
    if type(entityType) == 'string' then
        entData = GameData[entityType]
        useCache = true
    else
        -- Function was passed a table of entities rather than a entity type
        entData = entityType
    end
local entData = GameData[entityType]
local entData = GameData[entityType]
if entData == nil then
if entData == nil then
Line 155: Line 163:
elseif type(entData) ~= 'table' then
elseif type(entData) ~= 'table' then
error('Entity data is not a table: ' .. entityType, 2)
error('Entity data is not a table: ' .. entityType, 2)
elseif type(entData[1]) ~= 'table' or entData[1].id == nil then
elseif type(entData[1]) ~= 'table' then
error('Entity data is not composed of entities: ' .. entityType, 2)
error('Entity data is not composed of entities: ' .. entityType, 2)
end
end
for idx, entity in ipairs(entData) do
for idx, entity in ipairs(entData) do
setCache(entityType, entity.id, idx)
if useCache then
setCache(entityType, entity.id, idx)
end
if checkFunc(entity) then
if checkFunc(entity) then
entityCount = entityCount + 1
entityCount = entityCount + 1