Quantcast WoWInterface - View Single Post - Stuf Unit LUA help
View Single Post
12-20-10, 12:35 AM   #6
A Molten Giant
Taryble's Avatar
Join Date: Jan 2009
Posts: 811
Originally Posted by Xhelius View Post
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
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))]
  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
    local name = UnitName("player")
    if UnitExists(unit) then
      name = UnitName(unit)
    return "|cff%02x%02x%02x%s|r",cc.r*255,cc.g*255,cc.b*255,string.gsub(name, '%s?(.)%S+%s', '%1. ')
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).
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
        return "|cff%02x%02x%02x%s|r",cc.r*255,cc.g*255,cc.b*255,curmp 
      return "|cff%02x%02x%02x%s|r",cc.r*255,cc.g*255,cc.b*255,curmp
  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 
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.
-- Taryble
  Reply With Quote