View Single Post
01-03-12, 02:42 AM   #10
Jonisaurus
An Aku'mai Servant
 
Jonisaurus's Avatar
Join Date: Aug 2011
Posts: 35
Originally Posted by Phanx View Post
That's pretty inefficient, too, with numerous unnecessary extra functions and checks. I also have no idea why you're ignoring all events that occur in combat. I'd say the vast majority of totem drops/deaths/recalls occur in combat. It looks like the OP is trying to write a totem timer, which does not involve any secure frames.

Lua Code:
  1. -- First, check if the player is a shaman.
  2. -- If not, then we don't need to do anything else.
  3. local _, class = UnitClass("player")
  4. if class ~= "SHAMAN" then return end
  5.  
  6. local frame = CreateFrame("Frame")
  7.  
  8. frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  9. -- Unless you actually care whether the player is in combat or not, you
  10. -- do not need to listen for these two events:
  11. frame:RegisterEvent("PLAYER_REGEN_DISABLED")
  12. frame:RegisterEvent("PLAYER_REGEN_ENABLED")
  13. -- Unless you actually care how much mana the player has, you should
  14. -- probably listen for PLAYER_TOTEM_UPDATE instead of UNIT_POWER.
  15. frame:RegisterEvent("UNIT_POWER")
  16.  
  17. frame:SetScript("OnEvent", function(self, event, unit, resource)
  18.     -- Of the events you are watching, only UNIT_POWER passes any
  19.     -- arguments (or fires for units other than the player), so make
  20.     -- sure we're not responding to other units' power update events.
  21.     if event == "UNIT_POWER" and (unit ~= "player" or resource ~= "MANA") then return end
  22.  
  23.     -- Check if the player has ANY totem active.
  24.     local hasTotem
  25.     for i = 1, 4 do
  26.         local _, name = GetTotemInfo(i)
  27.         if name and name:len() > 0 then
  28.             hasTotem = true
  29.             break
  30.         end
  31.     end
  32.  
  33.     -- Do things here depending on whether or not a totem is active.
  34.     if hasTotem then
  35.         self:Show() -- Inside this scope, "self" refers to the frame.
  36.     else
  37.         self:Hide()
  38.     end
  39. end)
Thank you.
Of course I will listen to PLAYER_TOTEM_UPDATE and not to UNIT_POWER, that was just a substitute because I wasn't aware of that event's existence. That would also eliminate the need for querying the power type (and also the unitID?)

I do want to listen to the two "entering combat events".

I'm not at my home WoW installation so I can't work on the addon right now. I will update this post later.
  Reply With Quote