попытка индексировать поле 'текст' (нулевое значение) - PullRequest
0 голосов
/ 28 апреля 2018

Я получаю следующую ошибку в моем 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)"

Я звоню в 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>

и функция в 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

Может кто-нибудь пролить свет на то, почему он выдает эту ошибку? Я искал много примеров и не могу найти способ отладить это или решить сам.

1 Ответ

0 голосов
/ 24 августа 2018

Ваша кнопка наследуется от 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. text parentKey не назначен в 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
...