Thread Tools Display Modes
07-02-12, 03:04 PM   #1
Othgar
"That" Guy
 
Othgar's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 228
Updating TXP for MoP Beta

I just finally got a chance to login and start working on my experience bar addon today and after fighting with getting DC'd at 90% loaded for a few hours I got this error:
Code:
Message: Interface\AddOns\TXP\TXP.lua:58: Division by zero
Time: 07/02/12 16:28:24
Count: 1
Stack: Interface\AddOns\TXP\TXP.lua:58: in function <Interface\AddOns\TXP\TXP.lua:55>

Locals: self = TribalExperienceBar {
 0 = <userdata>
}
event = "VARIABLES_LOADED"
curxp = 0
levelmax = 0
(*temporary) = <function> defined =[C]:-1
(*temporary) = "%.1f%%"
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = nil
(*temporary) = "Division by zero"
TXPBarText = TXPBarText {
 0 = <userdata>
}
TribalExperienceBar = TribalExperienceBar {
 0 = <userdata>
}
It looks to me like curxp/levelmax is returning 0 and breaking the addon. "Division by Zero" and all that. I can't find any changes in the API anywhere that would do this though (although this is my first update for an expansion I could be missing a million things lol).

This is the relavant section of code:
LUA Code:
  1. TribalExperienceBar:SetScript("OnEvent", function(self, event, ...)
  2.     local curxp = UnitXP("player")
  3.     local levelmax = UnitXPMax("player")
  4.     local perc = string.format("%.1f%%", (100*(curxp/levelmax)))
  5.     local perbox = levelmax / 20
  6.  
  7.     local mystr = string.format("%.1fb - %.1fb tnl | %s",  curxp / perbox, (levelmax - curxp) / perbox, perc)
  8.    
  9.     TXPBarText:SetText(mystr)
  10.     TribalExperienceBar:SetWidth(675)
  11.     TribalExperienceBar:SetHeight(150)
  12.    
  13.     TribalExperienceBar:SetMinMaxValues(0, levelmax)
  14.     TribalExperienceBar:SetValue(curxp)
  15.  
  16.     if TXPVisible then
  17.         TribalExperienceBar:Show()
  18.     else
  19.         TribalExperienceBar:Hide()
  20.     end
  21. end)

And here is the entire code:
LUA Code:
  1. -- Tribal Experience Bar
  2. -- By: Othgar
  3. -- Copyleft All Rights Reversed
  4.  
  5. -- Default state
  6. TXPVisible = true;
  7. TXPLocked = true;
  8.  
  9. -- Constants
  10. local ThousandSeparator = ","
  11. local TipTitle = "Tribal XP"
  12. local _, class = UnitClass("player")
  13. local r,g,b = RAID_CLASS_COLORS[class].r, RAID_CLASS_COLORS[class].g, RAID_CLASS_COLORS[class].b
  14.  
  15.  
  16. local function FmtBigNum(n)
  17.         local left, num, right = string.match(n .. "", '^([^%d]*%d)(%d*)(.-)')
  18.         return left..(num:reverse():gsub('(%d%d%d)', '%1'.. ","):reverse())..right
  19. end
  20.  
  21. local TribalExperienceBar = CreateFrame("StatusBar","TribalExperienceBar",UIParent)
  22. TribalExperienceBar:SetWidth(95);
  23. TribalExperienceBar:SetHeight(50);
  24. TribalExperienceBar:SetPoint("CENTER", UIParent, "CENTER", 0, 0);
  25. TribalExperienceBar:SetFrameStrata("MEDIUM");  
  26. TribalExperienceBar:SetClampedToScreen(1);
  27. TribalExperienceBar:SetBackdrop({bgFile="Interface\\Tooltips\\UI-Tooltip-Background", edgeFile="", tile=1, tileSize=10, edgeSize=10, insets={left=2, right=2, top=2, bottom=2}});
  28. TribalExperienceBar:SetBackdropColor(r,g,b,0)
  29. TribalExperienceBar:SetMovable(1);
  30. TribalExperienceBar:EnableMouse(1);
  31.  
  32. TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\xptexture_rentaire1")
  33. TribalExperienceBar:SetStatusBarColor(r,g,b)
  34. TribalExperienceBar:SetAlpha(.75)
  35.  
  36. local TXPBarText = TribalExperienceBar:CreateFontString("TXPBarText","OVERLAY");
  37. TXPBarText:SetFontObject(GameFontNormal)
  38. TXPBarText:SetTextColor(r,g,b)
  39. TXPBarText:SetPoint("CENTER", TribalExperienceBar, "BOTTOM", 30, 0);
  40. TXPBarText:SetJustifyH("CENTER");
  41. TXPBarText:SetJustifyV("CENTER");
  42.  
  43. local TribalExperienceBar1 = CreateFrame("Frame","TribalExperienceBar1",TribalExperienceBar)
  44. TribalExperienceBar1:SetPoint("TOPLEFT", TribalExperienceBar, "TOPLEFT", -2, 1);
  45. TribalExperienceBar1:SetPoint("BOTTOMRIGHT", TribalExperienceBar, "BOTTOMRIGHT", 2, -1);
  46.  
  47. TribalExperienceBar:RegisterEvent("PLAYER_ENTERING_WORLD");
  48. TribalExperienceBar:RegisterEvent("VARIABLES_LOADED");
  49. TribalExperienceBar:RegisterEvent("PLAYER_XP_UPDATE");
  50. TribalExperienceBar:RegisterEvent("PLAYER_LOGIN");
  51. TribalExperienceBar:RegisterEvent("PLAYER_LEVEL_UP");  
  52.  
  53.  
  54. --Event handling
  55. TribalExperienceBar:SetScript("OnEvent", function(self, event, ...)
  56.     local curxp = UnitXP("player")
  57.     local levelmax = UnitXPMax("player")
  58.     local perc = string.format("%.1f%%", (100*(curxp/levelmax)))
  59.     local perbox = levelmax / 20
  60.  
  61.     local mystr = string.format("%.1fb - %.1fb tnl | %s",  curxp / perbox, (levelmax - curxp) / perbox, perc)
  62.    
  63.     TXPBarText:SetText(mystr)
  64.     TribalExperienceBar:SetWidth(675)
  65.     TribalExperienceBar:SetHeight(150)
  66.    
  67.     TribalExperienceBar:SetMinMaxValues(0, levelmax)
  68.     TribalExperienceBar:SetValue(curxp)
  69.  
  70.     if TXPVisible then
  71.         TribalExperienceBar:Show()
  72.     else
  73.         TribalExperienceBar:Hide()
  74.     end
  75. end)
  76.  
  77. -- move me
  78.  
  79. TribalExperienceBar:SetScript("OnMouseDown", function()
  80.     if not TXPLocked then
  81.         TribalExperienceBar:StartMoving()
  82.     else
  83.         TribalExperienceBar:StopMovingOrSizing()
  84.     end
  85. end)
  86.  
  87. TribalExperienceBar:SetScript("OnMouseUp", function() TribalExperienceBar:StopMovingOrSizing() end)
  88.  
  89.  
  90. -- register slash commands
  91.  
  92. SLASH_TXP1 = '/TXP';
  93. local function slashcmdhandler(param)
  94.     if param == "hide" then
  95.         if TXPVisible then
  96.             TXPVisible=false
  97.             TribalExperienceBar:Hide()
  98.             print("Experience hidden, type /TXP show to show.")
  99.         end
  100.     elseif param=="show" then
  101.         if not TXPVisible then
  102.             TXPVisible=true
  103.             TribalExperienceBar:Show()
  104.             print("Experience displayed, type /TXP hide to hide.")
  105.         end
  106.     elseif param=="rested" then
  107.         if GetXPExhaustion() == nil then
  108.             print("You have no rested EXP.")
  109.         else
  110.             print("You have "..GetXPExhaustion().." rested EXP remaining.")
  111.         end
  112.     elseif param == "lock" then
  113.         if TXPLocked == true then
  114.             TXPLocked = false
  115.             print("TXP UNlocked")
  116.         elseif TXPLocked == false then
  117.             TXPLocked = true
  118.            print("TXP Locked")
  119.         end
  120.  
  121.     else
  122.         print("Valid commands are:");
  123.         print("/TXP show (Show the experience bar.)");
  124.         print("/TXP hide (Hide the experience bar.)");
  125.         print("/TXP rested (Shows the amount of rested XP.)");
  126.         print("/TXP Lock (Locks the experience bar if it is unlocked, Unlocks it if it is locked.")
  127.     end
  128. end
  129. SlashCmdList["TXP"] = slashcmdhandler;
  130.  
  131. --Tooltip
  132.  
  133. local function OnEnter(self)  
  134.     GameTooltip:Show()
  135. end
  136.  
  137. local function OnLeave(self)  
  138.     GameTooltip:Hide()
  139. end
  140.  
  141. --hide @ 85
  142. if UnitLevel("player") >= 85 then
  143.     TribalExperienceBar:Hide() print("Experience bar hidden, type /TXP show to show.")
  144.     TXPVisible = false
  145. end

Any help would be awesom! Thanks guys!

Edit: Just realized I threw this in the wrong section... one of you wonderful admins want to stuff this in the corner where it belongs please.
__________________



Last edited by Othgar : 07-02-12 at 03:07 PM. Reason: Wrong section... derp..
 
07-02-12, 03:24 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
VARIABLES_LOADED is a useless event, and is causing this problem. It fires many times during the loading process to indicate that various Blizzard or game settings are loading, and has nothing to do with addons. At that time, information about your character's experience is not yet available. Just remove that event entirely. You don't need it. You're already catching the "I'm logged in, info is now available" event with PLAYER_LOGIN, PLAYER_ENTERING_WORLD, and PLAYER_XP_UPDATE.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 07-02-12 at 03:27 PM.
 
07-02-12, 04:16 PM   #3
Othgar
"That" Guy
 
Othgar's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 228
Phanx... I love you! Thanks so much, it's working perfectly in the beta for now. A little more testing and I'll upload it to the beta section.
__________________


 
07-03-12, 07:27 AM   #4
Othgar
"That" Guy
 
Othgar's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 228
Last night while messing around in Beta, I decided I wanted to add some additional texture options to my xp bar. I found the ones I wanted, sized them made them work and made them user selected via slash command. Now I'm stuck on how to get them to save per character. I tried it the same way that I did it for locked/unlocked visable/hidden and couldn't get it to work. I'm guessing it's because I'm not defining the variable correctly, but for the life of me I can't figure out a way to define the variable and save it. Hopefully one of you guys will see something that I don't.

Here's my code:
LUA Code:
  1. -- Tribal Experience Bar
  2. -- By: Othgar
  3. -- Copyleft All Rights Reversed
  4.  
  5. -- Default state
  6. TXPVisible = true;
  7. TXPLocked = true;
  8.  
  9.  
  10. -- Constants
  11. local ThousandSeparator = ","
  12. local TipTitle = "Tribal XP"
  13. local _, class = UnitClass("player")
  14. local r,g,b = RAID_CLASS_COLORS[class].r, RAID_CLASS_COLORS[class].g, RAID_CLASS_COLORS[class].b
  15.  
  16.  
  17.  
  18. local function FmtBigNum(n)
  19.         local left, num, right = string.match(n .. "", '^([^%d]*%d)(%d*)(.-)')
  20.         return left..(num:reverse():gsub('(%d%d%d)', '%1'.. ","):reverse())..right
  21. end
  22.  
  23. local TribalExperienceBar = CreateFrame("StatusBar","TribalExperienceBar",UIParent)
  24. TribalExperienceBar:SetSize(675, 150)
  25. TribalExperienceBar:SetPoint("CENTER", UIParent, "CENTER", 0, 0);
  26. TribalExperienceBar:SetFrameStrata("MEDIUM");  
  27. TribalExperienceBar:SetClampedToScreen(1);
  28. TribalExperienceBar:SetBackdrop({bgFile="Interface\\Tooltips\\UI-Tooltip-Background", edgeFile="", tile=1, tileSize=10, edgeSize=10, insets={left=2, right=2, top=2, bottom=2}});
  29. TribalExperienceBar:SetBackdropColor(r,g,b,0)
  30. TribalExperienceBar:SetMovable(1);
  31. TribalExperienceBar:EnableMouse(1);
  32. TribalExperienceBar:SetStatusBarTexture("..")
  33. TribalExperienceBar:SetStatusBarColor(r,g,b)
  34. TribalExperienceBar:SetAlpha(.75)
  35.  
  36.  
  37.  
  38. local TXPBarText = TribalExperienceBar:CreateFontString("TXPBarText","OVERLAY");
  39. TXPBarText:SetFontObject(GameFontNormal)
  40. TXPBarText:SetTextColor(r,g,b)
  41. TXPBarText:SetPoint("CENTER", TribalExperienceBar, "BOTTOM", 0, 0);
  42. TXPBarText:SetJustifyH("CENTER");
  43. TXPBarText:SetJustifyV("CENTER");
  44.  
  45. --Register Events
  46.  
  47. TribalExperienceBar:RegisterEvent("PLAYER_ENTERING_WORLD");
  48. TribalExperienceBar:RegisterEvent("PLAYER_XP_UPDATE");
  49. TribalExperienceBar:RegisterEvent("PLAYER_LOGIN");
  50. TribalExperienceBar:RegisterEvent("PLAYER_LEVEL_UP");  
  51.  
  52.  
  53. --Event handling
  54. TribalExperienceBar:SetScript("OnEvent", function(self, event, ...)
  55.     local curxp = UnitXP("player")
  56.     local levelmax = UnitXPMax("player")
  57.     local perc = string.format("%.1f%%", (100*(curxp/levelmax)))
  58.     local perbox = levelmax / 20
  59.  
  60.     local mystr = string.format("%.1fb - %.1fb tnl | %s",  curxp / perbox, (levelmax - curxp) / perbox, perc)
  61.    
  62.     TXPBarText:SetText(mystr)
  63.  
  64.    
  65.     TribalExperienceBar:SetMinMaxValues(0, levelmax)
  66.     TribalExperienceBar:SetValue(curxp)
  67.  
  68.     if TXPVisible then
  69.         TribalExperienceBar:Show()
  70.     else
  71.         TribalExperienceBar:Hide()
  72.     end
  73. end)
  74.  
  75. -- move me
  76.  
  77. TribalExperienceBar:SetScript("OnMouseDown", function()
  78.     if not TXPLocked then
  79.         TribalExperienceBar:StartMoving()
  80.     else
  81.         TribalExperienceBar:StopMovingOrSizing()
  82.     end
  83. end)
  84.  
  85. TribalExperienceBar:SetScript("OnMouseUp", function() TribalExperienceBar:StopMovingOrSizing() end)
  86.  
  87.  
  88. -- register slash commands
  89.  
  90. SLASH_TXP1 = '/TXP';
  91. local function slashcmdhandler(param)
  92.     if param == "hide" then
  93.         if TXPVisible then
  94.             TXPVisible=false
  95.             TribalExperienceBar:Hide()
  96.             print("Experience hidden, type /TXP show to show.")
  97.         end
  98.     elseif param=="show" then
  99.         if not TXPVisible then
  100.             TXPVisible=true
  101.             TribalExperienceBar:Show()
  102.             print("Experience displayed, type /TXP hide to hide.")
  103.         end
  104.     elseif param == "rested" then
  105.         if GetXPExhaustion() == nil then
  106.             print("You have no rested EXP.")
  107.         else
  108.             print("You have "..GetXPExhaustion().." rested EXP remaining.")
  109.         end
  110.     elseif param == "lock" then
  111.         if TXPLocked == true then
  112.             TXPLocked = false
  113.             print("TXP UNlocked")
  114.         elseif TXPLocked == false then
  115.             TXPLocked = true
  116.            print("TXP Locked")
  117.         end
  118.     elseif param == "curve" then
  119.         if TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve") == true then
  120.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve")
  121.             print("Texture already set to Curve")
  122.         elseif TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve") == false then
  123.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve")
  124.             print("Texture set to Curve")
  125.         end
  126.     elseif param == "simple" then
  127.         if TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple") == true then
  128.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple")
  129.             print("Texture already set to Simple")
  130.         elseif TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple") == false then
  131.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple")
  132.             print("Texture set to Simple")
  133.         end
  134.     elseif param == "split" then
  135.         if TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split") == true then
  136.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split")
  137.             print("Texture already set to Split")
  138.         elseif TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split") == false then
  139.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split")
  140.             print("Texture set to Split")
  141.         end
  142.  
  143.     else
  144.         print("Valid commands are:");
  145.         print("/TXP show (Show the experience bar.)");
  146.         print("/TXP hide (Hide the experience bar.)");
  147.         print("/TXP rested (Shows the amount of rested XP.)");
  148.         print("/TXP Lock (Locks the experience bar if it is unlocked, Unlocks it if it is locked.")
  149.         print("/TXP Curve (Sets the status bar texture to Curve.")
  150.         print("/TXP Simple (Sets the bar texture to Simple")
  151.         print("/TXP Split (Sets the Bar texture to Split")
  152.     end
  153. end
  154. SlashCmdList["TXP"] = slashcmdhandler;
  155.  
  156. --Tooltip
  157.  
  158. local function OnEnter(self)  
  159.     GameTooltip:Show()
  160. end
  161.  
  162. local function OnLeave(self)  
  163.     GameTooltip:Hide()
  164. end
  165.  
  166. --hide @ 85
  167. if UnitLevel("player") >= 85 then
  168.     TribalExperienceBar:Hide() print("Experience bar hidden, type /TXP show to show.")
  169.     TXPVisible = false
  170. end

Here's my TOC:
LUA Code:
  1. ## Interface: 50100
  2. ## Title: Tribal XP Bar
  3. ## Notes: Tribally XP Bar of Prettiness
  4. ## Author: Othgar
  5. ## Version: 1.5a
  6. ## SavedVariablesPerCharacter: TXPVisible, TXPLocked
  7.  
  8.  
  9. TXP.lua

and attached is the full addon just in case.
Attached Files
File Type: zip TXP.zip (362.4 KB, 509 views)
__________________


 
07-03-12, 03:38 PM   #5
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
You need to do a check like this:

lua Code:
  1. MySV = MySV or {}

This uses the pre-existing SVs or creates an empty table if there are none.

You can also improve your slashcmdhandler like this:

lua Code:
  1. local COMMAND_PARAMS = {
  2.     hide = function()
  3.         if TXPVisible then
  4.             TXPVisible=false
  5.             TribalExperienceBar:Hide()
  6.             print("Experience hidden, type /TXP show to show.")
  7.         end
  8.     end,
  9.     show = function()
  10.         if not TXPVisible then
  11.             TXPVisible=true
  12.             TribalExperienceBar:Show()
  13.             print("Experience displayed, type /TXP hide to hide.")
  14.         end
  15.     end,
  16.     rested = function()
  17.         if GetXPExhaustion() == nil then
  18.             print("You have no rested EXP.")
  19.         else
  20.             print("You have "..GetXPExhaustion().." rested EXP remaining.")
  21.         end
  22.     end,
  23.     lock = function()
  24.         if TXPLocked == true then
  25.             TXPLocked = false
  26.             print("TXP UNlocked")
  27.         elseif TXPLocked == false then
  28.             TXPLocked = true
  29.            print("TXP Locked")
  30.         end
  31.     end,
  32.     curve = function()
  33.         if TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve") == true then
  34.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve")
  35.             print("Texture already set to Curve")
  36.         elseif TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve") == false then
  37.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve")
  38.             print("Texture set to Curve")
  39.         end
  40.     end,
  41.     simple = function()
  42.         if TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple") == true then
  43.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple")
  44.             print("Texture already set to Simple")
  45.         elseif TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple") == false then
  46.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple")
  47.             print("Texture set to Simple")
  48.         end
  49.     end,
  50.     split = function()
  51.         if TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split") == true then
  52.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split")
  53.             print("Texture already set to Split")
  54.         elseif TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split") == false then
  55.             TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split")
  56.             print("Texture set to Split")
  57.         end
  58.     end,   
  59. }
  60.  
  61. local function slashcmdhandler(param)
  62.     if COMMAND_PARAMS[param] then
  63.         COMMAND_PARAMS[param]()
  64.         return
  65.     end
  66.     print("Valid commands are:");
  67.     print("/TXP show (Show the experience bar.)");
  68.     print("/TXP hide (Hide the experience bar.)");
  69.     print("/TXP rested (Shows the amount of rested XP.)");
  70.     print("/TXP Lock (Locks the experience bar if it is unlocked, Unlocks it if it is locked.")
  71.     print("/TXP Curve (Sets the status bar texture to Curve.")
  72.     print("/TXP Simple (Sets the bar texture to Simple")
  73.     print("/TXP Split (Sets the Bar texture to Split")
  74. end

This works by doing a simple table lookup and quickly coming back with a result or nil, instead of testing each check in an if-else chain.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
 
07-03-12, 05:06 PM   #6
Othgar
"That" Guy
 
Othgar's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 228
Thanks Torhal. So more or less then I need to define the saved variable like this

Code:
TXPTexture = TXPTexture or {}
but how am I passing the texture that is set via slash command to this so it gets saved? When I was trying it before I had tried

Code:
TXPTexture = TribalExperienceBar:SetStatusBarTexture("..")
and didn't have any luck. I'm not sure why this is getting me so lost. The Locked/Hidden saved variables were pretty easy.
__________________


 
07-03-12, 07:26 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Change your TOC so it actually saves the variable:
Code:
## SavedVariablesPerCharacter: TXPVisible, TXPLocked, TXPTexture
Then change your slash command handlers so they actually look for this variable, actually check the texutre, and actually save the variable:
Code:
    curve = function()
        if TXPTexture == "curve" then
            TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve")
            print("Texture already set to Curve")
        else
            TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve")
            TXPTexture = "curve"
            print("Texture set to Curve")
        end
    end,
statusbar:SetStatusBarTexture(texture) does not return any value, so it will not tell you whether the bar texture was already set to that.

On a side note, if you are performing a boolean check, eg. doing one thing if X == true and another thing if Y == false, the most efficient way to do that is like this:

Code:
if X then
    DoSomething()
else
    DoSomethingElse()
end
and not like this:

Code:
if X == true then
    DoSomething()
elseif X == false then
    DoSomethingElse()
end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
 
07-03-12, 08:05 PM   #8
Othgar
"That" Guy
 
Othgar's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 228
Thanks Phanx. Now i have one more small(ish) problem.

I changed my TOC to:
Code:
## Interface: 40300
## Title: Tribal XP Bar
## Notes: Tribally XP Bar of Prettiness
## Author: Othgar
## Version: 1.0b
## SavedVariablesPerCharacter: TXPVisible, TXPLocked, TXPTexture

TXP.lua
and changed the Lua file to:

Code:
-- Tribal Experience Bar
-- By: Othgar
-- Copyleft All Rights Reversed

-- Default state
TXPVisible = true;
TXPLocked = true;

-- Constants
local ThousandSeparator = ","
local TipTitle = "Tribal XP"
local _, class = UnitClass("player")
local r,g,b = RAID_CLASS_COLORS[class].r, RAID_CLASS_COLORS[class].g, RAID_CLASS_COLORS[class].b



local function FmtBigNum(n)
        local left, num, right = string.match(n .. "", '^([^%d]*%d)(%d*)(.-)')
        return left..(num:reverse():gsub('(%d%d%d)', '%1'.. ","):reverse())..right
end

local TribalExperienceBar = CreateFrame("StatusBar","TribalExperienceBar",UIParent)
TribalExperienceBar:SetSize(675, 150)
TribalExperienceBar:SetPoint("CENTER", UIParent, "CENTER", 0, 0);
TribalExperienceBar:SetFrameStrata("MEDIUM");	
TribalExperienceBar:SetClampedToScreen(1);
TribalExperienceBar:SetBackdrop({bgFile="Interface\\Tooltips\\UI-Tooltip-Background", edgeFile="", tile=1, tileSize=10, edgeSize=10, insets={left=2, right=2, top=2, bottom=2}});
TribalExperienceBar:SetBackdropColor(r,g,b,0)
TribalExperienceBar:SetMovable(1);
TribalExperienceBar:EnableMouse(1);
TribalExperienceBar:SetStatusBarTexture("TXPTexture")
TribalExperienceBar:SetStatusBarColor(r,g,b)
TribalExperienceBar:SetAlpha(.75)



local TXPBarText = TribalExperienceBar:CreateFontString("TXPBarText","OVERLAY");
TXPBarText:SetFontObject(GameFontNormal)
TXPBarText:SetTextColor(r,g,b)
TXPBarText:SetPoint("CENTER", TribalExperienceBar, "BOTTOM", 0, 0);
TXPBarText:SetJustifyH("CENTER");
TXPBarText:SetJustifyV("CENTER");

--Register Events

TribalExperienceBar:RegisterEvent("PLAYER_ENTERING_WORLD");
TribalExperienceBar:RegisterEvent("PLAYER_XP_UPDATE");
TribalExperienceBar:RegisterEvent("PLAYER_LOGIN");
TribalExperienceBar:RegisterEvent("PLAYER_LEVEL_UP");	


--Event handling
TribalExperienceBar:SetScript("OnEvent", function(self, event, ...)
	local curxp = UnitXP("player")
	local levelmax = UnitXPMax("player")
	local perc = string.format("%.1f%%", (100*(curxp/levelmax)))
	local perbox = levelmax / 20

	local mystr = string.format("%.1fb - %.1fb tnl | %s",  curxp / perbox, (levelmax - curxp) / perbox, perc)
	
	TXPBarText:SetText(mystr)

	
	TribalExperienceBar:SetMinMaxValues(0, levelmax)
	TribalExperienceBar:SetValue(curxp)

	if TXPVisible then 
		TribalExperienceBar:Show() 
	else
		TribalExperienceBar:Hide()
	end
end)

-- move me

TribalExperienceBar:SetScript("OnMouseDown", function() 
	if not TXPLocked then 
		TribalExperienceBar:StartMoving() 
	else 
		TribalExperienceBar:StopMovingOrSizing() 
	end 
end)

TribalExperienceBar:SetScript("OnMouseUp", function() TribalExperienceBar:StopMovingOrSizing() end)	


-- register slash commands

SLASH_TXP1 = '/TXP';
local COMMAND_PARAMS = {
	hide = function()
		if TXPVisible then
	    	TXPVisible=false
	     	TribalExperienceBar:Hide()
	     	print("Experience hidden, type /TXP show to show.")
		end
	end,
	show = function()
		if not TXPVisible then
	        TXPVisible=true
	        TribalExperienceBar:Show()
	        print("Experience displayed, type /TXP hide to hide.")
	    end
	end,    
	rested = function()
	    if GetXPExhaustion() == nil then
	       print("You have no rested EXP.")
		else
	       print("You have "..GetXPExhaustion().." rested EXP remaining.")
	    end
	end,
	lock = function()
	    if TXPLocked == true then
	       TXPLocked = false
	       print("TXP UNlocked")
	    elseif TXPLocked == false then
	       TXPLocked = true
	       print("TXP Locked")
	    end 
	end,
	curve = function()
	    if TXPTexture == "curve" then
			TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve")
	       	print("Texture already set to Curve")
	    else
	       	TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\curve")
		   	TXPTexture = "curve"
	       	print("Texture set to Curve")
	    end
	end,
	simple = function()
	    if TXPTexture == "simple" then
			TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple")
	        print("Texture already set to Simple")
	    else
			TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\simple")
			TXPTexture = "simple"
	        print("Texture set to Simple")
	    end
	end,
	split = function()
	    if TXPTexture == "split" then
	       TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split")
	       print("Texture already set to Split")
	    else
		   TribalExperienceBar:SetStatusBarTexture("Interface\\AddOns\\TXP\\media\\split")
		   TXPTexture = "split"
	       print("Texture set to Split")
	    end 
	end,    
}		
local function slashcmdhandler(param)
	if COMMAND_PARAMS[param] then
	   COMMAND_PARAMS[param]()
	   return
	end
	print("Valid commands are:");
	print("/TXP show (Show the experience bar.)");
	print("/TXP hide (Hide the experience bar.)");
	print("/TXP rested (Shows the amount of rested XP.)");
	print("/TXP Lock (Locks the experience bar if it is unlocked, Unlocks it if it is locked.")
	print("/TXP Curve (Sets the status bar texture to Curve.")
	print("/TXP Simple (Sets the bar texture to Simple")
	print("/TXP Split (Sets the Bar texture to Split")
