WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   What is wrong with this code? (https://www.wowinterface.com/forums/showthread.php?t=50604)

Zireko 11-28-14 04:03 PM

What is wrong with this code?
 
I'm keeping this as simple as I can. This code is just a little counter that uses a right mouse button click to open a window that has a counter in it. The code works it just doesn't save the count. So my question is what is wrong with this code?

Lua Code:
  1. -- First I will start with my saved variable table.
  2.  
  3. local ZCountSave = {
  4.     ["Count"] = {
  5.         ["ClickedCount"] = 0,
  6.     }
  7. }
  8.  
  9. -- Next I will create my button to pull up the counter.
  10.  
  11. ZCountButton = CreateFrame("Button","ZCountButton",UIParent,"UIPanelButtonTemplate")
  12.     ZCountButton:SetPoint("CENTER",0,0)
  13.     ZCountButton:SetWidth(30)
  14.     ZCountButton:SetHeight(30)
  15.     ZCountButton:SetText("ZC")
  16.     ZCountButton:SetMovable(true)
  17.     ZCountButton:RegisterForDrag("LeftButton")
  18.     ZCountButton:SetScript("OnDragStart",ZCountButton.StartMoving)
  19.     ZCountButton:SetScript("OnDragStop",ZCountButton.StopMovingOrSizing)
  20.  
  21. -- Now I'm going to create my frame that will have my counter inside of it.
  22.  
  23. ZCountBoxFrame = CreateFrame("Frame")
  24.     ZCountBoxFrame:SetPoint("CENTER", 0, 0)
  25.     ZCountBoxFrame:SetBackdrop(StaticPopup1:GetBackdrop())
  26.     ZCountBoxFrame:SetHeight(300)
  27.     ZCountBoxFrame:SetWidth(300)
  28.     ZCountBoxFrame:Hide(true)
  29.     ZCountBoxFrame:SetMovable(true)
  30.     ZCountBoxFrame:EnableMouse(true)
  31.     ZCountBoxFrame:RegisterForDrag("LeftButton")
  32.     ZCountBoxFrame:SetScript("OnDragStart",ZCountBoxFrame.StartMoving)
  33.     ZCountBoxFrame:SetScript("OnDragStop",ZCountBoxFrame.StopMovingOrSizing)
  34.  
  35.  
  36. -- Next we need to create a function that can open our ZCountBoxFrame.
  37.  
  38. ZCountButton:RegisterForClicks("AnyUp")
  39. ZCountButton:SetScript("OnClick", function(self, button, ...)
  40.     if (button == "RightButton") then
  41.         if ZCountBoxFrame:IsShown() then
  42.             ZCountBoxFrame:Hide()
  43.         else
  44.             ZCountBoxFrame:Show()
  45.         end
  46.     end
  47. end)
  48.  
  49. -- Now let's make the counter which will be inside our ZCountBoxFrame
  50.  
  51. ZCounterButton = CreateFrame("Button","ZCounterButton",ZCountBoxFrame,"UIPanelButtonTemplate")
  52.     ZCounterButton:SetPoint("CENTER",0,0)
  53.     ZCounterButton:SetWidth(80)
  54.     ZCounterButton:SetHeight(30)
  55.     ZCounterButton:SetText("Count: ")
  56.    
  57. -- Now that we have our Counter Button we need to create a function that will count a number up, down, and back to zero.
  58.    
  59. ZCounterButton:RegisterForClicks("AnyUp")
  60. ZCounterButton:SetScript("OnClick", function(self, button, ...)
  61.     if (button == "LeftButton") then
  62.         ZCountSave.Count.ClickedCount = ZCountSave.Count.ClickedCount + 1
  63.         ZCounterButton:SetText("Count: "..ZCountSave.Count.ClickedCount)
  64.     end
  65. end)
  66.  
  67. -- Now to put in the Addon Loaded so it can save some things for us.
  68.  
  69. local ZLoFrame=CreateFrame("Frame")
  70. ZLoFrame:RegisterEvent("ADDON_LOADED")
  71. ZLoFrame:SetScript("OnEvent",function(self,event,...)
  72.     if event=="ADDON_LOADED" and (...)==ZCount then
  73.        
  74.         if ZCountSave.Count.ClickedCount ~= 0 then
  75.             ZCounterButton:SetText("Completed: "..ZCountSave.Count.ClickedCount)
  76.         end
  77.        
  78.         self:UnregisterEvent("ADDON_LOADED")
  79.     end
  80. end)

SDPhantom 11-28-14 04:59 PM

The saved variable is a global. If you define a local with the same name, it won't access it.

myrroddin 11-28-14 09:33 PM

Put the word local in front of lines 11 and 23, and remove the word local from line 3, if that is your saved variable.

Rilgamon 11-29-14 02:33 AM

Lua Code:
  1. local ZCountSave = {
  2.     ["Count"] = {
  3.         ["ClickedCount"] = 0,
  4.     }
  5. }

Everytime your addon loads this table would be used if it were global and not local. And when your saved variable is loaded which is nil by default it would delete it.
When your main code runs your sv is not yet available. So you want to delay its use after an event that makes sure it is loaded. Typical is ADDON_LOADED (not sure if your implementation works ... as I dont see ZCount defined).

And in repsonse to the event you'd use a construct like

Lua Code:
  1. local default = {
  2.     ["Count"] = {
  3.         ["ClickedCount"] = 0,
  4.     }
  5. ZCountSave = ZCountSave or default

This would make sure your sv is used when available.

Edit:

A quick read should bring more light to this topic
http://wowpedia.org/AddOn_loading_process
http://wowpedia.org/Saving_variables..._game_sessions

A good choice to make sure your data is stored
http://www.wowace.com/addons/ace3/pages/api/ace-db-3-0/

SDPhantom 11-29-14 06:38 PM

Quote:

Originally Posted by Rilgamon (Post 301300)
Lua Code:
  1. local default = {
  2.     ["Count"] = {
  3.         ["ClickedCount"] = 0,
  4.     }
  5. ZCountSave = ZCountSave or default

In the last line, it's unnecessary to check ZCountSave and fall through to default. At that point in the Lua code, ZCountSave will always be nil.

Rilgamon 11-30-14 02:22 AM

Quote:

And in repsonse to the event you'd use a construct like
You missed the line before the code ;)

SDPhantom 12-02-14 03:49 AM

Wonder how I skipped over that. :rolleyes:


All times are GMT -6. The time now is 10:46 AM.

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