Thread Tools Display Modes
11-03-22, 08:52 PM   #1
Baz4k
A Deviate Faerie Dragon
 
Baz4k's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2008
Posts: 12
I am getting my butt kicked by saved variables.

Bear with me, I am learning as I go. I am making an addon that can hide the new Dragonflight Micro Menu bar. I am learning by looking at other peoples online tutorials and it's slow, but I am making progress.

I've hit a wall when it comes to saving and loading variables. I have two variables that I am attempting to load. They are Booleans called BagButtonVisable and MicroMenuVisable.

I added them to the toc and successfully got them to save to the file, but they never loaded. I found some great tutorials from Phanx, I think they are a above my current level.

I will include what I have done so far. Any help would be greatly appreciated.

Lua Code:
  1. --Setup variables saving/loading
  2. ----------------------------------------------------------------------------------
  3.  
  4. HideMicroMenuDBPC = { }
  5.  
  6. function HideMicroMenuDBPC:PLAYER_LOGIN()
  7.     -- This table defines the addon's default settings:
  8.     local defaults = {
  9.         BagButtonVisable = true,
  10.         MicroMenuVisable = true,
  11.     }
  12.  
  13.     -- This function copies values from one table into another:
  14.     local function copyDefaults(src, dst)
  15.         -- If no source (defaults) is specified, return an empty table:
  16.         if type(src) ~= "table" then return {} end
  17.         -- If no target (saved variable) is specified, create a new table:
  18.         if not type(dst) then dst = {} end
  19.         -- Loop through the source (defaults):
  20.         for k, v in pairs(src) do
  21.             -- If the value is a sub-table:
  22.             if type(v) == "table" then
  23.                 -- Recursively call the function:
  24.                 dst[k] = copyDefaults(v, dst[k])
  25.             -- Or if the default value type doesn't match the existing value type:
  26.             elseif type(v) ~= type(dst[k]) then
  27.                 -- Overwrite the existing value with the default one:
  28.                 dst[k] = v
  29.             end
  30.         end
  31.         -- Return the destination table:
  32.         return dst
  33.     end
  34.  
  35.     -- Copy the values from the defaults table into the saved variables table
  36.     -- if it exists, and assign the result to the saved variable:
  37.     HideMicroMenuDBPC = copyDefaults(defaults, HideMicroMenuDBPC)
  38. end
  39.  
  40.  
  41.  
  42.  
  43. --Setup Options Panel
  44. ----------------------------------------------------------------------------------
  45. local Frame = CreateFrame("Frame")
  46. Frame:RegisterEvent("PLAYER_LOGIN")
  47.  
  48. HideMicroMenu = {};
  49. HideMicroMenu.panel = CreateFrame( "Frame", "HideMicroMenuPanel", UIParent );
  50. HideMicroMenu.panel.name = "Hide Micro Menu";
  51. InterfaceOptions_AddCategory(HideMicroMenu.panel);
  52.  
  53. -- Create BagButton Frame
  54. local BagButtonFrame = CreateFrame("Frame", nil, nil, "BackdropTemplate")
  55. BagButtonFrame:SetSize(50, 50)
  56. BagButtonFrame:SetBackdrop(BACKDROP_TUTORIAL_16_16)
  57. BagButtonFrame:ClearBackdrop()
  58. BagButtonFrame:SetPoint("CENTER", MainMenuBarBackpackButton, "CENTER", 0, -0)
  59.  
  60. --BagButton Button in Options
  61. BagButton = CreateFrame("Button", "BagButton", HideMicroMenuPanel, "UIPanelButtonTemplate");
  62. BagButton:SetSize(200 ,40)
  63. BagButton:SetText("Toggle Bag Button Visability")
  64. BagButton:SetPoint("TOPLEFT", 35, -65);
  65. BagButton:SetScript("OnClick",
  66.   function()
  67.     if BagButtonVisable == true then
  68.         BagButtonFrame:Hide()
  69.         BagButtonVisable = false
  70.         print ("BagButton ", BagButtonVisable)
  71.     else
  72.         BagButtonFrame:Show()
  73.         BagButtonVisable = true
  74.         print ("BagButton ", BagButtonVisable)
  75.     end
  76.   end
  77. );
  78.  
  79.  
  80. --MicroMenu Button in Options
  81. MicroMenu = CreateFrame("Button", "MicroMenu", HideMicroMenuPanel, "UIPanelButtonTemplate");
  82. MicroMenu:SetSize(200 ,40)
  83. MicroMenu:SetText("Toggle Micro Menu Visability")
  84. MicroMenu:SetPoint("TOPLEFT", 35, -165);
  85. MicroMenu:SetScript("OnClick",
  86.   function()
  87.     if MicroMenuVisable == true then
  88.         MicroButtonAndBagsBar:Hide()
  89.         MicroMenuVisable = false
  90.         print ("MicroMenu ", MicroMenuVisable)
  91.     else
  92.         MicroButtonAndBagsBar:Show()
  93.         MicroMenuVisable = true
  94.         print ("MicroMenu ", MicroMenuVisable)
  95.     end
  96.   end
  97. );
  98.  
  99.  
  100. --Setup Bag Button
  101. ----------------------------------------------------------------------------------
  102.  
  103.  
  104. MainMenuBarBackpackButton:SetParent(BagButtonFrame)
  105. MainMenuBarBackpackButton:SetScale (0.65)
  106. BagBarExpandToggle:ClearAllPoints()
  107. BagBarExpandToggle:SetParent(BagButtonFrame)
  108.  
  109.  
  110. if (BagButtonVisable == false) then
  111.     BagButtonFrame:Hide()
  112. else
  113.     BagButtonFrame:Show()
  114. end
  115.  
  116.  
  117. --Setup Micro Menu
  118. ----------------------------------------------------------------------------------
  119.  
  120. if (MicroMenuVisable == false) then
  121.     MicroButtonAndBagsBar:Hide()
  122. else
  123.     MicroButtonAndBagsBar:Show()
  124. end
  125.  
  126.  
  127. --Setup Queue Status
  128. ----------------------------------------------------------------------------------
  129.  
  130. local QueueButtonFrame = CreateFrame("Frame", nil, nil, "BackdropTemplate")
  131. QueueButtonFrame:SetPoint("CENTER", QueueStatusButton, "CENTER", 0, -0)
  132. QueueButtonFrame:SetSize(50, 50)
  133. QueueButtonFrame:SetBackdrop(BACKDROP_TUTORIAL_16_16)
  134. QueueButtonFrame:ClearBackdrop()
  135. QueueButtonFrameCenter = QueueButtonFrame:GetCenter()
  136. print (QueueButtonFrameCenter)
  137.  
  138. QueueStatusButton:SetParent(QueueButtonFrame)
  139. QueueStatusButton:SetScale (0.65)
  140.  
  141. --Make Queue Status Frame Draggable
  142. QueueStatusButton:SetMovable(true)
  143. QueueStatusButton:EnableMouse(true)
  144. QueueStatusButton:RegisterForDrag("LeftButton")
  145. QueueStatusButton:SetScript("OnDragStart", function(self, button)
  146.     self:StartMoving()
  147. end)
  148. QueueStatusButton:SetScript("OnDragStop", function(self)
  149.     self:StopMovingOrSizing()
  150. end)
  151. QueueStatusButton:SetUserPlaced(true)
  Reply With Quote
