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)