Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ElvUF.Tags.Events['health:custom'] = 'UNIT_HEALTH_FREQUENT UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED'
- -- This tells ElvUI what events are important for our format, so it does update it when needed
- -- TBH I just copied this line from another tag :)
- -- If you don't know what a function is try to remember your Maths lessons.
- -- A function is a thing where you put things in, then the function does something with it and may return other things
- -- a bit like in calculus where you had f(x) = ... but this time there may be multiple arguments and they aren't always numbers
- -- Here we register our formatting function, it takes as input the unit which health should be formatted
- ElvUF.Tags.Methods['health:custom'] = function(unit)
- local status = UnitIsDead(unit) and L["Dead"] or UnitIsGhost(unit) and L["Ghost"] or not UnitIsConnected(unit) and L["Offline"]
- -- This line checks different statuses the unit can have, namely being dead, a ghost or offline
- -- and stores the result in the (local ) variable 'status'
- -- the statements L[".."] are for localization
- -- Read this line as 'when the unit is dead store the localized string for "dead" in the variable "status"
- -- or else when the unit is a ghost store ....'
- -- next we check whether we stored something in the variable and if so we just return that
- -- this makes it so, that when the unit is dead it will display "Dead" and so on
- -- This whole construct is one of the very basic ones!
- -- We can check some condition and execute instructions based on it. Note that only one of the branches "if" or "else" is executed
- if (status) then
- return status
- else
- local hp = UnitHealth(unit)
- -- retrieve the unit's health
- -- now we do another conditional branching to find the correct way to format the number
- -- 1e9 is scientific notation for a 1 with 9 '0's
- -- f.e. 2.5e4 = 25000
- -- I could have written the numbers in full, but this is more readable imo
- if hp >= 1e9 then
- return string.format("%dM", math.floor(0.5 + hp/1e6))
- -- string.format is a function that takes a string describing a format (%d is f.e. a integer) and
- -- fills in the other arguments. The arithmethic is basically cutting the last places and rounding correctly.
- -- math.floor takes a rational number and 'cuts off' the decimals so f.e. math.floor(1.337) -> 1
- -- so in order to round correctly I add 0.5
- elseif hp >= 1e6 then
- return string.format("%dK", math.floor(0.5 + hp/1e3))
- -- see comment above
- else
- return tostring(hp)
- --Just convert the number to string
- end --Here we need to close the blocks openend. This one is from the if..elseif..else
- end -- if..else
- end -- function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement