ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Attempt to index a nil value confusion (https://www.esoui.com/forums/showthread.php?t=1830)

Randactyl 06/22/14 09:57 PM

Attempt to index a nil value confusion
 
Hi all.

I'm trying to access an variable with a value from my saved variables, but ESO is complaining that the variable is nil.

This is the line in question:

Lua Code:
  1. if(Imperialization.savedVariables.ConvertOnEquip == true) then

This is executed by a function fired by EVENT_INVENTORY_SINGLE_SLOT_UPDATE.

If this line is not present, references just a few lines down in the same function to other items in my saved variables are picked up just fine. The error also only comes up at login, but not after a ui reload.

I've tried deleting the saved variables .lua file and starting from scratch, but still this issue persists.

Any help would be greatly appreciated!

Sasky 06/22/14 11:55 PM

Is the variable for your SavedVars properly initialized before this?

The error is happening because either Imperialization is nil or Imperialization.savedVariables is nil.

(Also check for typo in those two. Always happens when you least expect it...)

farangkao 06/23/14 12:26 AM

And remember you can safeguard your code

if Imperialization.savedVariables then
if Imperialization.savedVariables.ConvertOnEquip then
-- do your stuff
end

else
d("Error No Saved Variables here")
end




When you do a test like "if <variable> then" ,the code will only run if the variable is not nil or not false (nil = means not initialized)

Garkin 06/23/14 03:18 AM

The easiest way to fix this issue is to register event after you have saved variables available - i.e. move event registration to the Initialize function:
Lua Code:
  1. function Imperialization:Initialize()
  2.     Imperialization.savedVariables = ZO_SavedVars:New("ImperializationVariables", Imperialization.version, nil, Imperialization.Default)
  3.     LAM2:RegisterAddonPanel("ImperializationSettings", panelData)
  4.     LAM2:RegisterOptionControls("ImperializationSettings", optionsData)
  5.     EVENT_MANAGER:RegisterForEvent(Imperialization.name, EVENT_INVENTORY_SINGLE_SLOT_UPDATE, Imperialization.OnInventorySlotUpdate)
  6.     EVENT_MANAGER:UnregisterForEvent(Imperialization.name, EVENT_ADD_ON_LOADED)
  7. end
  8.  
  9. EVENT_MANAGER:RegisterForEvent(Imperialization.name, EVENT_ADD_ON_LOADED, Imperialization.OnAddOnLoaded)

By the way I think it's not a good idea to use addon version as a version of your saved variables. It will reset everything to the defaults with each new addon version.

EDIT:
Another suggestion - Conditions you use looks rather complicated. If you use itemSyle as a key in your saved variables, you can use much easier condition:

lua Code:
  1. local itemStyle = select(7, GetItemInfo(bagID, slotID))
  2. if Imperialization.savedVariables[itemStyle] == true then
  3.     if(Imperialization.savedVariables.DisplayResults) then
  4.         d(zo_strformat("<<t:1>> converted from the <<2>> style!", GetItemLink(bagID, slotID, LINK_STYLE_BRACKETS), GetString("SI_ITEMSTYLE", itemStyle)))
  5.     end
  6. end

Randactyl 06/23/14 09:46 AM

Quote:

Originally Posted by Garkin (Post 9618)
The easiest way to fix this issue is to register event after you have saved variables available - i.e. move event registration to the Initialize function:
Lua Code:
  1. function Imperialization:Initialize()
  2.     Imperialization.savedVariables = ZO_SavedVars:New("ImperializationVariables", Imperialization.version, nil, Imperialization.Default)
  3.     LAM2:RegisterAddonPanel("ImperializationSettings", panelData)
  4.     LAM2:RegisterOptionControls("ImperializationSettings", optionsData)
  5.     EVENT_MANAGER:RegisterForEvent(Imperialization.name, EVENT_INVENTORY_SINGLE_SLOT_UPDATE, Imperialization.OnInventorySlotUpdate)
  6.     EVENT_MANAGER:UnregisterForEvent(Imperialization.name, EVENT_ADD_ON_LOADED)
  7. end
  8.  
  9. EVENT_MANAGER:RegisterForEvent(Imperialization.name, EVENT_ADD_ON_LOADED, Imperialization.OnAddOnLoaded)

By the way I think it's not a good idea to use addon version as a version of your saved variables. It will reset everything to the defaults with each new addon version.

EDIT:
Another suggestion - Conditions you use looks rather complicated. If you use itemSyle as a key in your saved variables, you can use much easier condition:

lua Code:
  1. local itemStyle = select(7, GetItemInfo(bagID, slotID))
  2. if Imperialization.savedVariables[itemStyle] == true then
  3.     if(Imperialization.savedVariables.DisplayResults) then
  4.         d(zo_strformat("<<t:1>> converted from the <<2>> style!", GetItemLink(bagID, slotID, LINK_STYLE_BRACKETS), GetString("SI_ITEMSTYLE", itemStyle)))
  5.     end
  6. end

Wow, Garkin you really went above and beyond! Thank you, and everyone else, so much for the help!
Now that it has been pointed out the mistake seems silly. And yes, my conditions quickly got out of hand:D
Now seems like the best time to reign all of this stuff back in.


All times are GMT -6. The time now is 08:33 AM.

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