View Single Post
04/10/14, 09:51 AM   #36
Stormknight
 
Stormknight's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 128
Posting this here as it may help someone else.

I was getting odd "attempt to index a nil value" message on loading from my add-on, from a line in the OnUpdate() function.

The reason it was happening is because I have the <OnUpdate> declared within the XML and it was firing before my add-on had completed it's initialisation, which included loading saved variables.

So, the OnUpdate() function was trying to access a saved variable that hadn't been loaded yet.

I resolved this by simply creating an initialised variable in my add-on.

Code:
MyAddon = ()
MyAddon.initialised = false
MyAddon.name = "MyAddon"

function MyAddon.OnUpdate()
    -- Bail if we haven't completed the initialisation routine yet.
    if (not MyAddon.initialised) then return end
    -- code I wanted to execute here
end

function MyAddon.Initialise(eventCode, addOnName)
    -- Only initialize our own addon
    if (MyAddon.name ~= addOnName) then return end
    -- Do my initialisation tasks here, including loading saved variables etc
    MyAddon.initialised = true
end

EVENT_MANAGER:RegisterForEvent("MyAddon", EVENT_ADD_ON_LOADED, MyAddon.Initialise)
with the XML being something like
Code:
<GuiXml>
    <Controls>
        <TopLevelControl name="MyAddon" mouseEnabled="true" movable="true" clampedToScreen="true">
            <Dimensions x="200" y="200" />
            <OnUpdate>
                MyAddon.OnUpdate()
            </OnUpdate>
        </TopLevelControl>
    </Controls>
</GuiXml>
  Reply With Quote