Quantcast Variable in the table returns nil - ESOUI
Thread Tools Display Modes
06/01/20, 04:07 PM   #1
Unleash101
Join Date: Jun 2020
Posts: 5
Variable in the table returns nil

Hello!
I am learning to create add-ons and ran into a problem.
When I try to get a variable from any table, I get nil
Even if I try to get the variable myAddon.name

Lua Code:
  1. myAddon = {
  2.     name            = "myAddon",           -- Matches folder and Manifest file names.
  3.     version         = "1.0",                -- A nuisance to match to the Manifest.
  4.     author          = "Unleash101",
  5.     color           = "DDFFEE",             -- Used in menu titles and so on.
  6.     menuName        = "My Addon",          -- A UNIQUE identifier for menu object.
  7. }
  8.  
  9. -- Default settings.
  10. myAddon.defaultVars = {
  11.     firstLoad = true,                   -- First time the addon is loaded ever.
  12.     accountWide = false,                -- Load settings from account savedVars, instead of character.
  13.     greetingmes = true,
  14.     debugmes = false,
  15. }
  16.  
  17. myAddon.savedVars = {
  18.     firstLoad = true,                   -- First time the addon is loaded ever.
  19.     accountWide = false,                -- Load settings from account savedVars, instead of character.
  20.     --DEBUG
  21.     greetingmes = false,
  22.     debugmes = false,
  23. }
  24. --Vars
  25. local SGM_SavedVars = "myAddonvars";
  26. -- Wraps text with a color.
  27. function myAddon.Colorize(text, color)
  28.     -- Default to addon's .color.
  29.     if not color then color = myAddon.color end
  30.  
  31.     text = string.format('|c%s%s|r', color, text)
  32.  
  33.     return text
  34. end
  35.  
  36. function myAddon.showUserGUI(extra)
  37.     d("Work!");
  38.     if extra == "help" then
  39.         d("Help")
  40.     end
  41.  
  42. end
  43. SLASH_COMMANDS["/test101"] = myAddon.showUserGUI
  44.  
  45. -- Only show the loading message on first load ever.
  46. function myAddon.Activated(e)
  47.     EVENT_MANAGER:UnregisterForEvent(myAddon.name, EVENT_PLAYER_ACTIVATED)
  48.     --d(GetString(SI_DEBUG_MESSAGE))\
  49.     d(myAddon.name)
  50.    
  51. end
  52. -- When player is ready, after everything has been loaded.
  53. EVENT_MANAGER:RegisterForEvent(myAddon.name, EVENT_PLAYER_ACTIVATED, myAddon.Activated)
  54.  
  55. function myAddon.OnAddOnLoaded(event, addonName)
  56.    if addonName ~= myAddon.name then return end
  57.    EVENT_MANAGER:UnregisterForEvent(myAddon.name, EVENT_ADD_ON_LOADED)
  58.  
  59.     -- Load saved variables.
  60.     myAddon.characterSavedVars = ZO_SavedVars:New(SGM_SavedVars, 1, nil, myAddon.savedVars)
  61.     myAddon.accountSavedVars = ZO_SavedVars:NewAccountWide(SGM_SavedVars, 1, nil, myAddon.savedVars)
  62.  
  63.     if not myAddon.characterSavedVars.accountWide then
  64.         myAddon.savedVars = myAddon.characterSavedVars
  65.     else
  66.         myAddon.savedVars = myAddon.accountSavedVars
  67.     end
  68.    
  69. end
  70. -- When any addon is loaded, but before UI (Chat) is loaded.
  71. EVENT_MANAGER:RegisterForEvent(myAddon.name, EVENT_ADD_ON_LOADED, myAddon.OnAddOnLoaded)
  Reply With Quote
06/01/20, 04:16 PM   #2
Baertram
 
Baertram's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 2,951
Cannot see any reason from the code except that myAddon is not "unique" and every other of your active addons could define such a variable
Better choose something unique like "MyAddonForTest1" or similar or make sure all other addons are disabled as you test.

What line is the error message telling you the error is? Please copy&paste post the complete error message from the error popup.
You should also "expand" the error message so you can see the stack traceback (what way the loading of the functions took) and the variables and their contents. There you are able to see on first sight if any variable, and which one, does not have the value you did expect it has.

Also make sure the folder name and the manifest txt file name of your addon are exactly the same!


Oh and whereever you have taken the example from please do not use ZO_SavedVars:New for character SavedVariables anymore as these will use the charactername and are not rename safe.
Better use ZO_SavedVars:NewCharacterIdSettings instead now.
  Reply With Quote
06/01/20, 04:24 PM   #3
Unleash101
Join Date: Jun 2020
Posts: 5
Originally Posted by Baertram View Post
Cannot see any reason from the code except that myAddon is not "unique" and every other of your active addons could define such a variable
Better choose something unique like "MyAddonForTest1" or similar or make sure all other addons are disabled as you test.

What line is the error message telling you the error is? Please copy&paste post the complete error message from the error popup.
You should also "expand" the error message so you can see the stack traceback (what way the loading of the functions took) and the variables and their contents. There you are able to see on first sight if any variable, and which one, does not have the value you did expect it has.

Also make sure the folder name and the manifest txt file name of your addon are exactly the same!


Oh and whereever you have taken the example from please do not use ZO_SavedVars:New for character SavedVariables anymore as these will use the charactername and are not rename safe.
Better use ZO_SavedVars:NewCharacterIdSettings instead now.
Lua Code:
  1. function myAddon.Activated(e)
  2.     EVENT_MANAGER:UnregisterForEvent(myAddon.name, EVENT_PLAYER_ACTIVATED)
  3.     --d(GetString(SI_DEBUG_MESSAGE))\
  4.     d(myAddon.name)
  5.    
  6. end

This function sends nil to the chat, although it should send myAddon

I checked the name of the myAddon folder
Moreover, I disabled all add-ons during the test.

Thanks for the advice. I will use ZO_SavedVars: NewCharacterIdSettings
  Reply With Quote
06/01/20, 04:29 PM   #4
Baertram
 
Baertram's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 2,951
Lightbulb

Move the line here into your function event_add_on_laoded callback!

Lua Code:
  1. -- When player is ready, after everything has been loaded.
  2. EVENT_MANAGER:RegisterForEvent(myAddon.name, EVENT_PLAYER_ACTIVATED, myAddon.Activated)

It's currently called too early as it seems and not related to your addon.
Better register events AFTER your addon has loaded via event_add_on_loaded, means after this line:
if addonName ~= myAddon.name then return end

Lua Code:
  1. function myAddon.OnAddOnLoaded(event, addonName)
  2.    if addonName ~= myAddon.name then return end
  3.    EVENT_MANAGER:UnregisterForEvent(myAddon.name, EVENT_ADD_ON_LOADED)
  4.  
  5.     -- Load saved variables.
  6.     myAddon.characterSavedVars = ZO_SavedVars:New(SGM_SavedVars, 1, nil, myAddon.savedVars)
  7.     myAddon.accountSavedVars = ZO_SavedVars:NewAccountWide(SGM_SavedVars, 1, nil, myAddon.savedVars)
  8.  
  9.     if not myAddon.characterSavedVars.accountWide then
  10.         myAddon.savedVars = myAddon.characterSavedVars
  11.     else
  12.         myAddon.savedVars = myAddon.accountSavedVars
  13.     end
  14.  
  15.    -- When player is ready, after everything has been loaded.
  16.    EVENT_MANAGER:RegisterForEvent(myAddon.name, EVENT_PLAYER_ACTIVATED, myAddon.Activated)
  17.  
  18.    
  19. end
  20. -- When any addon is loaded, but before UI (Chat) is loaded.
  21. EVENT_MANAGER:RegisterForEvent(myAddon.name, EVENT_ADD_ON_LOADED, myAddon.OnAddOnLoaded)

This makes sure your SavedVAriables have been loaded as well BEFORE the player activated event callback fires for your addon.
  Reply With Quote
06/01/20, 04:44 PM   #5
Unleash101
Join Date: Jun 2020
Posts: 5
Originally Posted by Baertram View Post
Move the line here into your function event_add_on_laoded callback!

Lua Code:
  1. -- When player is ready, after everything has been loaded.
  2. EVENT_MANAGER:RegisterForEvent(myAddon.name, EVENT_PLAYER_ACTIVATED, myAddon.Activated)

It's currently called too early as it seems and not related to your addon.
Better register events AFTER your addon has loaded via event_add_on_loaded, means after this line:
if addonName ~= myAddon.name then return end

Lua Code:
  1. function myAddon.OnAddOnLoaded(event, addonName)
  2.    if addonName ~= myAddon.name then return end
  3.    EVENT_MANAGER:UnregisterForEvent(myAddon.name, EVENT_ADD_ON_LOADED)
  4.  
  5.     -- Load saved variables.
  6.     myAddon.characterSavedVars = ZO_SavedVars:New(SGM_SavedVars, 1, nil, myAddon.savedVars)
  7.     myAddon.accountSavedVars = ZO_SavedVars:NewAccountWide(SGM_SavedVars, 1, nil, myAddon.savedVars)
  8.  
  9.     if not myAddon.characterSavedVars.accountWide then
  10.         myAddon.savedVars = myAddon.characterSavedVars
  11.     else
  12.         myAddon.savedVars = myAddon.accountSavedVars
  13.     end
  14.  
  15.    -- When player is ready, after everything has been loaded.
  16.    EVENT_MANAGER:RegisterForEvent(myAddon.name, EVENT_PLAYER_ACTIVATED, myAddon.Activated)
  17.  
  18.    
  19. end
  20. -- When any addon is loaded, but before UI (Chat) is loaded.
  21. EVENT_MANAGER:RegisterForEvent(myAddon.name, EVENT_ADD_ON_LOADED, myAddon.OnAddOnLoaded)

This makes sure your SavedVAriables have been loaded as well BEFORE the player activated event callback fires for your addon.
Moved EVENT_ADD_ON_LOADED.

But this did not solve the problem. As I saw nil in the chat instead of myAddon, I see it.
  Reply With Quote
06/01/20, 04:45 PM   #6
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,397
Do you have an xml file with a control which is also named "myAddon"? If so, it's overwriting your global table with the control.
__________________
https://www.imperialtradingcompany.eu/ - My Addons - The Vault (Early updates and experimental projects) - My patreon - My blog
>siri.exe MyAddon
Does your addon work? [y/n] n
There is a typo in there.
  Reply With Quote
06/01/20, 04:49 PM   #7
Unleash101
Join Date: Jun 2020
Posts: 5
Originally Posted by sirinsidiator View Post
Do you have an xml file with a control which is also named "myAddon"? If so, it's overwriting your global table with the control.
Xml Code:
  1. <GuiXml>
  2. <Controls>
  3. <TopLevelControl name="myAddonActive" hidden="true" alpha="0.0">
  4.     <Dimensions x="200" y="25" />
  5.     <Anchor point="BOTTOM" relativeTo="GuiRoot" relativePoint="CENTER" offsetY="-20" />
  6.         <Controls>
  7.         <Label name="$(parent)Label" width="200" height="25" font="ZoFontWinH1" color="FFFFFF"
  8.        wrapMode="TRUNCATE" verticalAlignment="TOP" horizontalAlignment="CENTER"
  9.        text="myAddon is active!">
  10.     <Anchor point="TOP" relativeTo="$(parent)" relativePoint="TOP" />
  11. </Label>
  12. </Controls>
  13. </TopLevelControl>
  14. </Controls>
  15. </GuiXml>

Here is my XML GUI
  Reply With Quote
06/01/20, 04:58 PM   #8
Unleash101
Join Date: Jun 2020
Posts: 5
I am an idiot!
I have a settings.lua file that creates the settings panel via libaddonmenu. So in
Lua Code:
  1. LAM:RegisterAddonPanel("myAddon", panelData)
  2.     LAM:RegisterOptionControls("myAddon", optionsTable)
I indicated myAddon, but you need to specify myAddon-settings.
Changed and it worked.

@sirinsidiator thanks for pushing everything to check!
@Baertram thanks for the good tips!
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Variable in the table returns nil

Thread Tools
Display Modes

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