Your current method:
Code:
local spec = GetSpecialization()
local _, playerClass = UnitClass("PLAYER")
local isCaster
local function cbCheck()
-- stuff here
end
local cbCheckFrame = CreateFrame("Frame")
cbCheckFrame:RegisterEvent('PLAYER_SPECIALIZATION_CHANGED')
cbCheckFrame:RegisterEvent('PLAYER_ENTERING_WORLD')
cbCheckFrame:RegisterEvent('PLAYER_LOGIN')
cbCheckFrame:SetScript("OnEvent", function()
cbCheck()
end)
... may as well not listen for events at all, since you only check the player's spec once in the main chunk. Your
cbCheck function will run the same logic every time it's run, even if the player's spec changes during play or isn't available yet in the main chunk (eg. on a fresh login GetSpecialization() is probably always nil).
You need to check the spec [b]inside[/i] the function. Also, defining a function outside of the OnEvent handler offers no benefit, and has the non-insignifcant downside of adding an extra function call, which is pretty much the slowest thing you can do in Lua. Just move all that code directly into the OnEvent handler or, if you really want to have a variable pointing to your function, set the
cbCheck function as the OnEvent handler directly.
Code:
local isCaster
local _, playerClass = UnitClass("player")
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()
-- stuff here
end)
(On a side note, the proper capitalization for unit tokens is lowercase, so it should be "player", not "PLAYER". The API will auto-correct values you pass into it, but you should be in the habit of using the correct capitalization, not only because it's, well, correct, but also because if you're ever performing comparisons on values
returned by the API, you'll have to write your comparison correctly, or waste time calling
string.upper on the returned value.)