If you're using UNIT_HEALTH as suggested by Trimble_Epic (which is correct, by the way Trimble), the exact line is ...
local var = floor(UnitHealth(arg1) / UnitHealthMax(arg1) * 100)
Otherwise you just specify the unit directly to the functions as stated by everyone else. It's actually possible too to get a 'one decimal point' variant. This is code I bastardized from a tip given by industrial ...
local var = UnitHealth(arg1) / UnitHealthMax(arg1) * 100
var = format("%.1f%", var)
That way, if the health it returns is 36.991251059125...blah, then you'll just get '36.9%' as a return from the var variable.
My approach to this would be to have the unit object request UNIT_HEALTH messages from my event distributor when the arg1 is the unit object's self.PartyID. The unit object would maintain it's own knowledge of it's current health based on the events it receives.
Then, when I want to ask it for it's health percent, I would call math.floor(unit:HealthPercent()) which would probably be implemented something like this:
function unit:HealthPercent()
return self.health / self.maxHealth * 100
end
One thing to consider with trying to find a unit's health percent is that if the unit is NOT in your party or raid, then UnitHealth() already returns the health percent, not the absolute health. However, since UnitHealthMax() always returns 100 when the unit is NOT in your party or raid, then the math still works out ok ;)
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
To show up in game without the decimals? For example, right now it'll say 96.01342254418%. I only want it to show 96%.
Think you don't need "math." actually.. (just floor(numbers) has worked for me anywats) :wink:
*looks up to the ceiling... down to the floor...*
;)
Sayo
Randor
but, I'm not sure, since I can no longer see the wiki from work.
Otherwise you just specify the unit directly to the functions as stated by everyone else. It's actually possible too to get a 'one decimal point' variant. This is code I bastardized from a tip given by industrial ...
That way, if the health it returns is 36.991251059125...blah, then you'll just get '36.9%' as a return from the var variable.
My approach to this would be to have the unit object request UNIT_HEALTH messages from my event distributor when the arg1 is the unit object's self.PartyID. The unit object would maintain it's own knowledge of it's current health based on the events it receives.
Then, when I want to ask it for it's health percent, I would call math.floor(unit:HealthPercent()) which would probably be implemented something like this:
function unit:HealthPercent()
return self.health / self.maxHealth * 100
end
One thing to consider with trying to find a unit's health percent is that if the unit is NOT in your party or raid, then UnitHealth() already returns the health percent, not the absolute health. However, since UnitHealthMax() always returns 100 when the unit is NOT in your party or raid, then the math still works out ok ;)