View Single Post
02-28-14, 10:38 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
That's an awful lot of unnecessary event checking.

You don't need to listen for PLAYER_DEAD and PLAYER_ALIVE at all, as UNIT_HEALTH will fire for the player unit at those times anyway. You don't need UNIT_ENTERING_VEHICLE and UNIT_EXITING_VEHICLE, as you only care when those transitions end, not when they begin.

Lua Code:
  1. ht:SetScript("OnEvent", function(self, event, unit)
  2.     if UnitIsGhost("player") then
  3.         return htt:SetText("|cffb40000Dead")
  4.     elseif UnitIsDead("player") then
  5.         return htt:SetText("|cffb40000Ghost")
  6.     end
  7.  
  8.     local hcur, hmax = UnitHealth(unit or "player"), UnitHealthMax(unit or "player")
  9.  
  10.     if hcur >= 1000000 then
  11.         hcur = format("%.1fm", hcur / 1000000)
  12.     elseif hcur >= 1000 then
  13.         hcur = format("%.0fk", hcur / 1000)
  14.     end
  15.  
  16.     if hcur == hmax then
  17.         htt:SetFormattedText("|cff00b400%s", hcur)
  18.     else
  19.         htt:SetFormattedText("|cffbf0000%s", hcur)
  20.     end
  21. end)

I'm fairly sure, but not 100% sure, that UNIT_HEALTH will fire anyway when you enter or leave a vehicle, so you don't actually need to register for those events. However, if that's not the case, just register ENTERED and EXITED to tell you when those transitions end, and adjust the event handler as follows:

Code:

+	if event == "UNIT_ENTERED_VEHICLE" then
+		unit = "vehicle"
+	end

	local hcur, hmax = UnitHealth(unit or "player"), UnitHealthMax(unit or "player")
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote