Thread Tools Display Modes
03/25/15, 09:58 AM   #1
Woeler
 
Woeler's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 40
Saving hidden state

Hello all,

I want my addon to be set hidden in the options. I also want this to be saved in the savedVariables. However I constantly keep getting

Code:
user:/AddOns/RaidScore/RaidScore.lua:47: attempt to index a nil value
stack traceback:
	user:/AddOns/RaidScore/RaidScore.lua:47: in function 'func'
	user:/AddOns/CombatCloud/libs/LibAddonMenu/controls/button.lua:61: in function '(anonymous)'
And I have no idea what I'm doing wrong. I've tried countless constructions. Anyone who can help me? I'm quite new to this.

Lua Code:
  1. --Addon: RaidScore
  2. --Game: The Elder Scrolls Online: Tamriel Unlimited
  3. --Author: Woeler
  4.  
  5. local RS = {
  6.     Menu = LibStub:GetLibrary("LibAddonMenu-2.0"),
  7.     hideScore = true,
  8.     }
  9.  
  10. --Menu
  11.     RS.panelData = {
  12.         type = "panel",
  13.         name = "RaidScore",
  14.         author = "Woeler",
  15.         version = "1.0.2",
  16.         registerForRefresh = false,
  17.         }
  18.        
  19.    
  20.     RS.optionsData = {
  21.         [1] = {
  22.         type = "description",
  23.         text = "Welcome to RaidScore by Woeler (EU: @Woeler)",
  24.         width = "full",
  25.         },
  26.         [2] = {
  27.               type = "checkbox",
  28.               name = "Show/Hide RaidScore",
  29.               tooltip = "This will reset once you zone or ReloadUI",
  30.               getFunc = function() return hideScore end,
  31.               setFunc = function(value)
  32.               if value == true then
  33.                 savedVariables.HideScores = false
  34.                 RAIDSCORE:SetHidden(false)
  35.               elseif value == false then
  36.                 savedVariables.HideScores = true
  37.                 RAIDSCORE:SetHidden(true)
  38.               end
  39.              
  40.               end,
  41.          },
  42. --      [3] = {
  43. --      type = "button",
  44. --      name = "Apply",
  45. --      tooltip = "Apply the changes.",
  46. --      func = function()
  47. --      savedVariables.Scores = RS.hideScore
  48. --      end,
  49.  
  50.  
  51.         width = "full",
  52.        
  53.     },
  54.    
  55.  
  56.     RS.Menu:RegisterAddonPanel("RaidTimerPanel", RS.panelData)
  57.     RS.Menu:RegisterOptionControls("RaidTimerPanel", RS.optionsData)
  58.  
  59. --function for getting the current score
  60. function RaidScoreUpdate()
  61.     RAIDSCORECounter:SetText(string.format(GetCurrentRaidScore()))
  62. end
  63.  
  64.  
  65.     local savedVariables --local reference for saved variables, actual object will be assigned later in EVENT_ADD_ON_LOADED event handler
  66.     local defaults = { --default values for saved variables
  67.         offsetX = 200,
  68.         offsetY = 200,
  69.         HideScores = false
  70.     }
  71.      
  72.     --event handler for OnMoveStop
  73.     local function OnMoveStop(self)
  74.         savedVariables.offsetX = self:GetLeft()
  75.         savedVariables.offsetY = self:GetTop()
  76.     end
  77.      
  78.     --event handler for EVENT_ADD_ON_LOADED
  79.     local function OnAddonLoaded(event, addonName)
  80.         if addonName == "RaidScore" then --addonName is in general name of your addon manifext without .txt extension
  81.      
  82.             EVENT_MANAGER:UnregisterForEvent(MoveHandler, EVENT_ADD_ON_LOADED)
  83.      
  84.             --saved variables (in this case account wide)
  85.             savedVariables = ZO_SavedVars:NewAccountWide("RaidScore", 1, "namespace", defaults)
  86.      
  87.             --create top level window, so we have something to work with
  88.             RAIDSCORE:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, savedVariables.offsetX, savedVariables.offsetY) --restore window position from saved variables
  89.             RAIDSCORE:SetHandler("OnMoveStop", OnMoveStop) --set handler to event "OnMoveStop", it means when you stop moving the window
  90.             RAIDSCORE:SetHidden(savedVariables.HideScores) --set window invisible or visible
  91.  
  92.  
  93.         end
  94.     end
  95.      
  96.     EVENT_MANAGER:RegisterForEvent(MoveHandler, EVENT_ADD_ON_LOADED, OnAddonLoaded)

Last edited by Woeler : 03/25/15 at 10:32 AM.
  Reply With Quote
03/25/15, 10:33 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,028
Edit:
@Woeler
If you edit your sourcecode in the post after we are already answering it... this won't really help :-(
I was just wondering why I have a different source code that I've checked for you, then the sourc code that you have in your post NOW.
All of sudden variable names changed and source lines were commented?



My answer was:
Check your optionsData where you have set your variable "hideScore":

Lua Code:
  1. [2] = {
  2.         type = "checkbox",
  3.         name = "Show RaidScore always",
  4.         tooltip = "This will reset once you zone or ReloadUI",
  5.         getFunc = function() return hideScore end,
  6.         setFunc = function(value)
  7.             if value == true then
  8.                 hideScore = false
  9.                 RAIDSCORE:SetHidden(false)
  10.             elseif value == false then
  11.                 hideScore = true
  12.                 RAIDSCORE:SetHidden(true)
  13.             end
  14.         end,
  15.     },

1. hideScore here is a global variable because you did not define it as local
2. Or do you want to change the variable RS.hideScore and you just forgot the "RS." ?

For option 2 it should look like something like this:

Lua Code:
  1. [2] = {
  2.         type = "checkbox",
  3.         name = "Show RaidScore always",
  4.         tooltip = "This will reset once you zone or ReloadUI",
  5.         getFunc = function() return RS.hideScore end,
  6.         setFunc = function(value)
  7.             RS.hideScore = not value
  8.             RAIDSCORE:SetHidden(RS.hideScore)
  9.         end,
  10.     },


In addition your saved variables table/array is using the variable Scores in your options menu, if you press the "save" button:
Lua Code:
  1. [3] = {
  2.         type = "button",
  3.         name = "Apply",
  4.         tooltip = "Apply the changes.",
  5.         func = function()
  6.             savedVariables.Scores = RS.hideScore
  7.         end,
  8.  
  9.         width = "full",
  10.  
  11.     },

But you have initialized it with the default saved variables data local defaults to be named "HideScores":
Lua Code:
  1. local defaults = { --default values for saved variables
  2.     offsetX = 200,
  3.     offsetY = 200,
  4.     HideScores = false
  5. }

So I assume it should look like this for your button to save the data:
Lua Code:
  1. [3] = {
  2.         type = "button",
  3.         name = "Apply",
  4.         tooltip = "Apply the changes.",
  5.         func = function()
  6.             savedVariables.HideScores = RS.hideScore
  7.         end,
  8.         width = "full",
  9.     },


As a better explanation:
1. You define some default data for the saved variables which will be loaded if the saved variables do not have any stored data (1st time your addon loads e.g.). You haev defined them in your table "defaults":
Lua Code:
  1. local defaults = { --default values for saved variables
  2.     offsetX = 200,
  3.     offsetY = 200,
  4.     HideScores = false
  5. }
2. You load your saved variables from the local file stored on your harddrive if the addon loads, in your function "OnAddonLoaded":

Lua Code:
  1. savedVariables = ZO_SavedVars:NewAccountWide("RaidScore", 1, "namespace", defaults)

Your addon will load all data from the stored file and if nothing is found it will use the content of your variable "defaults" as the default values.

3. In your settings you will be able to change values and then update your saved variables with the updated values. You can do this directly by changing "savedVariables.HideScores" e.g., of like you do by changing a helper variable table "RS" witht he entry "hideScore", and afterwards assign the content of your helper variable to your "savedVariables" table.

4. If your game client is logging out, doing a /reloadui or exits the variable "savedVariables" of your addon will be written to the .LUA file on your local harddrive.
The harddrive's filename will have the same name like your addons does and will be stored inside the SavedVariables folder in "c:\users\<your windows username>\documents\Elder Scrolls Online\live (or liveeu)\SavedVariables\". Inside this .LUA file you will find the stored settings in the array structured that you have specified in your addon's .TXT manifest file in this statement:

Code:
## SavedVariables: <your saved variables name>
Below this structured table you will find the settings saved account wide, or for each of your characters (depends on what method you use to save the savedvariables) and below you will find your content of the table "savedVariables" ->
You'll find "offsetX", "offsetY" and "HideScores" in there with their values.

Last edited by Baertram : 03/25/15 at 10:53 AM.
  Reply With Quote
03/25/15, 10:41 AM   #3
votan
 
votan's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 578
savedVariables is declared after the options. In LUA the order is important. In line 47 savedVariables is still unknown.
  Reply With Quote
03/25/15, 11:22 AM   #4
Woeler
 
Woeler's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 40
Your solutions combined did it! Thanks for the help guys!

Last edited by Woeler : 03/25/15 at 11:32 AM.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Saving hidden state


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