View Single Post
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.