First I would avoid using somthing simple like “PetBarFrame”, “PetExpBar_OnLoad” as the name of a frame or global variable or global function as these are shared across all addons and the Blizzard UI code.
Something like “CBPB_PetBarFrame”, “CBPB_PetBarFrame_OnLoad” would make it more unique to your addon.
Names of local variables, function s etc. don’t matter so much.
Because you’re not creating the frame in XML, you don’t need to use globals so long as everything is defined the the right order.
You have a call to PetExpUpdate() which I’m guessing could also be made local so long as it is defined before PetExpBar_OnLoad ie. above PetExpBar_OnLoad in the same file.
local PetBarFrame
local isHunter
local function PetExpBar_OnLoad(self)
local x, y=UnitClass("player")
if x ~= "Hunter" then -- Only hunters allowed
isHunter = true
--if CBPetBars is empty, fill it with default values
if CBPetBars == nil then CBPetBars = {
height = 14,
width = 300,
xPos = 35,
yPos = -5,
}
end
-- Load the new default or previous saved frame variables
height = CBPetBars.height
width = CBPetBars.width
xPos = CBPetBars.xPos
yPos = CBPetBars.yPos
PetBarFrame:SetPoint("LEFT",PetActionBarFrame,"RIGHT",xPos,yPos)
PetBarFrame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 10, edgeSize = 11,insets = { left = 1, right = 1, top = 1, bottom = 1 }})
PetBarFrame:SetBackdropColor(.15,.15,.15,.50)
PetBarFrame:SetBackdropBorderColor(.75,.75,.75,.5)
PetBarFrame:SetFrameLevel(1)
PetBarFrame:SetHeight(height)
PetBarFrame:SetWidth(width)
PetBarFrame:EnableMouse(true)
PetBarFrame:SetMovable(true)
PetBarFrame:SetResizable(true)
PetBarFrame:SetMinResize(100,12)
PetBarFrame:SetMaxResize(1000,50)
PetBarFrame:SetScript("OnMouseDown",function() PetBarTryToMove() PetExpUpdate() end)
PetBarFrame:SetScript("OnMouseUp",function() PetBarStopMoving() end)
width = PetBarFrame:GetWidth()
height = PetBarFrame:GetHeight()
PetBarFrameSectionOne = CreateFrame("Frame",nil,PetBarFrame)
PetBarFrameSectionOne:SetFrameLevel(1)
PetBarFrameSectionOne:SetHeight(height)
PetBarFrameSectionOne:SetWidth(width/6+1)
PetBarFrameSectionOne:SetPoint("LEFT",PetBarFrame,"LEFT",1,0)
PetBarFrameSectionOne:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 10, edgeSize = 10,insets = { left = 1, right = 1, top = 1, bottom = 1 }})
PetBarFrameSectionOne:SetBackdropColor(.4,.4,.4,0)
PetBarFrameSectionOne:SetBackdropBorderColor(.75,.75,.75)
PetBarFrameSectionTwo = CreateFrame("Frame",nil,PetBarFrame)
PetBarFrameSectionTwo:SetFrameLevel(1)
PetBarFrameSectionTwo:SetHeight(height)
PetBarFrameSectionTwo:SetWidth(width/6+2)
PetBarFrameSectionTwo:SetPoint("LEFT",PetBarFrameSectionOne,"RIGHT",-2,0)
PetBarFrameSectionTwo:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 10, edgeSize = 10,insets = { left = 1, right = 1, top = 1, bottom = 1 }})
PetBarFrameSectionTwo:SetBackdropColor(.4,.4,.4,0)
PetBarFrameSectionTwo:SetBackdropBorderColor(.75,.75,.75)
PetBarFrameSectionThree = CreateFrame("Frame",nil,PetBarFrame)
PetBarFrameSectionThree:SetFrameLevel(1)
PetBarFrameSectionThree:SetHeight(height)
PetBarFrameSectionThree:SetWidth(width/6+2)
PetBarFrameSectionThree:SetPoint("LEFT",PetBarFrameSectionTwo,"RIGHT",-2,0)
PetBarFrameSectionThree:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 10, edgeSize = 10,insets = { left = 1, right = 1, top = 1, bottom = 1 }})
PetBarFrameSectionThree:SetBackdropColor(.4,.4,.4,0)
PetBarFrameSectionThree:SetBackdropBorderColor(.75,.75,.75)
PetBarFrameSectionFour = CreateFrame("Frame",nil,PetBarFrame)
PetBarFrameSectionFour:SetFrameLevel(1)
PetBarFrameSectionFour:SetHeight(height)
PetBarFrameSectionFour:SetWidth(width/6+2)
PetBarFrameSectionFour:SetPoint("LEFT",PetBarFrameSectionThree,"RIGHT",-2,0)
PetBarFrameSectionFour:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 10, edgeSize = 10,insets = { left = 1, right = 1, top = 1, bottom = 1 }})
PetBarFrameSectionFour:SetBackdropColor(.4,.4,.4,0)
PetBarFrameSectionFour:SetBackdropBorderColor(.75,.75,.75)
PetBarFrameSectionFive = CreateFrame("Frame",nil,PetBarFrame)
PetBarFrameSectionFive:SetFrameLevel(1)
PetBarFrameSectionFive:SetHeight(height)
PetBarFrameSectionFive:SetWidth(width/6+2)
PetBarFrameSectionFive:SetPoint("LEFT",PetBarFrameSectionFour,"RIGHT",-2,0)
PetBarFrameSectionFive:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 10, edgeSize = 10,insets = { left = 1, right = 1, top = 1, bottom = 1 }})
PetBarFrameSectionFive:SetBackdropColor(.4,.4,.4,0)
PetBarFrameSectionFive:SetBackdropBorderColor(.75,.75,.75)
PetBarFrameSectionSix = CreateFrame("Frame",nil,PetBarFrame)
PetBarFrameSectionSix:SetFrameLevel(1)
PetBarFrameSectionSix:SetHeight(height)
PetBarFrameSectionSix:SetWidth(width/6+0)
PetBarFrameSectionSix:SetPoint("LEFT",PetBarFrameSectionFive,"RIGHT",-2,0)
PetBarFrameSectionSix:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 10, edgeSize = 10,insets = { left = 1, right = 1, top = 1, bottom = 1 }})
PetBarFrameSectionSix:SetBackdropColor(.9,.9,.9,0)
PetBarFrameSectionSix:SetBackdropBorderColor(.75,.75,.75)
--SetTextSize()
PetExpText = PetBarFrame:CreateFontString(nil,"HIGHLIGHT","NumberFont_Outline_Med")
PetExpText:SetPoint("CENTER",PetBarFrame,"CENTER",-8,2)
PetExpBar = CreateFrame("Frame",nil,PetBarFrame)
PetExpBar:SetFrameLevel(1)
PetExpBar:SetPoint("LEFT",PetBarFrame,"LEFT",3,0)
PetExpBar:SetHeight(10)
PetExpBar:RegisterEvent("UNIT_PET_EXPERIENCE")
PetExpBar:SetScript("OnEvent",function() PetExpUpdate() end)
local tx = PetExpBar:CreateTexture(nil,"BACKGROUND")
tx:SetColorTexture(.9,.3,.9)
tx:SetGradientAlpha("VERTICAL",.2,0,.2,1, .9,.0,.9,1)
tx:SetAllPoints(PetExpBar)
PetBarFrame.texture = tx
PetExpUpdate()
else
DisableAddOn("__C_B_PetBars")
end
PetBarFrame:Hide()
PetFrame:SetScript("OnShow",function() PetExpUpdate() PetBarFrame:Show() end)
PetFrame:SetScript("OnHide",function() PetBarFrame:Hide() end)
end
PetBarFrame = CreateFrame("Frame",nil,UIParent)
PetBarFrame:RegisterEvent("PLAYER_LOGIN")
PetBarFrame:RegisterEvent("PLAYER_LOGOUT")
PetBarFrame:SetScript("OnEvent", function(self,event,...)
if event == "PLAYER_LOGIN" then
PetExpBar_OnLoad(self)
return
end
--PLAYER_LOGOUT is the only other event registered so no check required.
-- Player is logging out, save frame size and position
if isHunter then -- Assuming only hunters allowed
local point, relativeTo, relativePoint, xOfs, yOfs = PetBarFrame:GetPoint()
CBPetBars.height = height
CBPetBars.width = width
CBPetBars.xPos = PetBarFrame.xOfs
CBPetBars.yPos = PetBarFrame.yOfs
end
end)