初始化不设置字段

时间:2015-02-15 21:55:55

标签: lua lua-table

我正在进行Addon重写,基本上将旧的插件Wtacher for WildStar变成了一个更新版本,由于采用了新的模块化结构,因此更容易维护。原作者允许我这样做,但是告诉我重写附件以更好地理解它,因为我对此很陌生。

所以可以在这里找到原始的插件:http://www.curse.com/ws-addons/wildstar/221782-watcher

我的源代码可以在这里找到:https://github.com/NekoNova/WatcherEx

我正在访问的错误消息如下:

  

... aming \ NCSOFT \ WildStar \ Addons \ WatcherEx \ WatcherEx.lua:1433:糟糕   参数#1到'ipairs'(表格预期,得到零)堆栈跟踪:[C]:   功能'ipairs'     ... aming \ NCSOFT \ WildStar \ Addons \ WatcherEx \ WatcherEx.lua:1433:in   函数'FillFiltersList'     ... aming \ NCSOFT \ WildStar \ Addons \ WatcherEx \ WatcherEx.lua:886:in   功能< ... aming \ NCSOFT \ WildStar \ Addons \ WatcherEx \ WatcherEx.lua:867>

所以我在上面提到的行中查看了我的源代码,基本上这段代码导致了问题:

-- Fills the list of Filters available to be used in the Addon.
function WatcherEx:FillFiltersList()
    if self.wndMain == nil then
        return 
    end

    local filtersList = self.wndMain:FindChild("filters_list")
    filtersList:DestroyChildren()

    for i, filterClass in ipairs(self.tFilters) do
        local filterItem = Apollo.LoadForm(self.xmlDoc, "filter_item", filtersList, self)
        filterItem:SetData({ filter = filterClass })
        filterItem:FindChild("name"):SetText(filterClass.name)
    end

    return filtersList:ArrangeChildrenVert()
end

所以基本上我尝试使用我的内部 tFilters 变量,但它是零,应该是一个表。 正如我所说,我对Lua的游戏编程很新,但据我所知,我可能在我的__init元函数中设置了一些错误,因为我从原版中复制了这个并尝试清理它。这是代码可元数的代码:

function WatcherEx:new(tObject)
    tObject = tObject or {}
    setmetatable(tObject, self)
    self.__index = self

    -- Initialization
    self.tSettings = {}
    self.tDefaults = {}
    self.tAbilities = {}
    self.tInnates = {}
    self.tEngineerSpells = {}
    self.tActiveRules = {}

    local tClass = setmetatable({
        __init = function(self)         
            self.tActiveRules = { }
            self.bUnlockAll = false
            self.eventsHidden = nil
            self.tFilters = {
                AvailabilityFilter,
                TimeFilter,
                CombatFilter,
                ActionSetFilter,
                SpecialFilter,
                HPFilter,
                MPFilter,
                ShieldFilter,
                ChargeFilter,
                ResourcesFilter,
                BuffFilter,
                LuaFilter}
        end,
        __base = self,
        __name = "WatcherEx"
    }, {
        __index = self,
        __call = function(cls, ...)
            local tTable = setmetatable({}, self)
            cls.__init(tTable, ...)
            return tTable
        end})

    self.__class = tClass
    return tObject
end

有人可以告诉我我做错了什么,并解释问题?我正在尝试更多地了解Lua,但是有关继承和元类的这一部分有点令人困惑。

编辑: 当我现在看它时,我实际上认为我可以删除metatable结构并直接将变量设置为我的WatcherEx表的成员....

1 个答案:

答案 0 :(得分:0)

好, 终于搞清楚了......

这是我的AvailabilityFilter类的片段,它继承自BaseFilter,现在我的字段通过我的元数据正确设置:

local AvailabilityFilter = {}
local AvailabilityFilterClass = {
    __init = function(self, ...) return BaseFilter.__init(self, ...) end,
    __base = AvailabilityFilter,
    __name = "AvailabilityFilter",
    __parent = BaseFilter
}
local AvailabilityFilterClassMt = {
    __index = function(cls, strName)
                local val = rawget(AvailabilityFilter, strName)

                if val == nil then
                    return BaseFilter[strName]
                else
                    return val
                end
            end,
    __call = function(cls, ...)
                local _self = setmetatable({}, AvailabilityFilter)  
                cls.__init(_self, ...)
                return _self
            end
}
AvailabilityFilter.mt = {}
AvailabilityFilter.tDefaults = {
    strName = "Availability",
    strWindowName = "AvailabilityFilter",
    bInActionSet = true,
    tParent = BaseFilter
}
AvailabilityFilter.mt.__index = AvailabilityFilter.tDefaults
-----------------------------------------------------------------------------------------------
-- Constructor
-----------------------------------------------------------------------------------------------
function AvailabilityFilter:new()
    local tInstance = {}

    setmetatable(AvailabilityFilter.mt, BaseFilter.__base)
    setmetatable(tInstance, AvailabilityFilter.mt)
    setmetatable(AvailabilityFilterClass, AvailabilityFilterClassMt)

    tInstance.__class = AvailabilityFilterClass

    if BaseFilter.__inherited then
        BaseFilter.__inherited(BaseFilter, AvailabilityFilterClass)
    end

    return tInstance
end

基本上我做的是:

  • 创建BaseFilter
  • 为它创建元表
  • 为其创建默认值表
  • 创建新对象+ metatable
  • 链接他们:对象 - > object metatable - > basefilter元表

现在它起作用了:

> a = _G["WatcherExLibs"]["ActionSetFilter"].new()
inspect a.strName
"Action Set"