Fixed some error messages

This commit is contained in:
Pablo Ariel Mayobre 2020-03-15 00:20:06 -03:00
parent d8621e4070
commit 78dc7ee937
No known key found for this signature in database
GPG key ID: 5ACD9E6858BEB0A9
4 changed files with 48 additions and 39 deletions

View file

@ -7,19 +7,6 @@ local Type = require(PATH..".type")
local Components = {} 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 --- Returns true if the containter has the ComponentClass with the specified name
-- @string name Name of the ComponentClass to check -- @string name Name of the ComponentClass to check
-- @treturn boolean -- @treturn boolean
@ -33,14 +20,23 @@ end
-- @treturn boolean -- @treturn boolean
-- @treturn Component or error string -- @treturn Component or error string
function Components.try(name) 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 end
--- Returns the ComponentClass with the specified name --- Returns the ComponentClass with the specified name
-- @string name Name of the ComponentClass to get -- @string name Name of the ComponentClass to get
-- @treturn Component -- @treturn Component
function Components.get(name) function Components.get(name)
local ok, value = try(name) local ok, value = Components.try(name)
if not ok then error(value, 2) end if not ok then error(value, 2) end
@ -49,7 +45,7 @@ end
return setmetatable(Components, { return setmetatable(Components, {
__index = function(_, name) __index = function(_, name)
local ok, value = try(name) local ok, value = Components.try(name)
if not ok then error(value, 2) end if not ok then error(value, 2) end

View file

@ -204,7 +204,7 @@ function Entity:deserialize(data)
local componentData = data[i] local componentData = data[i]
if (not Components.has(componentData.__name)) then 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 end
local componentClass = Components[componentData.__name] local componentClass = Components[componentData.__name]

View file

@ -51,11 +51,11 @@ local validateFilters = function (baseFilters)
for name, componentsList in pairs(baseFilters) do for name, componentsList in pairs(baseFilters) do
if type(name) ~= 'string' then 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 end
if type(componentsList) ~= 'table' then 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 end
local filter = {} local filter = {}
@ -63,7 +63,7 @@ local validateFilters = function (baseFilters)
local ok, componentClass = Components.try(component) local ok, componentClass = Components.try(component)
if not ok then 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 end
filter[#filter + 1] = componentClass filter[#filter + 1] = componentClass

View file

@ -158,37 +158,31 @@ local blacklistedSystemFunctions = {
"onDisabled", "onDisabled",
} }
--- Adds a System to the World. local tryAddSystem = function (world, systemClass)
-- 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)
if (not Type.isSystemClass(systemClass)) then 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 end
if (self.__systemLookup[systemClass]) then if (world.__systemLookup[systemClass]) then
error("bad argument #1 to 'World:addSystems' (SystemClass was already added to World)", 2) return false, "SystemClass was already added to World"
end end
-- Create instance of system -- Create instance of system
local system = systemClass(self) local system = systemClass(world)
self.__systemLookup[systemClass] = system world.__systemLookup[systemClass] = system
self.__systems:add(system) world.__systems:add(system)
for callbackName, callback in pairs(systemClass) do for callbackName, callback in pairs(systemClass) do
-- Skip callback if its blacklisted -- Skip callback if its blacklisted
if (not blacklistedSystemFunctions[callbackName]) then if (not blacklistedSystemFunctions[callbackName]) then
-- Make container for all listeners of the callback if it does not exist yet -- Make container for all listeners of the callback if it does not exist yet
if (not self.__events[callbackName]) then if (not world.__events[callbackName]) then
self.__events[callbackName] = {} world.__events[callbackName] = {}
end end
-- Add callback to listeners -- Add callback to listeners
local listeners = self.__events[callbackName] local listeners = world.__events[callbackName]
listeners[#listeners + 1] = { listeners[#listeners + 1] = {
system = system, system = system,
callback = callback, callback = callback,
@ -197,8 +191,24 @@ function World:addSystem(systemClass)
end end
-- Evaluate all existing entities -- Evaluate all existing entities
for j = 1, self.__entities.size do for j = 1, world.__entities.size do
system:__evaluate(self.__entities[j]) 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 end
return self return self
@ -214,7 +224,10 @@ function World:addSystems(...)
for i = 1, select("#", ...) do for i = 1, select("#", ...) do
local systemClass = select(i, ...) 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 end
return self return self