View Single Post
05-01-21, 10:10 PM   #7
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Any nameplate you see has a valid unit in the form of nameplaten where n goes up to 40. All functions that accept a unit work with nameplate units.

As for looking for nearby guildies, in my experience, it's better to maintain a local table of guildies and then compare players to that list. This bypasses the awkwardness of GetGuildInfo (which isn't reliable outside of inspect distance) as well as avoiding querying the guild roster each time.

Lua Code:
  1. local guildie,myrealm={}
  2. local function normalize(name,realm)
  3.     realm=lower((gsub(realm or (match(name,"%-(.*)")) or 'nil','[%p%s]','')))
  4.     name=lower((gsub((match(name,"(.-)%-")) or name,'[%p%s]','')))
  5.     return realm~='nil' and name..'-'..realm or name..'-'..myrealm
  6. end
  7. local f=CreateFrame("frame")
  8. f:RegisterEvent("GUILD_ROSTER_UPDATE")
  9. f:RegisterEvent("NAME_PLATE_UNIT_ADDED")
  10. f:RegisterEvent("PLAYER_LOGIN")
  11. f:SetScript("OnEvent",function(self,event,unit)
  12.     if event=="GUILD_ROSTER_UPDATE" then
  13.         self.tguild,self.online=GetNumGuildMembers()
  14.         if self.oguild~=self.online then
  15.             self.oguild=self.online
  16.             wipe(guildie)
  17.             for i=1,self.tguild do
  18.                 local name,_,_,_,_,_,_,_,online=GetGuildRosterInfo(i)
  19.                 if name and online then
  20.                     -- guilds on a connected realm list names with -realm in them
  21.                     guildie[normalize(name)]=true
  22.                 end
  23.                 if #guildie==self.oguild then
  24.                     return
  25.                 end
  26.             end
  27.         end
  28.     elseif event=="NAME_PLATE_UNIT_ADDED" then
  29.  
  30.         if guildie[normalize(UnitFullName(unit))] then
  31.             -- here's where the check happens
  32.             -- you can either do something with the nameplate
  33.             -- using C_NamePlate.GetNamePlateForUnit(unit) to get the frame
  34.             -- or something simple like play a sound to announce the guildie
  35.         end
  36.  
  37.     elseif event=="PLAYER_LOGIN" then
  38.         local _,realm=UnitFullName("player")
  39.         myrealm=lower((gsub(realm,"[%p%s]","")))
  40.     end
  41. end)


EDIT: I forgot that GetGuildRosterInfo returns a GUID. Also, the code above is basically a modified version of my own code in an addon that deals with name-realm data coming from sources that don't include a GUID and only deals with those online. Below is a much simpler version of caching your guild.

Lua Code:
  1. local guildie={}
  2. local f=CreateFrame("frame")
  3. f:RegisterEvent("GUILD_ROSTER_UPDATE")
  4. f:RegisterEvent("NAME_PLATE_UNIT_ADDED")
  5. f:SetScript("OnEvent",function(self,event,unit)
  6.     if event=="GUILD_ROSTER_UPDATE" then
  7.         local total=GetNumGuildMembers()
  8.         wipe(guildie)
  9.         for i=1,total do
  10.             local guid=select(17,GetGuildRosterInfo(i))
  11.             if guid then
  12.                 guildie[guid]=true
  13.             end
  14.         end
  15.     elseif event=="NAME_PLATE_UNIT_ADDED" then
  16.  
  17.         if guildie[UnitGUID(unit))] then
  18.             -- here's where the check happens
  19.             -- you can either do something with the nameplate
  20.             -- using C_NamePlate.GetNamePlateForUnit(unit) to get the frame
  21.             -- or something simple like play a sound to announce the guildie
  22.         end
  23.  
  24.     end
  25. end)

Last edited by Kanegasi : 05-01-21 at 11:41 PM.
  Reply With Quote