11-03-22, 10:42 PM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Saved variables are not loaded when the client loads your addon files. The variables are loaded after. You need to wait at least until the ADDON_LOADED event fires, PLAYER_LOGIN is fine.

Also, your code has a few discrepancies:

HideMicroMenuDBPC does not interact with your two boolean saved variables.
That table object is not a frame, so it will never trigger the PLAYER_LOGIN function.
The frame object "Frame" you did register PLAYER_LOGIN to is not used anywhere else.
  Reply With Quote
11-04-22, 11:21 AM   #3
Baz4k
A Deviate Faerie Dragon
 
Baz4k's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2008
Posts: 12
Thanks for your help

Okay, I took some time to completely rewrite my code since I felt more confident that I understood what I was doing a little better. I now have the variables saving and loading properly, but I am not getting the desired result. I can tell that they are saving by looking at the lua file and I have a print command that fires when the game loads to tell that they are loading properly. I am trying to set the visibility of the Bag Button and the Micro Menu by using the saved variable. If the variable is FALSE then they should be hidden and visible when the variable is TRUE. But most of the time the Bag Button (for instance) will be visible even when the variable prints as FALSE.

What am I doing wrong?

Lua Code:
  1. ---------------------------------------
  2. --Setup variables saving/loading
  3. ---------------------------------------
  4.  
  5. HMMVars = {
  6.     BagButtonVisable = true,
  7.     MicroMenuVisable = true,
  8. }
  9.  
  10. local HMMFrame = CreateFrame("Frame")
  11. HMMFrame:RegisterEvent("ADDON_LOADED")
  12. HMMFrame:SetScript("OnEvent", function(self, event, addon)
  13.     if addon == "Hide Micro Menu" then
  14.         print("BagButton ", HMMVars.BagButtonVisable)
  15.         print("MicroMenu ", HMMVars.MicroMenuVisable)
  16.     end
  17. end)
  18.  
  19. ---------------------------------------
  20. --Create BagButton Frame
  21. ---------------------------------------
  22.  
  23. local BagButtonFrame = CreateFrame("Frame", nil, nil, "BackdropTemplate")
  24. BagButtonFrame:SetSize(50, 50)
  25. BagButtonFrame:SetBackdrop(BACKDROP_TUTORIAL_16_16)
  26. BagButtonFrame:ClearBackdrop()
  27. BagButtonFrame:SetPoint("CENTER", MainMenuBarBackpackButton, "CENTER", 0, -0)
  28. MainMenuBarBackpackButton:SetParent(BagButtonFrame)
  29. MainMenuBarBackpackButton:SetScale (0.65)
  30.  
  31. ---------------------------------------
  32. --Set BagButton Visability to variable
  33. ---------------------------------------
  34.  
  35. BagBarExpandToggle:SetParent(MainMenuBarBackpackButton) --Set the bag arrow as a child of the BagButton
  36.  
  37. if (HMMVars.BagButtonVisable == true) then
  38.     BagButtonFrame:Hide()
  39. else
  40.     BagButtonFrame:Show()
  41. end
  42.  
  43. ---------------------------------------
  44. --Set MicroMenu Visability to variable
  45. ---------------------------------------
  46.  
  47. if (HMMVars.MicroMenuVisable == false) then
  48.     MicroButtonAndBagsBar:Hide()
  49. else
  50.     MicroButtonAndBagsBar:Show()
  51. end
  52.  
  53. ---------------------------------------
  54. --Create Options Panel
  55. ---------------------------------------
  56.  
  57. local Frame = CreateFrame("Frame")
  58. Frame:RegisterEvent("PLAYER_LOGIN")
  59.  
  60. HideMicroMenu = {};
  61. HideMicroMenu.panel = CreateFrame( "Frame", "HideMicroMenuPanel", UIParent );
  62. HideMicroMenu.panel.name = "Hide Micro Menu";
  63. InterfaceOptions_AddCategory(HideMicroMenu.panel);
  64.  
  65. ---------------------------------------
  66. --Create BagButton Button in Options
  67. ---------------------------------------
  68.  
  69. BagButton = CreateFrame("Button", "BagButton", HideMicroMenuPanel, "UIPanelButtonTemplate");
  70. BagButton:SetSize(200 ,40)
  71. BagButton:SetText("Toggle Bag Button Visability")
  72. BagButton:SetPoint("TOPLEFT", 35, -65);
  73. BagButton:SetScript("OnClick",
  74.   function()
  75.     if HMMVars.BagButtonVisable == true then
  76.         BagButtonFrame:Hide()
  77.         HMMVars.BagButtonVisable = false
  78.         print ("BagButton ", HMMVars.BagButtonVisable)
  79.     else
  80.         BagButtonFrame:Show()
  81.         HMMVars.BagButtonVisable = true
  82.         print ("BagButton ", HMMVars.BagButtonVisable)
  83.     end
  84.   end
  85. );
  86.  
  87. ---------------------------------------
  88. --Create MicroMenu Button in Options
  89. ---------------------------------------
  90.  
  91. MicroMenu = CreateFrame("Button", "MicroMenu", HideMicroMenuPanel, "UIPanelButtonTemplate");
  92. MicroMenu:SetSize(200 ,40)
  93. MicroMenu:SetText("Toggle Micro Menu Visability")
  94. MicroMenu:SetPoint("TOPLEFT", 35, -165);
  95. MicroMenu:SetScript("OnClick",
  96.   function()
  97.     if HMMVars.MicroMenuVisable == true then
  98.         MicroButtonAndBagsBar:Hide()
  99.         HMMVars.MicroMenuVisable = false
  100.         print ("MicroMenu ", HMMVars.MicroMenuVisable)
  101.     else
  102.         MicroButtonAndBagsBar:Show()
  103.         HMMVars.MicroMenuVisable = true
  104.         print ("MicroMenu ", HMMVars.MicroMenuVisable)
  105.     end
  106.   end
  107. );
  108.  
  109. ---------------------------------------
  110. --Setup Queue Status Icon
  111. ---------------------------------------
  112.  
  113. local QueueButtonFrame = CreateFrame("Frame", nil, nil, "BackdropTemplate")
  114. QueueButtonFrame:SetPoint("CENTER", QueueStatusButton, "CENTER", 0, -0)
  115. QueueButtonFrame:SetSize(50, 50)
  116. --QueueButtonFrame:SetBackdrop(BACKDROP_TUTORIAL_16_16)
  117. --QueueButtonFrame:ClearBackdrop()
  118.  
  119. QueueStatusButton:SetParent(QueueButtonFrame)
  120. QueueStatusButton:SetScale (0.65)
  121. QueueStatusButton:SetMovable(true)
  122. QueueStatusButton:EnableMouse(true)
  123. QueueStatusButton:RegisterForDrag("LeftButton")
  124. QueueStatusButton:SetScript("OnDragStart", function(self, button)
  125.     self:StartMoving()
  126. end)
  127. QueueStatusButton:SetScript("OnDragStop", function(self)
  128.     self:StopMovingOrSizing()
  129. end)
  130. QueueStatusButton:SetUserPlaced(true)
  Reply With Quote