end
SlashCmdList["TXP"] = slashcmdhandler;

--Tooltip

local function OnEnter(self)  
	GameTooltip:Show()
end

local function OnLeave(self)  
	GameTooltip:Hide()
end

--hide @ 85
if UnitLevel("player") >= 85 then
	TribalExperienceBar:Hide() print("Experience bar hidden, type /TXP show to show.")
	TXPVisible = false
end
and it's saving the texture as a variable I think because I set the texture simple (for example) and do /rl, and I get "Texture already set simple." printed in the chat window. However the texture isn't showing up until I set it again with the slash command, and after another /rl I have to set it again.

Edit: After some more testing it seems that the texture remains set and appears after changing zones, but a /rl or logout requires me to reset the texture via slash command.
__________________



Last edited by Othgar : 07-03-12 at 11:15 PM. Reason: update
 
07-03-12, 11:38 PM   #9
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Here - more fixups and streamlining. This separates the textures from the commands, removes duplicated code, and ensures that the param is of the same case (lower) as the table keys.

lua Code:
  1. -- Tribal Experience Bar
  2. -- By: Othgar
  3. -- Copyleft All Rights Reversed
  4.  
  5. -- Default state
  6. -- Only set a value if the variable is nil, else you're overwriting every time.
  7. TXPVisible = TXPVisible or true;
  8. TXPLocked = TXPLocked or true;
  9. TXPTexture = TXPTexture or "simple"
  10.  
  11. -- Constants
  12. local ThousandSeparator = ","
  13. local TipTitle = "Tribal XP"
  14. local _, class = UnitClass("player")
  15. local r,g,b = RAID_CLASS_COLORS[class].r, RAID_CLASS_COLORS[class].g, RAID_CLASS_COLORS[class].b
  16.  
  17.  
  18. local TEXTURES = {
  19.     curve = [[Interface\AddOns\TXP\media\curve]],
  20.     simple = [[Interface\AddOns\TXP\media\simple]],
  21.     split = [[Interface\AddOns\TXP\media\split]],
  22. }
  23.  
  24. local function FmtBigNum(n)
  25.         local left, num, right = string.match(n .. "", '^([^%d]*%d)(%d*)(.-)')
  26.         return left..(num:reverse():gsub('(%d%d%d)', '%1'.. ","):reverse())..right
  27. end
  28.  
  29. local TribalExperienceBar = CreateFrame("StatusBar","TribalExperienceBar",UIParent)
  30. TribalExperienceBar:SetSize(675, 150)
  31. TribalExperienceBar:SetPoint("CENTER", UIParent, "CENTER", 0, 0);
  32. TribalExperienceBar:SetFrameStrata("MEDIUM");  
  33. TribalExperienceBar:SetClampedToScreen(1);
  34. TribalExperienceBar:SetBackdrop({bgFile="Interface\\Tooltips\\UI-Tooltip-Background", edgeFile="", tile=1, tileSize=10, edgeSize=10, insets={left=2, right=2, top=2, bottom=2}});
  35. TribalExperienceBar:SetBackdropColor(r,g,b,0)
  36. TribalExperienceBar:SetMovable(1);
  37. TribalExperienceBar:EnableMouse(1);
  38.  
  39. -- Use the variable, not a string representation of its name
  40. TribalExperienceBar:SetStatusBarTexture(TEXTURES[TXPTexture])
  41. TribalExperienceBar:SetStatusBarColor(r,g,b)
  42. TribalExperienceBar:SetAlpha(.75)
  43.  
  44.  
  45.  
  46. local TXPBarText = TribalExperienceBar:CreateFontString("TXPBarText","OVERLAY");
  47. TXPBarText:SetFontObject(GameFontNormal)
  48. TXPBarText:SetTextColor(r,g,b)
  49. TXPBarText:SetPoint("CENTER", TribalExperienceBar, "BOTTOM", 0, 0);
  50. TXPBarText:SetJustifyH("CENTER");
  51. TXPBarText:SetJustifyV("CENTER");
  52.  
  53. --Register Events
  54.  
  55. TribalExperienceBar:RegisterEvent("PLAYER_ENTERING_WORLD");
  56. TribalExperienceBar:RegisterEvent("PLAYER_XP_UPDATE");
  57. TribalExperienceBar:RegisterEvent("PLAYER_LOGIN");
  58. TribalExperienceBar:RegisterEvent("PLAYER_LEVEL_UP");  
  59.  
  60.  
  61. --Event handling
  62. TribalExperienceBar:SetScript("OnEvent", function(self, event, ...)
  63.     local curxp = UnitXP("player")
  64.     local levelmax = UnitXPMax("player")
  65.     local perc = string.format("%.1f%%", (100*(curxp/levelmax)))
  66.     local perbox = levelmax / 20
  67.  
  68.     local mystr = string.format("%.1fb - %.1fb tnl | %s",  curxp / perbox, (levelmax - curxp) / perbox, perc)
  69.    
  70.     TXPBarText:SetText(mystr)
  71.  
  72.    
  73.     TribalExperienceBar:SetMinMaxValues(0, levelmax)
  74.     TribalExperienceBar:SetValue(curxp)
  75.  
  76.     if TXPVisible then
  77.         TribalExperienceBar:Show()
  78.     else
  79.         TribalExperienceBar:Hide()
  80.     end
  81. end)
  82.  
  83. -- move me
  84.  
  85. TribalExperienceBar:SetScript("OnMouseDown", function()
  86.     if not TXPLocked then
  87.         TribalExperienceBar:StartMoving()
  88.     else
  89.         TribalExperienceBar:StopMovingOrSizing()
  90.     end
  91. end)
  92.  
  93. TribalExperienceBar:SetScript("OnMouseUp", function() TribalExperienceBar:StopMovingOrSizing() end)
  94.  
  95.  
  96. -- register slash commands
  97.  
  98. SLASH_TXP1 = '/TXP';
  99. local COMMAND_PARAMS = {
  100.     hide = function()
  101.         if TXPVisible then
  102.             TXPVisible=false
  103.             TribalExperienceBar:Hide()
  104.             print("Experience hidden, type /TXP show to show.")
  105.         end
  106.     end,
  107.     show = function()
  108.             if not TXPVisible then
  109.             TXPVisible=true
  110.             TribalExperienceBar:Show()
  111.             print("Experience displayed, type /TXP hide to hide.")
  112.         end
  113.     end,    
  114.     rested = function()
  115.         if not GetXPExhaustion() then
  116.            print("You have no rested EXP.")
  117.             else
  118.            print("You have "..GetXPExhaustion().." rested EXP remaining.")
  119.         end
  120.     end,
  121.     lock = function()
  122.         if TXPLocked then
  123.            TXPLocked = false
  124.            print("TXP UNlocked")
  125.         else
  126.            TXPLocked = true
  127.            print("TXP Locked")
  128.         end
  129.     end,
  130. }      
  131.  
  132. local function slashcmdhandler(param)
  133.     param = param:lower()
  134.  
  135.     if TEXTURES[param] then
  136.         if TXPTexture == param then
  137.             print(("Texture already set to %s"):format(param))
  138.             return
  139.         end
  140.         TXPTexture = param
  141.         TribalExperienceBar:SetStatusBarTexture(TEXTURES[param])
  142.         print(("Texture set to %s"):format(param))
  143.         return
  144.     end
  145.  
  146.     if COMMAND_PARAMS[param] then
  147.        COMMAND_PARAMS[param]()
  148.        return
  149.     end
  150.     print("Valid commands are:");
  151.     print("/TXP show (Show the experience bar.)");
  152.     print("/TXP hide (Hide the experience bar.)");
  153.     print("/TXP rested (Shows the amount of rested XP.)");
  154.     print("/TXP Lock (Locks the experience bar if it is unlocked, Unlocks it if it is locked.")
  155.     print("/TXP Curve (Sets the status bar texture to Curve.")
  156.     print("/TXP Simple (Sets the bar texture to Simple")
  157.     print("/TXP Split (Sets the Bar texture to Split")
  158. end
  159. SlashCmdList["TXP"] = slashcmdhandler;
  160.  
  161. --Tooltip
  162.  
  163. local function OnEnter(self)  
  164.     GameTooltip:Show()
  165. end
  166.  
  167. local function OnLeave(self)  
  168.     GameTooltip:Hide()
  169. end
  170.  
  171. --hide @ 85
  172. if UnitLevel("player") >= 85 then
  173.     TribalExperienceBar:Hide() print("Experience bar hidden, type /TXP show to show.")
  174.     TXPVisible = false
  175. end
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.

Last edited by Torhal : 07-03-12 at 11:41 PM.
 
07-04-12, 02:06 AM   #10
Othgar
"That" Guy
 
Othgar's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 228
Sweet thanks Torhal! Working perfectly now on live, and I'll test it on beta tomorrow.
__________________


 
 

WoWInterface » Site Forums » Archived Beta Forums » MoP Beta archived threads » Updating TXP for MoP Beta

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