mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-08-29 08:38:28 -04:00
Fixed some error messages
This commit is contained in:
parent
d8621e4070
commit
78dc7ee937
4 changed files with 48 additions and 39 deletions
|
@ -7,19 +7,6 @@ local Type = require(PATH..".type")
|
|||
|
||||
local Components = {}
|
||||
|
||||
local try = function (name)
|
||||
if type(name) ~= "string" then
|
||||
return false, "ComponentsClass name is expected to be a string, got "..type(name)..")"
|
||||
end
|
||||
|
||||
local value = rawget(Components, name)
|
||||
if not value then
|
||||
return false, "ComponentClass '"..name.."' does not exist / was not registered"
|
||||
end
|
||||
|
||||
return true, value
|
||||
end
|
||||
|
||||
--- Returns true if the containter has the ComponentClass with the specified name
|
||||
-- @string name Name of the ComponentClass to check
|
||||
-- @treturn boolean
|
||||
|
@ -33,14 +20,23 @@ end
|
|||
-- @treturn boolean
|
||||
-- @treturn Component or error string
|
||||
function Components.try(name)
|
||||
return try(name)
|
||||
if type(name) ~= "string" then
|
||||
return false, "ComponentsClass name is expected to be a string, got "..type(name)..")"
|
||||
end
|
||||
|
||||
local value = rawget(Components, name)
|
||||
if not value then
|
||||
return false, "ComponentClass '"..name.."' does not exist / was not registered"
|
||||
end
|
||||
|
||||
return true, value
|
||||
end
|
||||
|
||||
--- Returns the ComponentClass with the specified name
|
||||
-- @string name Name of the ComponentClass to get
|
||||
-- @treturn Component
|
||||
function Components.get(name)
|
||||
local ok, value = try(name)
|
||||
local ok, value = Components.try(name)
|
||||
|
||||
if not ok then error(value, 2) end
|
||||
|
||||
|
@ -49,7 +45,7 @@ end
|
|||
|
||||
return setmetatable(Components, {
|
||||
__index = function(_, name)
|
||||
local ok, value = try(name)
|
||||
local ok, value = Components.try(name)
|
||||
|
||||
if not ok then error(value, 2) end
|
||||
|
||||
|
|
|
@ -204,7 +204,7 @@ function Entity:deserialize(data)
|
|||
local componentData = data[i]
|
||||
|
||||
if (not Components.has(componentData.__name)) then
|
||||
error("bad argument #1 to 'Entity:deserialize' (ComponentClass "..type(componentData.__name).." wasn't yet loaded)") -- luacheck: ignore
|
||||
error("bad argument #1 to 'Entity:deserialize' (ComponentClass '"..tostring(componentData.__name).."' wasn't yet loaded)") -- luacheck: ignore
|
||||
end
|
||||
|
||||
local componentClass = Components[componentData.__name]
|
||||
|
|
|
@ -51,11 +51,11 @@ local validateFilters = function (baseFilters)
|
|||
|
||||
for name, componentsList in pairs(baseFilters) do
|
||||
if type(name) ~= 'string' then
|
||||
error("Invalid name for filter (string key expected, got "..type(name)..")", 3)
|
||||
error("invalid name for filter (string key expected, got "..type(name)..")", 3)
|
||||
end
|
||||
|
||||
if type(componentsList) ~= 'table' then
|
||||
error("Invalid component list for filter '"..name.."' (table expected, got "..type(componentsList)..")", 3)
|
||||
error("invalid component list for filter '"..name.."' (table expected, got "..type(componentsList)..")", 3)
|
||||
end
|
||||
|
||||
local filter = {}
|
||||
|
@ -63,7 +63,7 @@ local validateFilters = function (baseFilters)
|
|||
local ok, componentClass = Components.try(component)
|
||||
|
||||
if not ok then
|
||||
error("Invalid component for filter '"..name.."' at position #"..n.." ("..componentClass..")", 3)
|
||||
error("invalid component for filter '"..name.."' at position #"..n.." ("..componentClass..")", 3)
|
||||
end
|
||||
|
||||
filter[#filter + 1] = componentClass
|
||||
|
|
|
@ -158,37 +158,31 @@ local blacklistedSystemFunctions = {
|
|||
"onDisabled",
|
||||
}
|
||||
|
||||
--- Adds a System to the World.
|
||||
-- Callbacks are registered automatically
|
||||
-- Entities added before are added to the System retroactively
|
||||
-- @see World:emit
|
||||
-- @tparam System systemClass SystemClass of System to add
|
||||
-- @treturn World self
|
||||
function World:addSystem(systemClass)
|
||||
local tryAddSystem = function (world, systemClass)
|
||||
if (not Type.isSystemClass(systemClass)) then
|
||||
error("bad argument #1 to 'World:addSystems' (SystemClass expected, got "..type(systemClass)..")", 2)
|
||||
return false, "SystemClass expected, got "..type(systemClass)
|
||||
end
|
||||
|
||||
if (self.__systemLookup[systemClass]) then
|
||||
error("bad argument #1 to 'World:addSystems' (SystemClass was already added to World)", 2)
|
||||
if (world.__systemLookup[systemClass]) then
|
||||
return false, "SystemClass was already added to World"
|
||||
end
|
||||
|
||||
-- Create instance of system
|
||||
local system = systemClass(self)
|
||||
local system = systemClass(world)
|
||||
|
||||
self.__systemLookup[systemClass] = system
|
||||
self.__systems:add(system)
|
||||
world.__systemLookup[systemClass] = system
|
||||
world.__systems:add(system)
|
||||
|
||||
for callbackName, callback in pairs(systemClass) do
|
||||
-- Skip callback if its blacklisted
|
||||
if (not blacklistedSystemFunctions[callbackName]) then
|
||||
-- Make container for all listeners of the callback if it does not exist yet
|
||||
if (not self.__events[callbackName]) then
|
||||
self.__events[callbackName] = {}
|
||||
if (not world.__events[callbackName]) then
|
||||
world.__events[callbackName] = {}
|
||||
end
|
||||
|
||||
-- Add callback to listeners
|
||||
local listeners = self.__events[callbackName]
|
||||
local listeners = world.__events[callbackName]
|
||||
listeners[#listeners + 1] = {
|
||||
system = system,
|
||||
callback = callback,
|
||||
|
@ -197,8 +191,24 @@ function World:addSystem(systemClass)
|
|||
end
|
||||
|
||||
-- Evaluate all existing entities
|
||||
for j = 1, self.__entities.size do
|
||||
system:__evaluate(self.__entities[j])
|
||||
for j = 1, world.__entities.size do
|
||||
system:__evaluate(world.__entities[j])
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
--- Adds a System to the World.
|
||||
-- Callbacks are registered automatically
|
||||
-- Entities added before are added to the System retroactively
|
||||
-- @see World:emit
|
||||
-- @tparam System systemClass SystemClass of System to add
|
||||
-- @treturn World self
|
||||
function World:addSystem(systemClass)
|
||||
local ok, err = tryAddSystem(self, systemClass)
|
||||
|
||||
if not ok then
|
||||
error("bad argument #1 to 'World:addSystem' ("..err..")", 2)
|
||||
end
|
||||
|
||||
return self
|
||||
|
@ -214,7 +224,10 @@ function World:addSystems(...)
|
|||
for i = 1, select("#", ...) do
|
||||
local systemClass = select(i, ...)
|
||||
|
||||
self:addSystem(systemClass)
|
||||
local ok, err = tryAddSystem(self, systemClass)
|
||||
if not ok then
|
||||
error("bad argument #"..i.." to 'World:addSystems' ("..err..")", 2)
|
||||
end
|
||||
end
|
||||
|
||||
return self
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue