From 78dc7ee937e01a595859c075f4dad2e7f02d715c Mon Sep 17 00:00:00 2001 From: Pablo Ariel Mayobre Date: Sun, 15 Mar 2020 00:20:06 -0300 Subject: [PATCH] Fixed some error messages --- concord/components.lua | 28 ++++++++++------------- concord/entity.lua | 2 +- concord/system.lua | 6 ++--- concord/world.lua | 51 ++++++++++++++++++++++++++---------------- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/concord/components.lua b/concord/components.lua index d4d6482..bf20d2d 100644 --- a/concord/components.lua +++ b/concord/components.lua @@ -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 diff --git a/concord/entity.lua b/concord/entity.lua index acafe01..888353f 100644 --- a/concord/entity.lua +++ b/concord/entity.lua @@ -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] diff --git a/concord/system.lua b/concord/system.lua index 3583010..756b124 100644 --- a/concord/system.lua +++ b/concord/system.lua @@ -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 diff --git a/concord/world.lua b/concord/world.lua index f42a608..e874881 100644 --- a/concord/world.lua +++ b/concord/world.lua @@ -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