Reply
 
Thread Tools Display Modes
Unread 05-21-13, 10:29 AM   #1
Rusken
A Theradrim Guardian
AddOn Compiler - Click to view compilations
Join Date: Dec 2009
Posts: 61
Lua code help (stuf text)

Evening, i got this custom lua code from a fella over at mmo a couple of months back and what it does is that it shortens name like "Raider's Training Dummy" in to "R.T.Dummy".
What i need help with is to change the code so it still does what it does but i want it to look like this ->

"R.T.Dummy | 100", 100 being perchp and i want it to be colored by "hpthreshold", the name and the "|" should be white.

So, is this possible and is there any kind soul here with the knowledge on how to do this that could give me a hand?
Thanks in advance.
Rusken is offline   Reply With Quote
Unread 05-21-13, 04:01 PM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,026
It would be easier if you posted a copy of the working code as-is. Be sure to encase it in [code] tags.
__________________
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
SDPhantom is offline   Reply With Quote
Unread 05-22-13, 06:32 AM   #3
ravagernl
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,147
Originally Posted by SDPhantom View Post
It would be easier if you posted a copy of the working code as-is. Be sure to encase it in [code] tags.
I'm thinking it's something like
Code:
function(unit)
	local name = UnitName(unit) or unit
	name = name:gsub('(%S+) ',function(t) return t:sub(1,1)..'.' end)
	return name
end
ravagernl is offline   Reply With Quote
Unread 05-22-13, 10:26 AM   #4
Rusken
A Theradrim Guardian
AddOn Compiler - Click to view compilations
Join Date: Dec 2009
Posts: 61
Oh snap, forgot to do that, its the one ravagernl posted.

Code:
function(unit)
	local name = UnitName(unit) or unit
	name = name:gsub('(%S+) ',function(t) return t:sub(1,1)..'.' end)
	return name
end
Rusken is offline   Reply With Quote
Unread 05-22-13, 12:44 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,026
This has an improvement over the unit's name conversion as it doesn't allocate a dynamic function to handle the string.gsub() call and it's kept on C side. The way I have the coloring work is it starts as green at 100%, moves to yellow at 50% and red at 0%. The way text colors work is they use UI escape sequences. The one for colors is in the format |cAARRGGBB in which AA, RR, GG, and BB represent hex values for Alpha, Red, Green, and Blue respectively. Although it should be customary to use |r as an end tag for colored text, it's not necessary if you're immediately changing to another color for following text. The purpose of |r is to reset the color of following text to what is set by the object that is given the string to display. The use of || is to print a literal pipe character "|" as this is used for the UI escape sequences.
Code:
function(unit)
	local short=(UnitName(unit) or unit):gsub("(%S)%S*%s+","%1%.");
	local pcnt=math.min(1,UnitHealth(unit)/UnitHealthMax(unit));-- If max is zero (it happens) assume 100%
	local r,g=math.max(0,2-pcnt*2)*255,math.min(1,pcnt*2)*255;

	return ("|cffffffff%s || |cff%02x%02x00%.0f|r"):format(short,r,g,pcnt*100));
end

This is to get dynamic text colors to work in a string-only standpoint, but it'll be much better to leave the static colors in the string and control the dynamic color from a FontString object. The following code makes use of this.
Code:
function(obj,unit)
	local short=(UnitName(unit) or unit):gsub("(%S)%S*%s+","%1%.");
	local pcnt=math.min(1,UnitHealth(unit)/UnitHealthMax(unit));-- If max is zero (it happens) assume 100%
	obj:SetTextColor(math.max(0,2-pcnt*2),math.min(1,pcnt*2),0);
	obj:SetFormattedText("|cffffffff%s |||r %.0f",short,pcnt*100));
end

UI Escapes (WoWPedia)
__________________
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
SDPhantom is offline   Reply With Quote
Unread 05-23-13, 05:52 PM   #6
Rusken
A Theradrim Guardian
AddOn Compiler - Click to view compilations
Join Date: Dec 2009
Posts: 61
Originally Posted by SDPhantom View Post
This has an improvement over the unit's name conversion as it doesn't allocate a dynamic function to handle the string.gsub() call and it's kept on C side. The way I have the coloring work is it starts as green at 100%, moves to yellow at 50% and red at 0%. The way text colors work is they use UI escape sequences. The one for colors is in the format |cAARRGGBB in which AA, RR, GG, and BB represent hex values for Alpha, Red, Green, and Blue respectively. Although it should be customary to use |r as an end tag for colored text, it's not necessary if you're immediately changing to another color for following text. The purpose of |r is to reset the color of following text to what is set by the object that is given the string to display. The use of || is to print a literal pipe character "|" as this is used for the UI escape sequences.
Code:
function(unit)
	local short=(UnitName(unit) or unit):gsub("(%S)%S*%s+","%1%.");
	local pcnt=math.min(1,UnitHealth(unit)/UnitHealthMax(unit));-- If max is zero (it happens) assume 100%
	local r,g=math.max(0,2-pcnt*2)*255,math.min(1,pcnt*2)*255;

	return ("|cffffffff%s || |cff%02x%02x00%.0f|r"):format(short,r,g,pcnt*100));
end

This is to get dynamic text colors to work in a string-only standpoint, but it'll be much better to leave the static colors in the string and control the dynamic color from a FontString object. The following code makes use of this.
Code:
function(obj,unit)
	local short=(UnitName(unit) or unit):gsub("(%S)%S*%s+","%1%.");
	local pcnt=math.min(1,UnitHealth(unit)/UnitHealthMax(unit));-- If max is zero (it happens) assume 100%
	obj:SetTextColor(math.max(0,2-pcnt*2),math.min(1,pcnt*2),0);
	obj:SetFormattedText("|cffffffff%s |||r %.0f",short,pcnt*100));
end

UI Escapes (WoWPedia)
Thanks but none of them seem to work when i paste them in to "use custom lua" in stuf, is it something i need to add to the actual .lua?
As you might have noticed, my lua skills are worse then worse
Rusken is offline   Reply With Quote
Reply

Go BackWoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Lua code help (stuf text)

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