WoWInterface

WoWInterface (http://www.wowinterface.com/forums/index.php)
-   AddOn Help/Support (http://www.wowinterface.com/forums/forumdisplay.php?f=3)
-   -   Stuf Unit LUA help (http://www.wowinterface.com/forums/showthread.php?t=37760)

Porsha 12-19-10 02:40 PM

Stuf Unit LUA help
 
I have this for my target name but I was hoping someone could help me with adding level and class color to the string.

function(unit)
local name = UnitName(unit)
if (name and string.len(name) > 10) then
name = string.gsub(name, "([^%s]+) ", function(s) return string.sub(s,1,1) .. "." end)
end
return name
end


This code works fine as is but I can figure out how to add class color to the text or the level to it either.

thanks much!

Taryble 12-19-10 03:09 PM

Try this - it's based on portions of the "Target Name" text I use in my StUF uf's. This leaves the level uncolored, but colors the name by class.

Code:

function(unit)
  local name, level = UnitName(unit), UnitLevel(unit)
  local clcolor = RAID_CLASS_COLORS[select(2, UnitClass(unit))]
  if (name and string.len(name) > 10) then
    name = string.gsub(name, "([^%s]+) ", function(s) return string.sub(s,1,1) .. "." end)
  end
  return "%s |cff%02x%02x%02x%s|r", level, clcolor.r*255, clcolor.g*255, clcolor.b*255, name
end


Xhelius 12-19-10 04:26 PM

Just a quick question....Maybe i should start a new topic but it is directly related to what this post is about...What is the advantage of using LUA code to display name versus using the tags that exist in STUF already?

Porsha 12-19-10 05:13 PM

Quote:

Originally Posted by Xhelius (Post 223701)
Just a quick question....Maybe i should start a new topic but it is directly related to what this post is about...What is the advantage of using LUA code to display name versus using the tags that exist in STUF already?

Well, I want to truncate the name and the STUF tags wont do that without the LUA code so yea...I need this code :)

BTW Worked fine but I have a weird thing with the name...between the level and the name I am getting an extra character - like on mob target its a "d" and on players it's an "5" and if I target myself, its a little 'o"(without quote marks).

c/p'd twice and still same results..any clue?

Taryble 12-19-10 11:23 PM

Yeah, 3 tiny typo's in the "return" line.

Change all of the *256 to *255. My bad. I'm going to edit the original post as well.

Taryble 12-19-10 11:35 PM

Quote:

Originally Posted by Xhelius (Post 223701)
Just a quick question....Maybe i should start a new topic but it is directly related to what this post is about...What is the advantage of using LUA code to display name versus using the tags that exist in STUF already?

Well, it's mostly for doing things that StUF's (fairly limited) tag system won't handle.

For example, this is what I use for my text1
Code:

function(unit, cache, textframe)
  local cc = RAID_CLASS_COLORS[select(2, UnitClass("player"))]
  if UnitExists(unit) then
    cc = RAID_CLASS_COLORS[select(2, UnitClass(unit))]
  end
  local curhp, maxhp = UnitHealth(unit), UnitHealthMax(unit)
  if curhp ~= maxhp then
    local perchp, diffhp = 100 * curhp / maxhp, maxhp - curhp
    return "|cff%02x%02x%02x%.0f|r%% | |cffff0000-%s|r",cc.r*255,cc.g*255,cc.b*255,perchp,diffhp
  else
    local name = UnitName("player")
    if UnitExists(unit) then
      name = UnitName(unit)
    end
    return "|cff%02x%02x%02x%s|r",cc.r*255,cc.g*255,cc.b*255,string.gsub(name, '%s?(.)%S+%s', '%1. ')
  end
end

This text, for one, has some things in it to catch "stupid StUF errors" when you use custom lua in "Config Mode" (initializing the class color to the player's class before checking to see if there's an actual unit to get a class color for, same with the name - StUF throws lua errors in config mode otherwise, which can stop some frames from showing up in config).

What this mainly does, however, is it gives you a class-colored name. Multiple-word names ("Angry Guardsman") gives an abbreviated version ("A. Guardsman"). However, if you are not at full HP, it shows an HP Deficit text, a pipe, and then HP Percent.

The only part of that, really, that I needed to use custom Lua for was the abbreviation of the names. However, because of that, I had to write the rest in Lua as well.

Here's a more interesting text that I use (my "Power" text).
Code:

function(unit, cache, textframe)
  local powtype, maxmp, curmp = UnitPowerType(unit), UnitPowerMax(unit), UnitPower(unit)
  local cc = PowerBarColor[powtype]
  local permp = 100*curmp/maxmp
  if (powtype == 1 or powtype == 3) then
    local _, class = UnitClass(unit)
    if (class == "DRUID" and unit == "player") then
      local maxdmp, curdmp, ccd = UnitPowerMax(unit,0), UnitPower(unit,0), PowerBarColor[0]
      if curdmp ~= maxdmp then
        return "|cff%02x%02x%02x%.0f|r%% | |cff%02x%02x%02x%s|r",ccd.r*255,ccd.g*255,ccd.b*255,100*curdmp/maxdmp,cc.r*255,cc.g*255,cc.b*255,curmp
      else
        return "|cff%02x%02x%02x%s|r",cc.r*255,cc.g*255,cc.b*255,curmp
      end
    else       
      return "|cff%02x%02x%02x%s|r",cc.r*255,cc.g*255,cc.b*255,curmp
    end
  elseif curmp ~= maxmp then
    return "|cff%02x%02x%02x%.0f|r%%",cc.r*255,cc.g*255,cc.b*255,permp
  elseif powtype == 0 then
    return "|cff%02x%02x%02x%s|r",cc.r*255,cc.g*255,cc.b*255,maxmp
  end
end

This one is rather fun. Basically, if you're a druid in shapeshift form, it shows your rage/energy - and if you're below full mana, it shows your mana as a percentage (separated by a pipe character). If you're a Warrior or Rogue, it just shows your rage/energy.

If you're anything else, it shows your percentage of power - but if you're at full power, it just shows your maximum power. (ie, 99% OR 78532, for example).

And, yes, all you Lua coders out there, please feel free to take a look at it and give me advice on how to make it better.

I haven't really done any programming since 1997, and Lua wasn't very common back then, so this is all sorta tossed together with half-remembered scraps of programming theory and style. :)

Porsha 12-20-10 05:37 PM

You are a doll! I am totally hijacking those for mine as they are tons more effiecent space wise than mine now - I have my power/hp on mourseover cause I havent had the room to "fit it all in" so thank you so much for sharing!

Funny how I looked /goggle for some threads on STUF LUA I could jack but found none (even the one on EJ was locked/outdated cause all my old codes no longer work with STUF any more) and the thread on the old WoW forums is gone now since Cata.

So thanks again and I'll be sure if I find more to post them in hopes they help some out.

OH - These below were taken from maybe Fuji or (dang I cant remember the name but they did a Paladin UI with vertical coding) these are UNTESTED for the most part but someone may know how to get the to work again....I'm sure its simple wording or something but I dont use them any more so...I will separate them with ---------

BLOCK OF TEXT INC!!

Code:

    function(unit, cache)
local str1 = ''
local str2 = ''
local str3 = ''
local str4 = ''
local str5 = ''

local _, partyMaster, _ = GetLootMethod()

if UnitAffectingCombat("player") == 1 then
str1 = 'C'
end
if IsRaidLeader("player")==1 then
str2 = 'L'
end
if UnitIsPVP("player") == 1 then
str3 = 'P'
end
if partyMaster == 0 then
str5 = '+'
end
if IsResting("player") == 1 then
str4 = 'R'
end

return '|cffff4500%s|r |cffffff00%s|r |cffff9900%s|r |cff00ff00%s|r |cffffffff%s|r ', str1,str2,str3,str4,str5
end

------------------------------

PET:
Code:

function(unit, cache, textframe)
    local name = UnitName( unit )
    if name ~= nil then
          local strLen = strlen( name )
          local tmp = ''
          local outStr = ''
 
          if strLen > 14 then
              strLen = 14
          end
 
          for i = 1, strLen, 1 do
              tmp = strsub( name, i, i )
              if i == 1 then
                    outStr = outStr..tmp
              else
                    outStr = outStr..'\n'..tmp
              end
          end
 
          return '%s', outStr
    end
end

--------------------------------------------

PET HAPPY:

Code:

function(unit, cache, textframe)
    local happiness,_ = GetPetHappiness(unit)
 
    if happiness == 1 then
          return "|cffff2400*|r"
    elseif happiness == 2 then
          return "|cffFFD700*|r"
    elseif happiness == 3 then
          return "|cff238E23*|r"
    end
end

----------------------------------

COMBO POINTS:

Code:

function(unit, cache, textframe)
    local comboPoints = GetComboPoints(unit)
    local outStr = ''
    if comboPoints ~= 0 then
          for i = 1, comboPoints, 1 do
              outStr = outStr .. '*\n'
          end
          return '|cffFFFF00%s|r', outStr
    end
end
 
function(unit, cache, textframe)
    local comboPoints = GetComboPoints(unit)
    local outStr = ''
    if comboPoints ~= 0 then
          for i = 1, comboPoints, 1 do
              outStr = outStr .. '*'
          end
          return '|cffFFFF00%s|r', outStr
    end
end

----------------------------------

STRING STATES:

Code:

function(unit, cache)
    local str1 = ''
    local str2 = ''
    local str3 = ''
    local str4 = ''
    local str5 = ''
    local cr1 = ''
    local cr2 = ''
    local cr3 = ''
    local cr4 = ''
    local cr5 = ''
 
    local _, partyMaster, _ = GetLootMethod()
 
    if UnitAffectingCombat("player") == 1 then
          str1 = 'C'
          cr1 = '\n'
    end
    if IsRaidLeader("player")==1 then
          str2 = 'L'
          cr2 = '\n'
    end
    if UnitIsPVP("player") == 1 then
          str3 = 'P'
          cr3 = '\n'
    end
    if partyMaster == 0 then
          str5 = '+'
          cr5 = '\n'
    end
    if IsResting("player") == 1 then
          str4 = 'R'
          cr4 = '\n'
    end
 
    return '|cffff4500%s|r%s|cffffff00%s|r%s|cffff9900%s|r%s|cff00ff00%s|r%s|cffffffff%s|r%s', str1, cr1, str2, cr2, str3, cr3, str4, cr4, str5, cr5
end


--------------------------------


Code:

function(unit, cache, textframe)
    local name = UnitName( unit )
    if name ~= nil then
          local strLen = strlen( name )
          local tmp = ''
          local outStr = ''
 
          if strLen > 14 then
              strLen = 14
          end
 
          for i = 1, strLen, 1 do
              tmp = strsub( name, i, i )
              if i == 1 then
                    outStr = outStr..tmp
              else
                    outStr = outStr..'\n'..tmp
              end
          end
 
          return '%s', outStr
    end
end

-------------------------------

HEALTH STRING:

Code:

function(unit, cache, textframe)
    local curHealth = UnitHealth( unit )
    local maxHealth = UnitHealthMax( unit )
    local healthPercent = floor( ( curHealth / maxHealth ) * 100 )
    local healthStr = tostring( healthPercent )
    local strLen = strlen( healthStr )
    local isEnemy = UnitIsEnemy( "Paloladin", unit )
    local isDead = UnitIsDead(unit)
 
    local tmp = ''
    local outStr = ''
 
    if isDead == nil then
          for i = 1, strLen, 1 do
              tmp = strsub( healthStr, i, i )
              if i == 1 then
                    outStr = outStr..tmp
              else
                    outStr = outStr..'\n'..tmp
              end
          end
    else
          outStr = '*\nD\nE\nA\nD\n*'
    end
 
    if isEnemy == nil then
          return '%s', outStr
    else
          return '|cffFF4500%s|r', outStr
    end
 
end


-----------------------------

Vertical name:
Code:

function(unit, cache, textframe)
    local name = UnitName( unit )
    if name ~= nil then
          local strLen = strlen( name )
          local tmp = ''
          local outStr = ''
 
          if strLen > 14 then
              strLen = 14
          end
 
          for i = 1, strLen, 1 do
              tmp = strsub( name, i, i )
              if i == 1 then
                    outStr = outStr..tmp
              else
                    outStr = outStr..'\n'..tmp
              end
          end
 
          return '%s', outStr
    end
end


----------------------------------

health vertical:
Code:

function(unit, cache, textframe)
    local curHealth = UnitHealth( unit )
    local maxHealth = UnitHealthMax( unit )
    local healthPercent = floor( ( curHealth / maxHealth ) * 100 )
    local healthStr = tostring( healthPercent )
    local strLen = strlen( healthStr )
    local isEnemy = UnitIsEnemy( "Paloladin", unit )
    local isDead = UnitIsDead(unit)
 
    local tmp = ''
    local outStr = ''
 
    if isDead == nil then
          for i = 1, strLen, 1 do
              tmp = strsub( healthStr, i, i )
              if i == 1 then
                    outStr = outStr..tmp
              else
                    outStr = outStr..'\n'..tmp
              end
          end
    else
          outStr = '*\nD\nE\nA\nD\n*'
    end
 
    if isEnemy == nil then
          return '%s', outStr
    else
          return '|cffFF4500%s|r', outStr
    end
 
end


Seerah 12-20-10 07:54 PM

*ahem* code blocks are your friend :)

carry on. :)

Porsha 12-21-10 09:18 AM

hrm sorry but what are "code blocks"? I know it was a large wall of text >< sorry but if there was a better format then if someone can change it or tell me how :)

Seerah 12-21-10 11:12 AM

The [code] tag is what we use to mark off sections of code. It keeps formatting and allows the code to be read easier. :) When writing a post, it is the button on the toolbar with the # sign.

Porsha 12-21-10 12:41 PM

Sweet thanks Seerah!

Seerah 12-21-10 08:11 PM

You're welcome. I took the liberty of adding the whitespace back in to make it easier to read. It also made me realize that you're missing the first line to that first chunk of code. ;)

Porsha 12-21-10 09:51 PM

Fixed it I hope :)

Taryble 12-21-10 11:55 PM

The first chunk of code in her post is repeated later in her post, actually, with the missing line(s) - the "String States" section.


All times are GMT -6. The time now is 10:42 AM.

vBulletin © 2014, Jelsoft Enterprises Ltd
©2012 ZAM Network LLC