Thread Tools Display Modes
10-03-15, 01:16 PM   #1
hikokun
A Murloc Raider
 
hikokun's Avatar
Join Date: Oct 2015
Posts: 4
Stuf LUA help

I'm looking for a custom lua code to show curhp/maxhp followed by perchp with two decimals for Stuf Unit Frames. The current code I'm using which only shows perchp with two decimals -

Code:
function(unit, cache, textframe)
  if UnitExists(unit) then
    local perchp = 100 * UnitHealth(unit)/UnitHealthMax(unit)
    return "%.2f%%",perchp
  else
    local perchp = 100 * UnitHealth("player")/UnitHealthMax("player")
    return "%.2f%%",perchp
  end
end
I'd like a code for both of the following setups if possible

300000/500000 | 60.00%
60.00% | 300000/500000


Thanks for any help!
  Reply With Quote
10-03-15, 06:15 PM   #2
Nikita S. Doroshenko
A Cyclonian
 
Nikita S. Doroshenko's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 45
Originally Posted by hikokun View Post
I'm looking for a custom lua code to show curhp/maxhp followed by perchp with two decimals for Stuf Unit Frames. The current code I'm using which only shows perchp with two decimals -

Code:
function(unit, cache, textframe)
  if UnitExists(unit) then
    local perchp = 100 * UnitHealth(unit)/UnitHealthMax(unit)
    return "%.2f%%",perchp
  else
    local perchp = 100 * UnitHealth("player")/UnitHealthMax("player")
    return "%.2f%%",perchp
  end
end
I'd like a code for both of the following setups if possible

300000/500000 | 60.00%
60.00% | 300000/500000


Thanks for any help!
Could you please paste a code that calls this function with this parameters (unit, cache, textframe).
Because we need to know what previous function do with our return.
Or you can show us whole .lua or .xml file, it will be more easy.

Solution will looks something like this:
Lua Code:
  1. local stamp = UnitHealth("player").." / "..UnitHealthMax("player").." | "..perchp
  2. return stamp
But then we need to change function that get our return value.
  Reply With Quote
10-03-15, 06:24 PM   #3
Nikita S. Doroshenko
A Cyclonian
 
Nikita S. Doroshenko's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 45
Originally Posted by hikokun View Post
I'm looking for a custom lua code to show curhp/maxhp followed by perchp with two decimals for Stuf Unit Frames. The current code I'm using which only shows perchp with two decimals -

Code:
function(unit, cache, textframe)
  if UnitExists(unit) then
    local perchp = 100 * UnitHealth(unit)/UnitHealthMax(unit)
    return "%.2f%%",perchp
  else
    local perchp = 100 * UnitHealth("player")/UnitHealthMax("player")
    return "%.2f%%",perchp
  end
end
I'd like a code for both of the following setups if possible

300000/500000 | 60.00%
60.00% | 300000/500000


Thanks for any help!
I think i fount a solution, try to replace your function with this one:

Lua Code:
  1. function(unit, cache, textframe)
  2.     if UnitExists(unit) then
  3.         local curhp = UnitHealth(unit)
  4.         local maxhp = UnitHealthMax(unit)
  5.         local perchp = 100 * curhp/maxhp
  6.         local stamp = curhp.." / "..maxhp.." | ".."%.2f%%"
  7.         return stamp,perchp
  8.     else
  9.         local curhp = UnitHealth("player")
  10.         local maxhp = UnitHealthMax("player")
  11.         local perchp = 100 * curhp/maxhp
  12.         local stamp = curhp.." / "..maxhp.." | ".."%.2f%%"
  13.         return stamp,perchp
  14.     end
  15. end

Or cleaner:

Lua Code:
  1. function(unit, cache, textframe)
  2.     local curhp, maxhp
  3.     if UnitExists(unit) then
  4.         curhp = UnitHealth(unit)
  5.         maxhp = UnitHealthMax(unit)
  6.     else
  7.         curhp = UnitHealth("player")
  8.         maxhp = UnitHealthMax("player")
  9.     end
  10.     local perchp = 100 * curhp/maxhp
  11.     local stamp = curhp.." / "..maxhp.." | ".."%.2f%%"
  12.     return stamp,perchp
  13. end

Last edited by Nikita S. Doroshenko : 10-03-15 at 06:28 PM.
  Reply With Quote
10-03-15, 07:15 PM   #4
hikokun
A Murloc Raider
 
hikokun's Avatar
Join Date: Oct 2015
Posts: 4
Originally Posted by Nikita S. Doroshenko View Post
Lua Code:
  1. function(unit, cache, textframe)
  2.     local curhp, maxhp
  3.     if UnitExists(unit) then
  4.         curhp = UnitHealth(unit)
  5.         maxhp = UnitHealthMax(unit)
  6.     else
  7.         curhp = UnitHealth("player")
  8.         maxhp = UnitHealthMax("player")
  9.     end
  10.     local perchp = 100 * curhp/maxhp
  11.     local stamp = curhp.." / "..maxhp.." | ".."%.2f%%"
  12.     return stamp,perchp
  13. end
This one works perfect, thank you! I attempted (with no knowledge of lua) to also make it into the format of

60.00% | 300000/500000

but the text would disappear each time. How would I go about switching it around?

Edit:
Lua Code:
  1. function(unit, cache, textframe)
  2.         local curhp, maxhp
  3.         if UnitExists(unit) then
  4.             curhp = UnitHealth(unit)
  5.             maxhp = UnitHealthMax(unit)
  6.         else
  7.             curhp = UnitHealth("player")
  8.             maxhp = UnitHealthMax("player")
  9.         end
  10.         local perchp = 100 * curhp/maxhp
  11.         local stamp = "%.2f%%".." | "..curhp.." / "..maxhp
  12.         return stamp,perchp
  13.     end

Figured it out after messing around a bit! However, I've now noticed that the numbers are not being shortened (I have my STUF options to shorten starting at 1 million).

Instead of showing 1M, its showing 1000000. Any way to fix this?

Ideally I'd like it to show like this -

60.00% | 3.0M / 5.0M
5.00% | 250000 / 5.0M

Last edited by hikokun : 10-03-15 at 07:43 PM.
  Reply With Quote
10-03-15, 08:34 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
The simplest way would be to just call the Blizzard function for short number display:
Code:
        -- After this line:
        local perchp = 100 * curhp/maxhp
        -- add these two lines:
        curhp = AbbreviateLargeNumbers(curhp)
        maxhp = AbbreviateLargeNumbers(maxhp)
__________________
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
11-06-17, 02:59 PM   #6
hikokun
A Murloc Raider
 
hikokun's Avatar
Join Date: Oct 2015
Posts: 4
Long ago I had figured out a solution but I've now lost it and I'm looking for it again --

The above solution only shortens it as so -

60.00% | 3000K / 5000K

I would prefer it to start shortening at 1000000 as so -

60.00% | 3.0M / 5.0M
5.00% | 250000 / 5.0M

Thanks for any help!

Edit:
Lua Code:
  1. function(unit, cache, textframe)
  2.             local curhp, maxhp
  3.             if UnitExists(unit) then
  4.                 curhp = UnitHealth(unit)
  5.                 maxhp = UnitHealthMax(unit)
  6.             else
  7.                 curhp = UnitHealth("player")
  8.                 maxhp = UnitHealthMax("player")
  9.             end
  10.             local perchp = 100 * curhp/maxhp
  11.             local stamp = "%.2f%%".." | "..curhp.." / "..maxhp
  12.             return stamp,perchp
  13.         end

Current code I'm using.

Last edited by hikokun : 11-06-17 at 03:09 PM.
  Reply With Quote
11-06-17, 05:09 PM   #7
Ammako
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 256
This is something I do to format numbers and group into thousands/millions.

lua Code:
  1. local function formatPlayerNumbers(amount)
  2.     local formatted, k = amount
  3.  
  4.     if amount >= 100000 and amount < 100000000 then
  5.         formatted = string.sub(formatted, 0, (string.len(formatted) - 3)) .. " K"
  6.     elseif amount >= 100000000 then
  7.         formatted = string.sub(formatted, 0, (string.len(formatted) - 6)) .. " M"
  8.     else
  9.         while true do  
  10.             formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  11.             if (k==0) then
  12.                 break
  13.             end
  14.         end
  15.     end
  16.  
  17.     return formatted
  18. end

Of course, you'll have to tweak the amounts at lines 4 and 6 to get it to shorten 5,000,000 as 5M rather than 5,000K, but you get the point.

Last edited by Ammako : 11-07-17 at 01:46 AM.
  Reply With Quote
11-06-17, 05:31 PM   #8
Kakjens
A Cliff Giant
Join Date: Apr 2017
Posts: 75
Could you explain what happens in lines 9-14 because for something that should have a simple display the code seems too complex.
  Reply With Quote
11-06-17, 06:01 PM   #9
Ammako
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 256
Couldn't tell you honestly. When I needed a function like that I googled it and this is what I got as a result.
It works so I didn't bother messing with it any further, but if you find that this part of the code isn't actually needed it can be removed, of course.
  Reply With Quote
11-06-17, 06:02 PM   #10
hikokun
A Murloc Raider
 
hikokun's Avatar
Join Date: Oct 2015
Posts: 4
Originally Posted by Ammako View Post
This is something I do to format numbers and group into thousands/millions.

lua Code:
  1. local function formatPlayerNumbers(amount)
  2.     local formatted, k = amount
  3.  
  4.     if amount >= 100000 and amount < 100000000 then
  5.         formatted = string.sub(formatted, 0, (string.len(formatted) - 3)) .. " K"
  6.     elseif amount >= 100000000 then
  7.         formatted = string.sub(formatted, 0, (string.len(formatted) - 6)) .. " M"
  8.     else
  9.         while true do  
  10.             formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  11.             if (k==0) then
  12.                 break
  13.             end
  14.         end
  15.     end
  16.  
  17.     return formatted
  18. end

Of you're, you'll have to tweak the amounts at lines 4 and 6 to get it to shorten 5,000,000 as 5M rather than 5,000K, but you get the point.
I appreciate the code, but how do I incorporate it into my previous code? I've tried a few different ways and all it does is causes the text to disappear.
  Reply With Quote
11-06-17, 06:10 PM   #11
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
gsub returns 1, the new string and 2, the number of matches. When the number of matches == 0 (in this case k) break out of the loop.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
11-07-17, 01:27 AM   #12
Kakjens
A Cliff Giant
Join Date: Apr 2017
Posts: 75
Originally Posted by Ammako View Post
This is something I do to format numbers and group into thousands/millions.

lua Code:
  1. local function formatPlayerNumbers(amount)
  2.     local formatted, k = amount
  3.  
  4.     if amount >= 100000 and amount < 100000000 then
  5.         formatted = string.sub(formatted, 0, (string.len(formatted) - 3)) .. " K"
  6.     elseif amount >= 100000000 then
  7.         formatted = string.sub(formatted, 0, (string.len(formatted) - 6)) .. " M"
  8.     else
  9.         while true do  
  10.             formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  11.             if (k==0) then
  12.                 break
  13.             end
  14.         end
  15.     end
  16.  
  17.     return formatted
  18. end

Of you're, you'll have to tweak the amounts at lines 4 and 6 to get it to shorten 5,000,000 as 5M rather than 5,000K, but you get the point.
hikokun, while debugging I suggest in line 16 of this code to add print(formatted). And in your code, in line 11 to replace curhp with formatPlayerNumbers(curhp), and maxhp with formatPlayerNumbers(maxhp).

Fizzlemizz, thanks for explanation but I meant to ask what the code accomplishes instead of what it does.

But won't this become unneeded after 7.3.5 with ilvl (and stat) squishes?
  Reply With Quote
11-07-17, 01:53 AM   #13
Ammako
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 256
Holy crap, I just realized. "Of you're." What kind of typo is that?

Anyway patterns are too much for my brain to comprehend tbh, but the end result is that it takes the number and adds comma separator (so 10000 becomes 10,000.)
If you feel like you can understand it you can probably read up on documentation to figure out exactly what the pattern matches.

Maybe it won't be as useful anymore once they do the next numbers squish, but that's hardly a reason not to make addons for the current game. ;p
  Reply With Quote
11-07-17, 03:09 AM   #14
Kakjens
A Cliff Giant
Join Date: Apr 2017
Posts: 75
Ammako, human language is relatively fault-tolerant (unlike programming ones), so was able to guess what you intended to write.
Thanks for explaining the result of this loop with gsub and link. No experience with pattern matching but I believe it does something like: "If, starting from beginning, there's at least one digit (1-st group) followed by three digits (2-nd group), then between 1-st and second group insert comma". Sounds somehow similar to BreakupLageNumbers.
Seeing that the applicable numbers for this procedure are between 999 and 100000, I wonder if it would be more efficient to use extra "if" instead of "while" loop.
Edit. About writing the code which won't be used after this patch: I would be really hesitant to do it. I would think about whether having the exact desired output would be worth the trouble or be content with, for example, AbbreviateLargeNumbers.

Last edited by Kakjens : 11-07-17 at 03:49 AM. Reason: BreakupLageNumbers, some strange philosophy
  Reply With Quote
11-07-17, 10:11 AM   #15
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Originally Posted by Kakjens View Post
But won't this become unneeded after 7.3.5 with ilvl (and stat) squishes?
It will be needed, because in future patches, the numbers will go up again and most likely get the these sizes before something happens...again.

Using the while statement is more of a maintenance thing so you don't have to keep adding "if" statements as the numbers get larger while requiring less iterations when the numbers a smaller.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-07-17 at 10:13 AM.
  Reply With Quote
11-07-17, 03:13 PM   #16
Ammako
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 256
Ok so I took the time to understand the pattern, I might as well explain how it works.
  • ^ only matches starting from the beginning of the string.
  • -? the string will match whether it has a - character at the front or not (meaning -100000 would be broken up as -100,000 just the same as 100000 would be broken up as 100,000.)
  • %d+ matches one or more digits.
  • ^(-?%d+) matches infinitely from the beginning of the string, whether it has a leading - character or not, if there is at least one digit, until it hits a non-digit character.
  • (%d%d%d) matches three digits in a row.
  • ^(-?%d+)(%d%d%d) matches a string, whether it has a leading - character or not, with at least four leading digits (with the last three being sorted into their own separate group.)

So when you feed it a number, it'll start looking from the beginning of the string for something that may or may not begin with a - character and has at least four leading digits, stopping whenever it hits anything else (in this case, a comma character.)
After it matches, it adds a comma between the two parts.

^(-?%d+)(%d%d%d) : 10000000 --> (10000),(000) --> (10),(000),000 --> 10,000,000 (no more matches.)
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Stuf LUA help

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off