11-05-22, 09:40 AM   #4
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Like I said, saved variables aren't loaded until the ADDON_LOADED event fires. While you solved printing them to chat, the rest of your code is still trying to use them at file load.

https://wowpedia.fandom.com/wiki/AddOn_loading_process

The client reads your toc file, then it loads files you listed executing all lines of code as they load. Once all files in a toc are loaded, the client then loads the associated saved variables, then finally fires ADDON_LOADED. This means, for example, at line 37 in your second post here, "HMMVars.BagButtonVisable" will always be nil, not true or false.

You also still have that unused Frame object with PLAYER_LOGIN registered. Since you haven't shared your toc file, I'm also assuming your only saved variable is currently the table object HMMVars and not the individual booleans you spoke of originally.

I put everything in the PLAYER_LOGIN event:

Lua Code:
  1. ---------------------------------------
  2. --create saved variable object
  3. ---------------------------------------
  4.  
  5. HMMVars = {}
  6.  
  7. ---------------------------------------
  8. --verify saved variable loaded
  9. ---------------------------------------
  10.  
  11. local HMMFrame = CreateFrame("Frame")
  12. HMMFrame:RegisterEvent("ADDON_LOADED")
  13. HMMFrame:SetScript("OnEvent", function(self, event, addon)
  14.     if addon == "Hide Micro Menu" then
  15.         print("BagButton ", HMMVars.BagButtonVisable)
  16.         print("MicroMenu ", HMMVars.MicroMenuVisable)
  17.     end
  18. end)
  19.  
  20. ---------------------------------------
  21. --delay everything until login event
  22. ---------------------------------------
  23.  
  24. local frame=CreateFrame("frame")
  25. frame:RegisterEvent("PLAYER_LOGIN")
  26. frame:SetScript("OnEvent",function()
  27.  
  28.     ---------------------------------------
  29.     --Create BagButton Frame
  30.     ---------------------------------------
  31.  
  32.     local BagButtonFrame = CreateFrame("Frame", nil, nil, "BackdropTemplate")
  33.     BagButtonFrame:SetSize(50, 50)
  34.     BagButtonFrame:SetBackdrop(BACKDROP_TUTORIAL_16_16)
  35.     BagButtonFrame:ClearBackdrop()
  36.     BagButtonFrame:SetPoint("CENTER", MainMenuBarBackpackButton, "CENTER", 0, -0)
  37.     MainMenuBarBackpackButton:SetParent(BagButtonFrame)
  38.     MainMenuBarBackpackButton:SetScale (0.65)
  39.  
  40.     ---------------------------------------
  41.     --Set BagButton Visability to variable
  42.     ---------------------------------------
  43.  
  44.     BagBarExpandToggle:SetParent(MainMenuBarBackpackButton) --Set the bag arrow as a child of the BagButton
  45.  
  46.     if (HMMVars.BagButtonVisable == true) then
  47.         BagButtonFrame:Hide()
  48.     else
  49.         BagButtonFrame:Show()
  50.     end
  51.  
  52.     ---------------------------------------
  53.     --Set MicroMenu Visability to variable
  54.     ---------------------------------------
  55.  
  56.     if (HMMVars.MicroMenuVisable == false) then
  57.         MicroButtonAndBagsBar:Hide()
  58.     else
  59.         MicroButtonAndBagsBar:Show()
  60.     end
  61.  
  62.     ---------------------------------------
  63.     --Create Options Panel
  64.     ---------------------------------------
  65.  
  66.     HideMicroMenu = {};
  67.     HideMicroMenu.panel = CreateFrame( "Frame", "HideMicroMenuPanel", UIParent );
  68.     HideMicroMenu.panel.name = "Hide Micro Menu";
  69.     InterfaceOptions_AddCategory(HideMicroMenu.panel);
  70.  
  71.     ---------------------------------------
  72.     --Create BagButton Button in Options
  73.     ---------------------------------------
  74.  
  75.     BagButton = CreateFrame("Button", "BagButton", HideMicroMenuPanel, "UIPanelButtonTemplate");
  76.     BagButton:SetSize(200 ,40)
  77.     BagButton:SetText("Toggle Bag Button Visability")
  78.     BagButton:SetPoint("TOPLEFT", 35, -65);
  79.     BagButton:SetScript("OnClick",
  80.       function()
  81.         if HMMVars.BagButtonVisable == true then
  82.             BagButtonFrame:Hide()
  83.             HMMVars.BagButtonVisable = false
  84.             print ("BagButton ", HMMVars.BagButtonVisable)
  85.         else
  86.             BagButtonFrame:Show()
  87.             HMMVars.BagButtonVisable = true
  88.             print ("BagButton ", HMMVars.BagButtonVisable)
  89.         end
  90.       end
  91.     );
  92.  
  93.     ---------------------------------------
  94.     --Create MicroMenu Button in Options
  95.     ---------------------------------------
  96.  
  97.     MicroMenu = CreateFrame("Button", "MicroMenu", HideMicroMenuPanel, "UIPanelButtonTemplate");
  98.     MicroMenu:SetSize(200 ,40)
  99.     MicroMenu:SetText("Toggle Micro Menu Visability")
  100.     MicroMenu:SetPoint("TOPLEFT", 35, -165);
  101.     MicroMenu:SetScript("OnClick",
  102.       function()
  103.         if HMMVars.MicroMenuVisable == true then
  104.             MicroButtonAndBagsBar:Hide()
  105.             HMMVars.MicroMenuVisable = false
  106.             print ("MicroMenu ", HMMVars.MicroMenuVisable)
  107.         else
  108.             MicroButtonAndBagsBar:Show()
  109.             HMMVars.MicroMenuVisable = true
  110.             print ("MicroMenu ", HMMVars.MicroMenuVisable)
  111.         end
  112.       end
  113.     );
  114.  
  115.     ---------------------------------------
  116.     --Setup Queue Status Icon
  117.     ---------------------------------------
  118.  
  119.     local QueueButtonFrame = CreateFrame("Frame", nil, nil, "BackdropTemplate")
  120.     QueueButtonFrame:SetPoint("CENTER", QueueStatusButton, "CENTER", 0, -0)
  121.     QueueButtonFrame:SetSize(50, 50)
  122.     --QueueButtonFrame:SetBackdrop(BACKDROP_TUTORIAL_16_16)
  123.     --QueueButtonFrame:ClearBackdrop()
  124.  
  125.     QueueStatusButton:SetParent(QueueButtonFrame)
  126.     QueueStatusButton:SetScale (0.65)
  127.     QueueStatusButton:SetMovable(true)
  128.     QueueStatusButton:EnableMouse(true)
  129.     QueueStatusButton:RegisterForDrag("LeftButton")
  130.     QueueStatusButton:SetScript("OnDragStart", function(self, button)
  131.         self:StartMoving()
  132.     end)
  133.     QueueStatusButton:SetScript("OnDragStop", function(self)
  134.         self:StopMovingOrSizing()
  135.     end)
  136.     QueueStatusButton:SetUserPlaced(true)
  137.  
  138. end)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » I am getting my butt kicked by saved variables.

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