View Single Post
12-29-11, 09:09 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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)
  Reply With Quote