ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Circonians stambar tutorial, UI error (https://www.esoui.com/forums/showthread.php?t=7973)

Dekthro 08/09/18 06:49 PM

Circonians stambar tutorial, UI error
 
I've made it to part 3 of Circonians Stamina Bar tutorial and I am receiving the error

Quote:

user:/AddOns/Dek01/Dek01.lua:39: attempt to index a nil value
stack traceback:
user:/AddOns/Dek01/Dek01.lua:39: in function 'Dek01:Initialize'
user:/AddOns/Dek01/Dek01.lua:29: in function 'Dek01.OnAddOnLoaded'
I've combed through my lua file trying to resolve this on my own but I am just not seeing what it could be.

Lua Code:
  1. -------------------------------------------------------------------------------------------------
  2. --  Libraries --
  3. -------------------------------------------------------------------------------------------------
  4. local LAM2 = LibStub:GetLibrary("LibAddonMenu-2.0")
  5.  
  6. Dek01 = {}
  7. Dek01.Default = {
  8.     OffsetX = 20,
  9.     OffsetY = 75,
  10.     Show = true,
  11.     StaminaBarColor = {1, 0, 0, 1},
  12.     BarWidth = 300,
  13.     BarHeight = 50,
  14. }
  15.  
  16. -------------------------------------------------------------------------------------------------
  17. --  Initialize Variables --
  18. -------------------------------------------------------------------------------------------------
  19. Dek01.name = "Dek01"
  20. Dek01.version = 1
  21. Dek01.varVersion = 2
  22.  
  23. -------------------------------------------------------------------------------------------------
  24. --  OnAddOnLoaded  --
  25. -------------------------------------------------------------------------------------------------
  26. function Dek01.OnAddOnLoaded(event, addonName)
  27.    if addonName ~= Dek01.name then return end
  28.  
  29.     Dek01:Initialize()
  30. end
  31.  
  32. -------------------------------------------------------------------------------------------------
  33. --  Initialize Function --
  34. -------------------------------------------------------------------------------------------------
  35. function Dek01:Initialize()
  36.     Dek01.savedVars = ZO_SavedVars:New("Dek01Vars", Dek01.varVersion, nil, Dek01.Default)
  37.    
  38.     Dek01.CreateSettingsWindow()
  39.     Dek01StaminaBarWindow:SetHidden(not Dek01.savedVars.Show)
  40.     Dek01.SetBarSize(Dek01.savedVars.BarWidth, Dek01.savedVars.BarHeight)
  41.     Dek01StaminaBarWindowStatusBar:SetColor(unpack(Dek01.savedVars.StaminaBarColor))
  42.  
  43.     local current, max, effectiveMax = GetUnitPower("player", POWERTYPE_STAMINA)
  44.     Dek01StaminaBarWindowStatusBar:SetMinMax(0, max)                    -- Set max stamina
  45.     Dek01StaminaBarWindowStatusBar:SetValue(current)                    -- Set current stamina
  46.  
  47.     local ourName = GetUnitName("player")                               --Get player name
  48.     Dek01StaminaBarWindowLabel:SetText(ourName)                         --Set label to player name
  49.  
  50.     Dek01StaminaBarWindow.ClearAnchors()
  51.     Dek01StaminaBarWindow:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, Dek01.savedVars.OffsetX, Dek01.savedVars.OffsetY)
  52.    
  53.     EVENT_MANAGER:UnregisterForEvent(Dek01.name, EVENT_ADD_ON_LOADED)   --Unregister event
  54. end
  55.  
  56. -------------------------------------------------------------------------------------------------
  57. --  Other Functions --
  58. -------------------------------------------------------------------------------------------------
  59.  function Dek01.UpdateStamina(eventCode, unitTag, powerIndex, powerType, powerValue, powerMax, powerEffectiveMax)
  60.  
  61.     if powerType == POWERTYPE_STAMINA then
  62.         Dek01StaminaBarWindowStatusBar:SetMinMax(0, powerMax)                   -- Set max stamina
  63.         Dek01StaminaBarWindowStatusBar:SetValue(powerValue)                 -- Set current stamina
  64.     end
  65. end
  66.  
  67. function Dek01.SaveLoc()
  68.     Dek01.savedVars.OffsetX = Dek01StaminaBarWindow:GetLeft()
  69.     Dek01.savedVars.OffsetY = Dek01StaminaBarWindow:GetTop()
  70. end
  71.  
  72. -------------------------------------------------------------------------------------------------
  73. --  Restore Functions --
  74. -------------------------------------------------------------------------------------------------
  75.  function Dek01.SetBarSize(_width, _height)
  76.     Dek01StaminaBarWindowStatusBar:SetDimensions(_width, _height)
  77.     Dek01StaminaBarWindowBackdrop:SetDimensions(_width, _height)
  78.     Dek01StaminaBarBorder:SetDimensions(_width, _height)
  79.     Dek01StaminaBarWindow:SetDimensions(_width, _height)
  80. end
  81.  
  82. -------------------------------------------------------------------------------------------------
  83. --  Menu Functions --
  84. -------------------------------------------------------------------------------------------------
  85. function Dek01.CreateSettingsWindow()
  86.     local panelData = {
  87.         type = "panel",
  88.         name = "Dek01",
  89.         displayName = "DekUI",
  90.         author = "Dekthro",
  91.         version = Dek01.version,
  92.         slashCommand = "/dekui",
  93.         registerForRefresh = true,
  94.         registerForDefaults = true,
  95.     }
  96.  
  97.     local cntrlOptionsPanel = LAM2:RegisterAddonPanel("Dek_01", panelData)
  98.    
  99.     local optionsData = {
  100.         [1] = {
  101.             type = "header",
  102.             name = "Stamina Bar Settings",
  103.         },
  104.         [2] = {
  105.             type = "description",
  106.             text = "Here you can adjust how the stamina bar works.",
  107.         },
  108.         [3] = {
  109.             type = "checkbox",
  110.             name = "Show Stamina Bar",
  111.             tooltip = "When ON the stamina bar will be visible. When OFF the stamina bar will be hidden.",
  112.             default = true,
  113.             getFunc = function() return Dek01.savedVars.Show end,
  114.             setFunc = function(newValue)
  115.                 Dek01.savedVars.Show = newValue
  116.                 Dek01StaminaBarWindow:SetHidden(not newValue) end,
  117.         },
  118.         [4] = {
  119.             type = "slider",
  120.             name = "Select Width",
  121.             tooltip = "Adjusts the width of the stamina bar.",
  122.             min = 100,
  123.             max = 1000,
  124.             step = 1,
  125.             default = 300,
  126.             getFunc = function() return Dek01.savedVars.BarWidth end,
  127.             setFunc = function(newValue)
  128.                 Dek01.savedVars.BarWidth = newValue
  129.                 Dek01.SetBarSize(newValue, Dek01.savedVars.BarHeight)
  130.                 end,
  131.         },
  132.         [5] = {
  133.             type = "slider",
  134.             name = "Select Height",
  135.             tooltip = "Adjusts the height of the stamina bar.",
  136.             min = 25,
  137.             max = 100,
  138.             step = 1,
  139.             default = 50,
  140.             getFunc = function() return Dek01.savedVars.BarHeight end,
  141.             setFunc = function(newValue)
  142.                 Dek01.savedVars.BarHeight = newValue
  143.                 Dek01.SetBarSize(Dek01.savedVars.BarWidth, newValue)
  144.                 end,
  145.         },
  146.         [6] = {
  147.             type = "submenu",
  148.             name = "Colors",
  149.             tooltip = "Allows you to change colors.",
  150.             controls = {
  151.                 [1] = {
  152.                     type = "colorpicker",
  153.                     name = "Bar Color",
  154.                     tooltip = "Changes the color of the stamina bar background.",
  155.                     getFunc = function() return unpack(Dek01.savedVars.StaminaBarColor ) end,
  156.                     setFunc = function(r,g,b,a)
  157.                         local alpha = Dek01StaminaBarWindowStatusBar:GetAlpha()
  158.                         Dek01.savedVars.StaminaBarColor = { r, g, b, a}
  159.                         Dek01StaminaBarWindowStatusBar:SetColor( r, g, b, a)
  160.                         end,
  161.                 },
  162.             }
  163.         }
  164.     }
  165.    
  166.     LAM2:RegisterOptionControls("Dek_01", optionsData)
  167. end
  168.  
  169. -------------------------------------------------------------------------------------------------
  170. --  Register Events --
  171. -------------------------------------------------------------------------------------------------
  172. EVENT_MANAGER:RegisterForEvent(Dek01.name, EVENT_ADD_ON_LOADED, Dek01.OnAddOnLoaded)
  173. EVENT_MANAGER:RegisterForEvent(Dek01.name, EVENT_POWER_UPDATE, Dek01.UpdateStamina)

Thank you!

SlippyCheeze 08/09/18 06:56 PM

Well, the backtrace tells us it is on line 39, so it must be in:

Lua Code:
  1. Dek01StaminaBarWindow:SetHidden(not Dek01.savedVars.Show)

I'd guess that either the window wasn't created, possibly due to forgetting to add the XML to the manifest, or that your defaults / saved vars don't have the show value.

You can investigate using the `d` function in there, to dump out the various things you are looking at.

Dekthro 08/09/18 07:13 PM

Manifest
Code:

## Title: A Dek Stamina Bar
## Description: Dek01 Stamina Bar Version 1.0
## APIVersion: 100023
## OptionalDependsOn: LibAddonMenu-2.0
## SavedVariables: Dek01Vars

Libs/LibStub/LibStub.lua
Libs/LibAddonMenu-2.0/LibAddonMenu-2.0.lua
Libs/LibAddonMenu-2.0/controls/panel.lua
Libs/LibAddonMenu-2.0/controls/submenu.lua
Libs/LibAddonMenu-2.0/controls/button.lua
Libs/LibAddonMenu-2.0/controls/checkbox.lua
Libs/LibAddonMenu-2.0/controls/colorpicker.lua
Libs/LibAddonMenu-2.0/controls/custom.lua
Libs/LibAddonMenu-2.0/controls/description.lua
Libs/LibAddonMenu-2.0/controls/dropdown.lua
Libs/LibAddonMenu-2.0/controls/editbox.lua
Libs/LibAddonMenu-2.0/controls/header.lua
Libs/LibAddonMenu-2.0/controls/slider.lua
Libs/LibAddonMenu-2.0/controls/texture.lua
Libs/LibAddonMenu-2.0/controls/iconpicker.lua

Dek01.lua
Dek01.xml

It is listed, I also tried adding in the debug output
Lua Code:
  1. function Dek01:Initialize()
  2.     d("Initializing")
  3.     Dek01.savedVars = ZO_SavedVars:New("Dek01Vars", Dek01.varVersion, nil, Dek01.Default)
  4.     Dek01.CreateSettingsWindow()
  5.     d(Dek01.savedVars.Show)

Which returned true in the console, as I have it set in my defaults. It's also set in my saved vars as well.

Lua Code:
  1. Dek01Vars =
  2. {
  3.     ["Default"] =
  4.     {
  5.         ["@Dekthro"] =
  6.         {
  7.             ["Domochevsky"] =
  8.             {
  9.                 ["BarHeight"] = 50,
  10.                 ["OffsetY"] = 75,
  11.                 ["StaminaBarColor"] =
  12.                 {
  13.                     [4] = 1,
  14.                     [1] = 1,
  15.                     [2] = 0,
  16.                     [3] = 0,
  17.                 },
  18.                 ["version"] = 20,
  19.                 ["Show"] = true,
  20.                 ["OffsetX"] = 20,
  21.                 ["BarWidth"] = 300,
  22.             },
  23.         },
  24.     },
  25. }

And my XML file while we're at it :P

Code:

