1. #1

    Need help with some simple lua :)

    Hello

    Yea might seem simple for some, but mammoth for me . This is what I need: a couple of lua texts for my unit frames.

    The first:
    "Level" "name"... where the level should only be there for sub-maxlevels, colour white. The name should be classcoloured for PCs and reaction for NPCs

    The second:
    "Max HP" : "percent health"... The "Max HP" should be truncated (220k). The ": percent health" should only be shown when not at full health. All of the second should be white please

    Thanks in advance, and sorry for being an idiot at lua, and not even being able to supply a sample code


    Kunstliche

  2. #2
    1)

    Code:
    local GetLevelText(unit)
    	if ( UnitIsWildBattlePet(unit) or UnitIsBattlePetCompanion(unit) ) then
    		return '|cffffffff'..UnitBattlePetLevel(unit)..'|r'
    	elseif level == UnitLevel('player') then
    		return '|cffffffff'..level..'|r'
    	elseif(level > 0) then
    		return '|cffffffff'..level.."|r"
    	else
    		return '|cffffffff??|r'
    	end
    end
    Code:
    local __colors = {
    	[1] = { 0.78, 0.25, 0.25, 1 }, 
    	[2] = { 0.78, 0.25, 0.25, 1 }, 
    	[3] = { 0.78, 0.25, 0.25, 1 }, 
    	[4] = { 218/255, 197/255, 92/255 }, 
    	[5] = { 75/255,  175/255, 76/255 }, 
    	[6] = { 75/255,  175/255, 76/255 }, 
    	[7] = { 75/255,  175/255, 76/255 },
    	[8] = { 75/255,  175/255, 76/255 },
    }
    local function GetNameColor(unit)
    local unitReaction = UnitReaction(unit, 'player')
    	local _, unitClass = UnitClass(unit)
    	if (UnitIsPlayer(unit)) then
    		local class = RAID_CLASS_COLORS[unitClass]
    		if class then return '|c'..RAID_CLASS_COLORS[unitClass].colorStr end
    		return '|cFFFFFFFF'
    	elseif (unitReaction) then
    		local reaction = __colors[unitReaction]
    		return format("|cff%02x%02x%02x", reaction[1]*255, reaction[2]*255, reaction[3]*255)
    	else
    		return '|cFFC2C2C2'
    	end
    end
    Code:
    local text = format('%s %s %s|r', GetLevelText(unit), GetNameColor(unit), ( UnitName(unit) ))

    2)
    Code:
    local function ShortValue(v)
    	if v >= 1e9 then
    		return ("%.0fb"):format(v / 1e9)
    	elseif v >= 1e6 then
    		return ("%.0fm"):format(v / 1e6)
    	elseif v >= 1e3 or v <= -1e3 then
    		return ("%.0fk"):format(v / 1e3)
    	else
    		return v
    	end
    end
    Code:
    local function GetHPPerc(unit)
    local hp = UnitExists(unit) and ( UnitHealth(unit) / UnitHealthMax(unit) ) * 100 or 0		
    	if hp >= 100 then
    		return ''
    	else
    		return format("%.1f%s", hp, "%%")
    	end
    end
    Code:
    local text = format("%s %s", ShortValue(UnitHealthMax(unit)),  GetHPPerc(unit))
    Last edited by Aleaa; 2015-03-24 at 10:04 PM.

  3. #3
    You sir are awesome

    Sad to say it though. Maybe I did something wrong I don't know. But it did not work . Maybe i should have added, no idea if it would change anything, that this is for Stuf unit frames and they are to be put into two text "fields" (or whatever it's called), which is where i put it initially, though i also tried one by one.

    When i put your codes into it it just dissapears ... I'm at a loss


    Kunstliche

    - - - Updated - - -

    Figured I would post a few pics of what I had in mind

    Max level, taken no dmg: https://imageshack.com/i/hlafHec1p

    Max level, taken dmg: https://imageshack.com/i/eypKF7Ftp

    Not max level taken no dmg: https://imageshack.com/i/f0nSETSxp

    Not maxlevel, taken dmg: https://imageshack.com/i/ipMgGHA3p

    This was what i had in mind. I hope it's possible to get Stuf to behave like this in the two text "fields".


    Kunstliche

  4. #4
    Code:
    function(unit, cache, textframe) 
    	
    	local text = '|cffffffff'
    	local name = UnitName(unit) or UNKNOWN
    	
    	local level = UnitLevel(unit)
    	
    	if ( UnitIsWildBattlePet(unit) or UnitIsBattlePetCompanion(unit) ) then
    		text = text..UnitBattlePetLevel(unit)..'|r'
    	elseif level == UnitLevel('player') then
    		text =  text..''..'|r'
    	elseif(level > 0) then
    		text =  text..level.."|r"
    	else
    		text =  text..'??|r'
    	end
    
    	GLOBAL_STUF_CUSTOM_REACT_COLORS = GLOBAL_STUF_CUSTOM_REACT_COLORS or {
    		[1] = { 0.78, 0.25, 0.25, 1 }, 
    		[2] = { 0.78, 0.25, 0.25, 1 }, 
    		[3] = { 0.78, 0.25, 0.25, 1 }, 
    		[4] = { 218/255, 197/255, 92/255 }, 
    		[5] = { 75/255,  175/255, 76/255 }, 
    		[6] = { 75/255,  175/255, 76/255 }, 
    		[7] = { 75/255,  175/255, 76/255 },
    		[8] = { 75/255,  175/255, 76/255 },
    	}
    	
    	local __colors = GLOBAL_STUF_CUSTOM_REACT_COLORS
    	
    	local unitReaction = UnitReaction(unit, 'player')
    	local _, unitClass = UnitClass(unit)
    	if (UnitIsPlayer(unit)) then
    		local class = RAID_CLASS_COLORS[unitClass]
    		if class then 
    			text = text..'|c'..RAID_CLASS_COLORS[unitClass].colorStr 
    		else
    			text = text..'|cFFFFFFFF'
    		end
    	elseif (unitReaction) then
    		local reaction = __colors[unitReaction]
    		text = text..format("|cff%02x%02x%02x", reaction[1]*255, reaction[2]*255, reaction[3]*255)
    	else
    		text = text..'|cFFC2C2C2'
    	end
    	
    
    	return text..format('%.15s', name)..'|r'
    end
    Code:
    function(unit, cache, textframe) 
    	
    	local text = '|cffffffff'
    	local maxhp = UnitHealthMax(unit) or 1
    	local curhp = UnitHealth(unit) or 1
    	
    	if curhp >= 1e9 then
    		text = text .. ("%.0fb"):format(curhp / 1e9)
    	elseif curhp >= 1e6 then
    		text = text ..  ("%.0fm"):format(curhp / 1e6)
    	elseif curhp >= 1e3 or curhp <= -1e3 then
    		text = text ..  ("%.0fk"):format(curhp / 1e3)
    	else
    		text = text ..  curhp
    	end
    	
    	local hp = UnitExists(unit) and ( curhp / maxhp ) * 100 or 0		
    	if curhp < 100 then
    		text = text .. format("%.0f", hp)
    	end
    	
    	return text
    end

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •