尝试索引“text”字段(值为Null)

在编写插件时,我的wow客户端遇到了以下错误/

83x FrameXML\OptionsFrameTemplates.lua:157: attempt to index field
'text' (a nil value)
FrameXML\OptionsFrameTemplates.lua:157: in function <FrameXML\OptionsFrameTemplates.lua:156>

Locals:
self = ShiftDropDown_Button {
 0 = <userdata>
 toggle = ShiftDropDown_ButtonToggle {
 }
}
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = "尝试索引“text”字段(值为Null)"

我在XML文件中调用它

        <Button name="ShiftDropDown_Button" inherits="InterfaceOptionsListButtonTemplate" text="选择目标">
            <Size>
                <AbsDimension x="150" y="10" />
            </Size>
            <Anchors>
                <Anchor point="TOPLEFT">
                    <Offset x="189" y="-115" />
                </Anchor>
            </Anchors>
            <Scripts>
                <OnLoad>
                    ShiftDropDown_Button_OnLoad(self)
                </OnLoad>
                <OnClick>
                    ShiftDropDownFrame:Show()
                </OnClick>
            </Scripts>

函数在Lua中

function ShiftDropDown_Button_OnLoad(self)
    self:RegisterEvent('ADDON_LOADED')
    self:SetScript('OnEvent', function(self, event, ...)
        if event == 'ADDON_LOADED' and ... == 'TauntMasterReborn' then
            TauntMasterRebornDB = TauntMasterRebornDB or {}
            for option, value in pairs(TauntM_defaults) do
                if not TauntMasterRebornDB[option] then TauntMasterRebornDB[option] = value end
            end
            self:SetText(TauntMasterRebornDBChar.SHIFTTARGET1)
        end
    end)
end

能否有人解释一下它为什么会出现这个错误?我已经搜索过许多示例,但找不到调试或解决这个问题的方法。

点赞
用户10258222
用户10258222

你的按钮从InterfaceOptionsListButtonTemplate继承,此模板最初继承自OptionsListButtonTemplate

此模板具有一个按钮文本:

<ButtonText name="$parentText" justifyH="LEFT" wordwrap="false"/>

以下是错误发生的代码:

function OptionsListButton_OnEnter (self)
  if (self.text:IsTruncated()) then
    GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
    GameTooltip:SetText(self:GetText(), NORMAL_FONT_COLOR[1], NORMAL_FONT_COLOR[2], NORMAL_FONT_COLOR[3], 1, true);
  end
end

它试图使用按钮文本的属性值(在本例中为self)通过使用self.text。在XML文件中未分配text parentKey,而是在被覆盖的OnLoad函数中进行分配。

要修复您的模板,必须扩展您的ShiftDropDown_Button_OnLoad函数:

function ShiftDropDown_Button_OnLoad(self)
    self.text = _G[self:GetName() .. "Text"];
    self.highlight = self:GetHighlightTexture();

    self:RegisterEvent('ADDON_LOADED')
    self:SetScript('OnEvent', function(self, event, ...)
        if event == 'ADDON_LOADED' and ... == 'TauntMasterReborn' then
            TauntMasterRebornDB = TauntMasterRebornDB or {}
            for option, value in pairs(TauntM_defaults) do
                if not TauntMasterRebornDB[option] then TauntMasterRebornDB[option] = value end
            end
            self:SetText(TauntMasterRebornDBChar.SHIFTTARGET1)
        end
    end)
end
2018-08-24 10:19:19