View Single Post
05-19-13, 05:56 PM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Code:
f:SetScript("OnEvent", function(self, event, unit)
	if event == "UNIT_HEALTH" then
		local currentHealth, maxHealth = UnitHealth(unit), UnitHealthMax(unit)
		local percentHealth = currentHealth / maxHealth * 100
		self.HealthText:SetFormattedText("%d/%d (%d%%)", currentHealth, maxHealth, percentHealth)
	elseif event == "UNIT_POWER" then
		local currentPower, maxPower = UnitPower(unit), UnitPowerMax(unit)
		local percentPower = currentPower / maxPower * 100
		self.PowerText:SetFormattedText("%d/%d (%d%%)", currentPower, maxPower, percentPower)
	end
end)
f:RegisterUnitEvent("UNIT_HEALTH", f.unit)
f:RegisterUnitEvent("UNIT_POWER", f.unit)
You should only update the portions relevant to the event that fired, and you shouldn't need to manually update on load unless your code is loading very late. If you notice wrong values on login, just add this at the end:

Code:
f:GetScript("OnEvent")(f, "UNIT_HEALTH", f.unit)
f:GetScript("OnEvent")(f, "UNIT_POWER", f.unit)
__________________
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