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

Nat*_*995 2 lua world-of-warcraft

在插件开发过程中,我的 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) = "attempt to index field 'text' (a nil value)"
Run Code Online (Sandbox Code Playgroud)

我用这个在 XML 文件中调用它

        <Button name="ShiftDropDown_Button" inherits="InterfaceOptionsListButtonTemplate" text="Select Target">
            <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>
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

谁能解释一下为什么会抛出这个错误?我搜索了很多例子,但找不到调试或解决这个问题的方法。

Syn*_*row 5

您的按钮继承自 ,InterfaceOptionsListButtonTemplate该按钮最初继承自OptionsListButtonTemplate

该模板有一个按钮文本:

<ButtonText name="$parentText" justifyH="LEFT" wordwrap="false"/>
Run Code Online (Sandbox Code Playgroud)

这是发生错误的代码:

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
Run Code Online (Sandbox Code Playgroud)

self它尝试通过使用按钮文本的属性值(在本例中) self.text。ParentKeytext不是在 xml 文件中分配的,而是在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
Run Code Online (Sandbox Code Playgroud)