Thread Tools Display Modes
02-07-12, 04:47 PM   #1
Zapotek
A Defias Bandit
Join Date: Feb 2012
Posts: 3
LUA string editing (stuf unitframes)

I've been searching this forum and other places for this and found pieces of the answer but not a complete solution.

I have recently installed Stuf unitframes and set it up for my shaman healer. In the dungeon finder, all names of toons from other realms look like " Charactername (*) ". I want to trim the " (*) " from the name on my bars, just for a clean look.

I guess the easiest way is identifying the blank and trimming anything after that? I have some basic programming understanding but I've never done LUA before today. I got my bars to show group role and a counter for my Earth Shield stacks on the target at least.

Any help in solving this simple issue and/or teaching me basic LUA is much appreciated.

Last edited by Zapotek : 02-07-12 at 04:51 PM.
  Reply With Quote
02-07-12, 06:08 PM   #2
Zapotek
A Defias Bandit
Join Date: Feb 2012
Posts: 3
Ok, I guess I didn't try hard enough last time. I got it working, but there's likely room for some improvement. Here's where I found most of what I needed. The final code looks like this:

Code:
function(unit)
local UntrimmedName = UnitName("target")
if string.match(UntrimmedName, ' ')==nil
then return UntrimmedName
else return string.match(UntrimmedName, ".*%s")
end
end
If there are no spaces in the name, it simply returns the name. If there are, it trims anything after the space and returns the rest.

Any suggestions?
  Reply With Quote
02-07-12, 06:41 PM   #3
Barjack
A Black Drake
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 89
If you're actually doing your own calls to the UnitName API function you shouldn't actually need to do anything as the (*) does not come from that function but is rather added by your unit frames addon. In that case, all you need to use is

Code:
UnitName(unit)
If you did however have a situation where you have a string with the (*) in it, I'd do something like this: (as a general function here which may not be what your addon actually needs)

Code:
function stripRealmName (name)
  local newname = string.match(name, "[^ ]*")
  return newname
end
Matching that way will match any name, even without spaces, so we can avoid the "check" first. It's not strictly necessary though, but if you do it your way with the space-check first, you'll probably want to use "(.*) " or "(.*)%s" instead of just ".* " or ".*%s". The () around the .* are a capture group, which is used to make sure you don't also grab the trailing space itself even though you are checking for it, which is what happens in your example.

Another thing to note here is there are slight differences in where these things will match should the string contain multiple spaces. "(.*) " will match until the last space in the string, for example, but "[^ ]*" will match until the first.

Another thing to think about, though, is other situations where a unit's name might have spaces. Obviously NPCs and such won't be party members, but player's pets and vehicles might have spaces in them. So if we did have to do the stripping manually, it might be necessary to check for the full " (*)" with a pattern like " %(%*%)" or even with a UnitIsPlayer call. However, directly using UnitName means we don't have to bother with that either.
  Reply With Quote
02-08-12, 05:32 AM   #4
Zapotek
A Defias Bandit
Join Date: Feb 2012
Posts: 3
Thank you Barjack for a very comprehensive answer.

I assumed since stuf's own [name] tag returned the name with the (*), UnitName(unit) would too. It does not. So all I needed really was that function. I don't mind having done a lot of extra work though. I learned some things in the process, and with posts like yours, I learn a lot more.

I'll be back with more newb LUA questions for sure. ^^
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » LUA string editing (stuf unitframes)


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