Thread Tools Display Modes
03/31/14, 04:26 PM   #1
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Sometimes I hate this :)

So, I'm cleaning up my add-on by shifting more code (such as registering events) to their own functions, then calling them as necessary. I shifted the code that defines my top down window and the label control for my info bar into its own function. The label control is updated every tick.

Here's my crux. The add-on works still, but I'm getting a nil function error on the call to the label in the update function. Oddly, if I dismiss the error five times, it goes away until the next reload.

So, inside function: MS.toolText = WM:CreateControl("lsGoldT",statWin,CT_LABEL)

In update function: MS.toolText:SetText(MS.globals.barString)

This worked with no errors if I defined MS.toolText outside a function.
  Reply With Quote
03/31/14, 09:28 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Are you creating your IsGoldT control before the OnUpdate handler is set?
  Reply With Quote
03/31/14, 10:31 PM   #3
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Using OnUpdate in the xml. Should I move it out of there?
  Reply With Quote
03/31/14, 10:39 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
I don't know. Is MS.toolText being created before the OnUpdate script is defined?
  Reply With Quote
03/31/14, 11:45 PM   #5
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Yes, the function that creates it is being called in my initialization function, which is called with EVENT_ADD_ON_LOADED. Is it possible that the update handler is being called before that?

EDIT: Just for clarification, this is the current order of things in my add-on:
Initial Declarations
Initialization Function - Calls functions to register events post-add-on start, setup the initial variable values, and create the bar
EVENT_ADD_ON_LOADED is registered - calls the above mentioned Init function
Function to create toolbar - creates top level window, then creates MS.toolText
Function for registering events post load - called from init function above
Function to setup initial variables - called from init function above
Helper function for counting saved variables
Update Handler Function
...

Last edited by skyraker : 04/01/14 at 12:17 AM.
  Reply With Quote
04/01/14, 04:51 PM   #6
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
So your OnUpdate handler is set in the main chunk of the addon? Not during or after the EVENT_ADD_ON_LOADED event?
  Reply With Quote
04/01/14, 05:41 PM   #7
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Originally Posted by Seerah View Post
So your OnUpdate handler is set in the main chunk of the addon? Not during or after the EVENT_ADD_ON_LOADED event?
Is the add-on loaded event supposed to be after everything now? But that would not explain why it worked before my changes. Maybe my changes were the problem?

What I did:
  • Moved both the top-level window definition and the toolText definition from outside any function to inside a function.
  • Changed top-level window and toolText to be members of the addon's global table MS.
  • Added call to this new function from my initialization function.

The funny thing is that the add on still works after you clear the errors out. Typically something that give an error about getting nil when it expects something else indicates something is broken and the add on doesn't work.
  Reply With Quote
04/01/14, 07:10 PM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
EVENT_ADD_ON_LOADED fires when your addon has finished loading and your previously saved variables are ready to be accessed. Assume this is your addon below - I'll comment the order in which everything occurs.

YourAddon.txt
Code:
## Title: YourAddon

YourAddon.lua     --1: this file and its contents gets loaded into memory first
YourAddon.xml     --6: now that the above is finished, this file gets executed
YourAddon.lua - this file is loaded into memory from top to bottom
Lua Code:
  1. local myVar, counterLabel     --2: these local variables exist in memory now but haven't been assigned anything yet (nil)
  2.  
  3. --3: this function is loaded into memory but hasn't actually been called/run yet
  4. local function YourAddonInitialize()
  5.      EVENT_MANAGER:RegisterForEvent("YourAddon", EVENT_PLAYER_DEAD, YourAddonEvent)     --12: this other event gets registered for
  6.      counterLabel = WINDOW_MANAGER:CreateControl("YourAddonFrameCounter", YourAddonFrame, CT_LABEL)     --13: the label is now created (meanwhile, the OnUpdate script has tried to access it multiple times)
  7.      myVar = 0     --14: the counter is set to its initial value
  8. end
  9.  
  10. --4: the event is registered for, but hasn't fired yet, and so still hasn't called the function
  11. EVENT_MANAGER:RegisterForEvent("YourAddon", EVENT_ADD_ON_LOADED, function(event, addon)
  12.           if addon == "YourAddon" then     --11: your addon and its saved variables have finished loading, so EVENT_ADD_ON_LOADED fires and calls the initialize function since the addon argument matches the name of our addon now
  13.                YourAddonInitialize()
  14.           end
  15.      end)
  16.  
  17. --5: this function is loaded into memory but hasn't been called yet
  18. function YourAddonUpdate()     --9...: this begins to be called constantly starting with when the handler is set at #8 below
  19.      YourAddonFrameCounter:SetText(myVar)     --note it's trying to access the counter which still hasn't been created yet
  20.      myVar = myVar + 1
  21. end

YourAddon.xml - this file is executed from top to bottom
Code:
<GuiXml>
    <Controls>
        <TopLevelControl name="YourAddonFrame">     --7: your toplevel window is created
            <Dimensions x="200" y="42" />
            <Anchor point="CENTER" />
 
            <OnUpdate>
                YourAddonUpdate()     --8: your OnUpdate handler is set to this function - this now begins firing and calls this function according to your frame rate (50 times per second, if at 50fps)
            </OnUpdate>
 
            <Controls>
                <Backdrop name="$(parent)BG" inherits="ZO_ThinBackdrop" />     --10: the bg frame is created
            </Controls>
        </TopLevelControl>
    </Controls>
</GuiXml>

Now... this isn't the exact format that you described your addon to be, but am I close? Do you see the error(s)?
  Reply With Quote
04/01/14, 07:42 PM   #9
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Yes, so it was when I removed the control definitions into a function. When they were globals they were defined before my OnUpdate handler.
  Reply With Quote
04/01/14, 08:06 PM   #10
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
"globals" is the wrong term, but yes. It was a scoping issue.
  Reply With Quote
04/01/14, 08:10 PM   #11
skyraker
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 154
Aye, and thanks to you it is now completely fixed.
  Reply With Quote
04/01/14, 09:08 PM   #12
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Wonderful.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Sometimes I hate this :)


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off