Thread Tools Display Modes
05-04-06, 03:19 PM   #1
Vardelm
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 15
target's name in chat text?

Simple n00b question here: how do I get a unit's name into the text for a chat message? I'm using:
Code:
DEFAULT_CHAT_FRAME:AddMessage("blablablablabla <target>")
I'm sure this can't be hard. I've been doing some searching and haven't found anything to help yet.

Thanks!
  Reply With Quote
05-04-06, 03:30 PM   #2
Gello
A Molten Giant
AddOn Author - Click to view addons
Join Date: Jan 2005
Posts: 521
If using AddMessage, you'll want to use UnitName("target").

I also suggest using UnitName("target") or "<no target>" if there's a possibility you can have no target (to avoid concatenation with nil errors)

DEFAULT_CHAT_FRAME:AddMessage("blablablablabla "..UnitName("target"))

or

DEFAULT_CHAT_FRAME:AddMessage("blablablablabla "..(UnitName("target") or "<no target>"))
  Reply With Quote
05-04-06, 03:44 PM   #3
Vardelm
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 15
Gah!!! Of course!

May I also ask, what exactly does the ".." do before UnitName? I've seen that used, and I think I'm actually doing that at one point in this function. I just copied it from somewhere, so I'm not familiar with it.

BTW, this is the last question I have, and my little project is complete.
  Reply With Quote
05-04-06, 04:01 PM   #4
Gello
A Molten Giant
AddOn Author - Click to view addons
Join Date: Jan 2005
Posts: 521
.. is a string concatenation operator

local txt = "abc" .. "def" -- txt is now "abcdef"

If your target is "Bob" then "blahblahblah "..UnitName("target") will append the strings to make "blahblahblah Bob"

Veeeering off topic (servers down), a really neat trick with lua is the "or" bit. Lua is fine using or/and on any variable type. In an "or", if the first expression is false, then it returns the second value.

local txt = UnitName("target") or "<no target>"
with Bob targeted: UnitName("target") is "BoB", so it takes that return.
with no target: UnitName("target") is nil, so it uses the second expression, "<no target>"

An "and" returns the second expression if the first expression is true.

So you can do things like:

local channel = (GetNumRaidMembers()>0 and "RAID") or (GetNumPartyMembers()>0 and "PARTY") or "SAY"

which is the equivalent of

Code:
channel = "SAY"
if GetNumRaidMembers()>0 then
  channel = "RAID"
elseif GetNumPartyMembers()>0 then
  channel = "PARTY"
end
If you're in a raid, GetNumRaidMembers()>0 is true so the first expression is "RAID".
If you're not in a raid, but in a party, the first expression is false, but the second is true, so it's "PARTY".
If you're neither in a raid or party, it will use the last expression "SAY".

It's great for initializing defaults too. I need to get in the habit of always assuming nil for default values, so this isn't the best way, but a few of my mods that have options added over time get default values assigned this way:

ItemRack_Settings.RightClick = ItemRack_Settings.RightClick or "OFF" -- 1.1
ItemRack_Settings.TinyTooltip = ItemRack_Settings.TinyTooltip or "OFF" -- 1.2
ItemRack_Settings.ShowTooltips = ItemRack_Settings.ShowTooltips or "ON" -- 1.2
ItemRack_Settings.RotateMenu = ItemRack_Settings.RotateMenu or "OFF" -- 1.3
ItemRack_Users[user].Spaces = ItemRack_Users[user].Spaces or {} -- 1.3
ItemRack_Users[user].Sets = ItemRack_Users[user].Sets or {} -- 1.4

So it uses the existing value if one exists, or sets a default if one doesn't exist. (the -- 1.x are the version numbers)

You'll need to be pretty careful on the order of operations too.

x = y and z or 1

is very different from

x = (y and z) or 1

Last edited by Gello : 05-04-06 at 04:12 PM.
  Reply With Quote
05-04-06, 05:21 PM   #5
Vardelm
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 15
Thanks a BUNCH!!! That's very, very helpful!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » target's name in chat text?


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