Thread Tools Display Modes
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
11-28-14, 04:59 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
The saved variable is a global. If you define a local with the same name, it won't access it.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
11-28-14, 09:33 PM   #3
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
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.
  Reply With Quote
11-29-14, 02:33 AM   #4
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
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/
__________________
The cataclysm broke the world ... and the pandas could not fix it!

Last edited by Rilgamon : 11-29-14 at 02:37 AM.
  Reply With Quote
11-29-14, 06:38 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Rilgamon View Post
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.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
11-30-14, 02:22 AM   #6
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
And in repsonse to the event you'd use a construct like
You missed the line before the code
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
12-02-14, 03:49 AM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Wonder how I skipped over that.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » What is wrong with this code?

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