<GuiXml>
        <Controls>
                <TopLevelControl name="Dek01StaminaBarWindow" clampedToScreen="true"
                mouseEnabled="true"        movable="true" hidden="false">
                        <Dimensions x="300" y="50" />
                        <Anchor point ="TOPLEFT" relativeTo="GuiRoot" relativePoint="CENTER" offsetX="0" offsetY="0"/>
                       
                        <OnMoveStop>
                                Dek01.SaveLoc()
                        </OnMoveStop
                       
                        <Controls>
                                <Backdrop name="$(parent)Backdrop" edgeColor="00000000" centerColor="6495ED" alpha="0.6" layer="0" level="0">
                                        <Dimensions x="300" y="50" />
                                        <Anchor point="TOPLEFT" relativeTo="$(parent)" relativePoint="TOPLEFT"/>
                                        <Edge edgeSize="0" />
                                </Backdrop>
                               
                                <Backdrop name="$(parent)Border" edgeColor="FF0000" centerColor="00000000" alpha="1" layer="0" level="2">
                                        <Dimensions x="300" y="50" />
                                        <Anchor point="TOPLEFT" relativeTo="$(parent)" relativePoint="TOPLEFT"/>
                                        <Edge edgeSize="6" />
                                </Backdrop>
                               
                                <StatusBar name="$(parent)StatusBar" color="8A2BE2" alpha="1" layer="0" level="1">
                                        <Dimensions x="300" y="50" />
                                        <Anchor point="TOPLEFT" relativeTo="$(parent)" relativePoint="TOPLEFT" offsetX="0" offsetY="0" />
                                        <Limits min="0" max="100" />
                                </StatusBar>
                               
                                <Label name="$(parent)Label" font="ZoFontWinH1" color="FFFFFF" text="Status Bar"
                                verticalAlignment="CENTER" horizontalAlignment="LEFT" alpha="0.85">
                                        <Dimensions x="350" y="50" />
                                        <Anchor point="BOTTOMLEFT" relativeTo="$(parent)StatusBar" relativePoint="TOPLEFT"
                                        offsetX="0" offsetY="0" />
                                </Label>
                        </Controls>
                </TopLevelControl>
        </Controls>
</GuiXml>


Baertram 08/10/18 12:25 AM

Try to add a nil check against Dek01StaminaBarWindow before the line 39 like this:
if nil == Dek01StaminaBarWindow then d ("TopLevelControl is mising!") end
This way cou can see if the control is somwhow missing.
And you can use the addon zgoo ingame like /zgoo Dek01StaminaBarWindow to check if the control is there, and see it's values.

SlippyCheeze 08/10/18 08:53 AM

Quote:

Originally Posted by Dekthro (Post 35550)
It is listed, I also tried adding in the debug output
Lua Code:
  1. function Dek01:Initialize()
  2.     d("Initializing")
  3.     Dek01.savedVars = ZO_SavedVars:New("Dek01Vars", Dek01.varVersion, nil, Dek01.Default)
  4.     Dek01.CreateSettingsWindow()
  5.     d(Dek01.savedVars.Show)

Ah. I see I wasn't quite clear here. I was suggesting something just before line 39:

Lua Code:
  1. d("window="..tostring(Dek01StaminaBarWindow))
  2. d("Dek01="..tostring(Dek01))
  3. d("Dek01.savedVars="..tostring(Dek01.savedVars))
  4. d("d.s.Show="..tostring(Dek01.savedVars.Show))
  5. -- line 39 contains this:
  6. Dek01StaminaBarWindow:SetHidden(not Dek01.savedVars.Show)

That should break on the line with the nil dereference, and make it clear where the issue is coming from. Then you have to figure out *why*, but that is a simpler problem once you know what. :)

PS: depending on taste, you may want to steal the `dmsg` function from my ReadItOnce addon, which automates the `tostring` parts of that.


All times are GMT -6. The time now is 03:16 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI