Thread Tools Display Modes
04-30-06, 03:52 PM   #1
Khenan
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 5
See if the person that /w's me is in my party/raid

Hi, I've posted this on another forum, but since I didnt get any help there I'm trying my luck here. I've just started creating my first addon so pardon my noobines..

I want to check if the person that /w's me is in my party/raid. The name of the person that sends the message is saved in the variable arg2.
I try to check if he's in my raid/party by using

Code:
if (UnitInRaid(arg2) or UnitInParty(arg2)) then
...
end
but I get an error. If the person that sent the message is named Carl I get an error message saying "unknown unit: Carl" or something similar.

Looking at http://www.wowwiki.com/API_TYPE_UnitId none of the UnitIDs are right for me.. So how do I do it?

Thanks in advance
  Reply With Quote
04-30-06, 05:18 PM   #2
Gello
A Molten Giant
AddOn Author - Click to view addons
Join Date: Jan 2005
Posts: 521
As you've seen "unit" can't accept names like "Carl". "unit" expects "player", "party1", "party3", etc.

The simplest way is to search for someone named Carl in the raid or party:

Code:
local function NameIsGrouped(name)
  local inGroup
  if GetNumPartyMembers()>0 then
    for i=1,4 do
      inGroup = inGroup or (UnitName("party"..i)==name)
    end
  end
  if not inGroup and GetNumRaidMembers()>0 then
    for i=1,40 do
      inGroup = inGroup or (UnitName("raid"..i)==name)
    end
  end
  return inGroup
end
Then in your code:

Code:
OnEvent:
if event=="CHAT_MSG_WHISPER" and NameIsGrouped(arg2 or "") then
  --[[ someone in party/raid sent you that whisper ]]
end
Many balk at doing a loop like that on a whisper fearing it will be some performance hit, but the reality is it would take *a lot* of whispers for that to happen, much more than someone can reasonably expect to get.

If you really do plan on receiving *a lot* of whispers (like hundreds a second), you can set up a lookup table:

Code:
NamesInGroup = {}

local function PopulateNamesInGroup()
  for i in NamesInGroup do
    NamesInGroup[i] = nil
  end
  for i=1,4 do
    if UnitExists("party"..i) then NamesInGroup[UnitName("party"..i)]=1 end
  end
  for i=1,40 do
    if UnitExists("raid"..i) then NamesInGroup[UnitName("raid"..i)]=1 end
  end
end

OnLoad:
this:RegisterEvent("CHAT_MSG_WHISPER")
this:RegisterEvent("PARTY_ROSTER_CHANGED")
this:RegisterEvent("RAID_ROSTER_CHANGED")
this:RegisterEvent("PLAYER_LOGIN")

OnEvent:
if event=="CHAT_MSG_WHISPER" and NamesInGroup[arg2 or ""] then
  --[[ someone in raid or party sent a whisper]]
else
  PopulateNamesInGroup()
end
But if you're doing this because you're concerned about performance, don't stop there. The above will contribute to the mini lockup most experience when a party turns into a raid or when the raid changes. (Again I don't think performance is critical in reacting to whispers. In OnUpdates sure, in UNIT_HEALTH definitely, but something that happens infrequently, simplicity is good. I would prefer the first method at the top instead of contributing to the mini lockups.) To avoid the mini lockups, you'll want to only react to changes after a spasm of changes are done:

Code:
XML:
<Frame name="MyModFrame" ... hidden="true">
  <Scripts>
    ...
    <OnUpdate>
      MyMod_OnUpdate()
    </OnUpdate>
  </Scripts>
</Frame>

NamesInGroup = {}

local function PopulateNamesInGroup()
  for i in NamesInGroup do
    NamesInGroup[i] = nil
  end
  for i=1,4 do
    if UnitExists("party"..i) then NamesInGroup[UnitName("party"..i)]=1 end
  end
  for i=1,40 do
    if UnitExists("raid"..i) then NamesInGroup[UnitName("raid"..i)]=1 end
  end
end

OnLoad:
this:RegisterEvent("CHAT_MSG_WHISPER")
this:RegisterEvent("PARTY_ROSTER_CHANGED")
this:RegisterEvent("RAID_ROSTER_CHANGED")
this:RegisterEvent("PLAYER_LOGIN")

OnEvent:
if event=="CHAT_MSG_WHISPER" and NamesInGroup[arg2 or ""] then
  --[[ someone in raid or party sent a whisper]]
else
  this.timer = 0
  this:Show()
end

function MyMod_OnUpdate()
  this.timer = this.timer + arg1
  if this.timer > .5 then
    this:Hide()
    PopulateNamesInGroup()
  end
end
That will react to roster changes .5 seconds after the last of a roster spasm happens. So if you join a full raid of 39 people, PopulateNamesInGroup only runs once. If you join a group with one person in it, PopulateNamesInGroup only runs once.

But I'd ignore most of this post and do the first method.
  Reply With Quote
05-01-06, 07:30 AM   #3
Khenan
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 5
Thanks a lot, it worked perfectly! Just copied the code stright into my lua-file and its done!
I took the first piece of code. I'm not very worried a lot about performance issues since the code wont even be run on every whisper I get, just some whispers that cointain certain keywords. It'll run maybe 3-4 times/h tops.

Since we're on the topic, I've got a very similar problem.
How can I tell if the person that /w'd me (arg2) is in my guild?
If I could use arg2 with GetGuildInfo() it'd be very easy, but since I cant..
  Reply With Quote
05-01-06, 08:30 AM   #4
Gello
A Molten Giant
AddOn Author - Click to view addons
Join Date: Jan 2005
Posts: 521
You can't use arg2 with GetGuildInfo, but since you're scanning the party/raid you can make an adjustment to return the unit usable in GetGuildInfo:

Code:
local function NameIsGrouped(name)
  if GetNumPartyMembers()>0 then
    for i=1,4 do
      if UnitName("party"..i)==name then
        return "party"..i -- person in party, return party unit id
      end
    end
  end
  if not inGroup and GetNumRaidMembers()>0 then
    for i=1,40 do
      if UnitName("raid"..i)==name then
        return "raid"..i -- person in raid, return raid unit id
      end
    end
  end
  return -- return nil (not necessary), name not in party/raid
end
Instead of true/nil, the function above will return the unit or nil. If Carl is raid30, then it will return "raid30" that you can use in GetGuildInfo. The final return isn't necessary, a function that doesn't return a value will always return nil.

Code:
OnEvent:
if event=="CHAT_MSG_WHISPER" then
  local unit = NameIsGrouped(arg2 or "")
  if unit and GetGuildInfo(unit)==GetGuildInfo("player") then
    --[[ someone in party/raid in the same guild as you sent you that whisper ]]
  end
end
  Reply With Quote
05-01-06, 08:54 AM   #5
Khenan
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 5
Once again, thx for the reply.
However there is a small problem. I want to check if the person is in the same guild OR party/raid as me. But this script checks if he is both in the same guild AND party/raid as me, right?

I tried to change
Code:
if unit and GetGuildInfo(unit)==GetGuildInfo("player") then
to
Code:
if unit OR GetGuildInfo(unit)==GetGuildInfo("player") then
But that resulted in an error message (complaining about the use of GetGuildInfo("unit"), which makes sense since "unit" is returned from NameIsGrouped()..
  Reply With Quote
05-01-06, 09:31 AM   #6
Gello
A Molten Giant
AddOn Author - Click to view addons
Join Date: Jan 2005
Posts: 521
Oh can scrap previous post then.

To use GetGuildInfo it requires a unit id. There's no way to convert any random person to a unit id. If they're not in party, raid, target or the target of someone in party/raid (or their target) then anything requiring unit id's is out.

An alternative is to to register for GUILD_ROSTER_UPDATE, reuse a table that contains the name of everyone online that's in your guild. Similar to the second method in the first reply.
When the whisper happens, check if arg2 is in that table. It's quite a bit more complicated.
  Reply With Quote
05-01-06, 09:44 AM   #7
Khenan
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Apr 2006
Posts: 5
Ok, that doesnt sound like anything I'd be able to code, so I think I'll skip that part. The addon I'm writing will work without it, it was just a minor improvement. When I've done more lua-programming I might try to do that.

Thanks for your help and advice. I'll post a thread in the "Beta Interfaces/Scripts"-forum soon hopefully. =)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » See if the person that /w's me is in my party/raid


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