Advertisement
Guest User

Custom Tag Explanation

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