View Single Post
08-25-14, 01:55 AM   #5
Wimpface
A Molten Giant
 
Wimpface's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 648
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:
  1. local isCaster
  2. local cbCheckFrame = CreateFrame("Frame")
  3. cbCheckFrame:RegisterEvent('PLAYER_SPECIALIZATION_CHANGED')
  4. cbCheckFrame:RegisterEvent('PLAYER_ENTERING_WORLD')
  5. cbCheckFrame:RegisterEvent('PLAYER_LOGIN')
  6. cbCheckFrame:SetScript("OnEvent", function()
  7.     local spec = GetSpecialization()
  8.  
  9.     if playerClass == 'DRUID' or playerClass == 'MONK' or playerClass == 'PALADIN' or playerClass == 'SHAMAN' then
  10.         if playerClass == 'DRUID' then
  11.             if spec == 1 or spec == 4 then
  12.                 isCaster = true
  13.             else
  14.                 isCaster = false
  15.             end
  16.         elseif playerClass == 'MONK' then
  17.             if spec == 2 then
  18.                 isCaster = true
  19.             else
  20.                 isCaster = false
  21.             end
  22.         elseif playerClass == 'PALADIN' then
  23.             if spec == 1 then
  24.                 isCaster = true
  25.             else
  26.                 isCaster = false
  27.             end
  28.         elseif playerClass == 'SHAMAN' then
  29.             if spec == 2 then
  30.                 isCaster = false
  31.             else
  32.                 isCaster = true
  33.             end
  34.         end
  35.     elseif playerClass == 'DEATHKNIGHT' or playerClass == 'HUNTER' or playerClass == 'ROGUE' or playerClass == 'WARRIOR' then
  36.         isCaster = false
  37.     elseif playerClass == 'MAGE' or playerClass == 'PRIEST' or playerClass == 'WARLOCK' then
  38.         isCaster = true
  39.     end
  40.  
  41.     print(isCaster)
  42. 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:
  1. local isCaster
  2. local cbCheck = function()
  3.     local spec = GetSpecialization()
  4.  
  5.     if playerClass == 'DRUID' or playerClass == 'MONK' or playerClass == 'PALADIN' or playerClass == 'SHAMAN' then
  6.         if playerClass == 'DRUID' then
  7.             if spec == 1 or spec == 4 then
  8.                 isCaster = true
  9.             else
  10.                 isCaster = false
  11.             end
  12.         elseif playerClass == 'MONK' then
  13.             if spec == 2 then
  14.                 isCaster = true
  15.             else
  16.                 isCaster = false
  17.             end
  18.         elseif playerClass == 'PALADIN' then
  19.             if spec == 1 then
  20.                 isCaster = true
  21.             else
  22.                 isCaster = false
  23.             end
  24.         elseif playerClass == 'SHAMAN' then
  25.             if spec == 2 then
  26.                 isCaster = false
  27.             else
  28.                 isCaster = true
  29.             end
  30.         end
  31.     elseif playerClass == 'DEATHKNIGHT' or playerClass == 'HUNTER' or playerClass == 'ROGUE' or playerClass == 'WARRIOR' then
  32.         isCaster = false
  33.     elseif playerClass == 'MAGE' or playerClass == 'PRIEST' or playerClass == 'WARLOCK' then
  34.         isCaster = true
  35.     end
  36.  
  37.     print(isCaster)
  38. end
  39.  
  40. cbCheck()
__________________
All I see is strobe lights blinding me in my hindsight.

Last edited by Wimpface : 08-25-14 at 02:11 AM.
  Reply With Quote