This site makes extensive use of JavaScript.
Please enable JavaScript in your browser.
Live
PTR
10.2.7
PTR
10.2.6
Beta
LUA help
Post Reply
Return to board index
Post by
278110
This post was from a user who has deleted their account.
Post by
Farmbuyer
That's not really a Lua thing; that's specific to STUF. You might attract more knowledgeable responses if you change your subject line. :-)
My offhand guess is that you want something like
(UnitXP("player") / UnitMaxXP(*"player") / 100) .. '%'
but I don't know how smart STUF is at converting your return values to a formatted string.
Post by
GTMistral
You will most likely get hundreds of decimal using this line. If you can format it, that'd be better.
function(unit, cache, textframe) return string.format("%.3f",(UnitXP("player")/UnitXPMax("player"))*100).."%%" end
This will output something like "24.537%". Tested.
Lua concatenation operator is: ..
The equivalent in C-based language is "+".
Sample:
local str1 = "Hello";
local str2 = "World";
print(str1); -- Hello
print(str2); -- World
print(str1..str2); -- HelloWorld
print(str1.." "..str2.."!"); -- Hello World!
Post by
HighFive
Wait... There is no concatenation operator in C, is there?
Post by
GTMistral
Well, technically speaking it's an overload of the + operator that can accept any string when called from another string. Therefore, ...
string s1 = "Hello ";
string s2 = "World!";
string s3 = s1 + s2;
std::cout << s3; // Output: "Hello World!"
But there is no overload that can accept anything other than a string or a char. It doesn't compile if you try with an
int
or
float
number for example. So in this case, it's roughly a concatenation operator.
Post by
Nipah
To steal from GTMistral:
function(unit, cache, textframe) return string.format("%.3f",(UnitXP("player")/UnitXPMax("player"))*100).."%%" end
Change the .3 to .0 for no trailing decimals (or to some other # for how ever many trailing decimal spots you want).
And in case someone ever comes in here for Stuf XP showing goodness, I'll post the bit of code I use for my XP in Stuf:
function(unit, cache, textframe)
local level, exp, maxxp, restd, end1, end2, end3 = UnitLevel("player"), UnitXP("player"), UnitXPMax("player"), GetXPExhaustion(), "", "", ""
if exp >= 1000 then
if exp >= 1000000 then
exp = exp / 1000000
end1 = "m"
else
exp = exp / 1000
end1 = "k"
end end
if maxxp >= 1000 then
if maxxp >= 1000000 then
maxxp = maxxp / 1000000
end2 = "m"
else
maxxp = maxxp / 1000
end2 = "k"
end end
if restd == nil then
restd = 0
elseif restd >= 1000 then
if restd >= 1000000 then
restd = restd / 1000000
end3 = "m"
else
restd = restd / 1000
end3 = "k"
end end
if level == 85 then
return ""
else
return string.format("%.1f%s/%.0f%s |cff6666ff%.1f%s|r",exp, end1, maxxp, end2, restd, end3)
end end
Its a bit clunky, but it does what it's supposed to. Anyhow, it shows the following style: "123.4k/234m 123.4k", with the last # being a nice blue for rested XP.
Post by
GTMistral
For performance sake, you should put the "if level == 85 then" block before all others. In
ANY
case, you evaluate everything, even if you are 85. If you are 85, you could evaluate this expression only and return string.empty right away instead of going through the whole process.
function(unit, cache, textframe)
local level, exp, maxxp, restd, end1, end2, end3 = UnitLevel("player"), UnitXP("player"), UnitXPMax("player"), GetXPExhaustion(), "", "", ""
if level == 85 then
return ""
else
if exp >= 1000 then
if exp >= 1000000 then
exp = exp / 1000000
end1 = "m"
else
exp = exp / 1000
end1 = "k"
end
if maxxp >= 1000 then
if maxxp >= 1000000 then
maxxp = maxxp / 1000000
end2 = "m"
else
maxxp = maxxp / 1000
end2 = "k"
end
end
if restd == nil then
restd = 0
elseif restd >= 1000 then
if restd >= 1000000 then
restd = restd / 1000000
end3 = "m"
else
restd = restd / 1000
end3 = "k"
end
end
end
end
return string.format("%.1f%s/%.0f%s |cff6666ff%.1f%s|r",exp, end1, maxxp, end2, restd, end3)
end
In any case, it's a neat snippet. :)
Post by
HighFive
Well, technically speaking it's an overload of the + operator that can accept any string when called from another string. Therefore, ...
string s1 = "Hello ";
string s2 = "World!";
string s3 = s1 + s2;
std::cout << s3; // Output: "Hello World!"
But there is no overload that can accept anything other than a string or a char. It doesn't compile if you try with an
int
or
float
number for example. So in this case, it's roughly a concatenation operator.Oh, you meant C++/#. Okay.
Post by
278110
This post was from a user who has deleted their account.
Post by
Nipah
No worries... Any credit for
LUA I post
is generally going to go to either TheRealArkayn, GTMistral, or HighFive.
The guys on the UI & Macro forum are top notch.
@GTMistral: I'm actually surprised that I didn't notice that. I'll have to remedy that when I log back in today.
Post Reply
You are not logged in. Please
log in
to post a reply or
register
if you don't already have an account.