Thanks Phanx, you're the best as always.
I moved the code and the
spec variable into the OnEvent handler. I had no idea function calls were slow, I should probably re-do some of my other addons...
Forgive my ignorance, but is the reason I don't need to check for events at all because I need to reload anyway for my castbar to change, even if the variable value changes when PLAYER_SPECIALIZATION_CHANGED fires? I'm fairly sure it won't be dynamic in it's current incarnation. If I'm correct, that means I can just make a function and call it whenever the code loads because I'll need a reload anyway. Would this be more efficient or not, given what I've learned about function calls being slow?
EDIT: Ofcourse it won't be dynamic. Zz
Also, would I not run into troubles with GetSpecialization() not being available until PLAYER_ENTERING_WORLD? How would I go about finding this out?
I read in an old thread from 2011 in which you replied to that GetPrimaryTalentTree() isn't available until after PLAYER_ALIVE fires. Perhaps I should just check for that event? Assuming GetSpecialization() follows the same rules it's predecessor did.
This is the state my code is in right now, I'm going to try out some of the stuff in this post and see where it gets me. The castbar still disappears on reload with this code.
Lua Code:
local isCaster
local cbCheckFrame = CreateFrame("Frame")
cbCheckFrame:RegisterEvent('PLAYER_SPECIALIZATION_CHANGED')
cbCheckFrame:RegisterEvent('PLAYER_ENTERING_WORLD')
cbCheckFrame:RegisterEvent('PLAYER_LOGIN')
cbCheckFrame:SetScript("OnEvent", function()
local spec = GetSpecialization()
if playerClass == 'DRUID' or playerClass == 'MONK' or playerClass == 'PALADIN' or playerClass == 'SHAMAN' then
if playerClass == 'DRUID' then
if spec == 1 or spec == 4 then
isCaster = true
else
isCaster = false
end
elseif playerClass == 'MONK' then
if spec == 2 then
isCaster = true
else
isCaster = false
end
elseif playerClass == 'PALADIN' then
if spec == 1 then
isCaster = true
else
isCaster = false
end
elseif playerClass == 'SHAMAN' then
if spec == 2 then
isCaster = false
else
isCaster = true
end
end
elseif playerClass == 'DEATHKNIGHT' or playerClass == 'HUNTER' or playerClass == 'ROGUE' or playerClass == 'WARRIOR' then
isCaster = false
elseif playerClass == 'MAGE' or playerClass == 'PRIEST' or playerClass == 'WARLOCK' then
isCaster = true
end
print(isCaster)
end)
Finally, I've been doing "PLAYER" for years and saw you mention something similar in a thread yesterday. Can't believe it's something I've never thought about twice, even though I've looked up UnitClass, UnitName, UnitRace and so forth about a million times. Bad habits die hard I suppose.
EDIT#2: I took events out of it altogether, and just did a function that I call once when the code fires. It now works correctly, but maybe it isn't the best way of doing it? Is this different than running a SetScript on a frame? If not, I have no idea what handler to use as OnEvent seems wasteful. Maybe OnLoad?
Lua Code:
local isCaster
local cbCheck = function()
local spec = GetSpecialization()
if playerClass == 'DRUID' or playerClass == 'MONK' or playerClass == 'PALADIN' or playerClass == 'SHAMAN' then
if playerClass == 'DRUID' then
if spec == 1 or spec == 4 then
isCaster = true
else
isCaster = false
end
elseif playerClass == 'MONK' then
if spec == 2 then
isCaster = true
else
isCaster = false
end
elseif playerClass == 'PALADIN' then
if spec == 1 then
isCaster = true
else
isCaster = false
end
elseif playerClass == 'SHAMAN' then
if spec == 2 then
isCaster = false
else
isCaster = true
end
end
elseif playerClass == 'DEATHKNIGHT' or playerClass == 'HUNTER' or playerClass == 'ROGUE' or playerClass == 'WARRIOR' then
isCaster = false
elseif playerClass == 'MAGE' or playerClass == 'PRIEST' or playerClass == 'WARLOCK' then
isCaster = true
end
print(isCaster)
end
cbCheck()