View Single Post
11-28-14, 04:03 PM   #1
Zireko
An Aku'mai Servant
Join Date: Nov 2014
Posts: 38
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)
  Reply With Quote