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:
local guildie,myrealm={}
local function normalize(name,realm)
realm=lower((gsub(realm or (match(name,"%-(.*)")) or 'nil','[%p%s]','')))
name=lower((gsub((match(name,"(.-)%-")) or name,'[%p%s]','')))
return realm~='nil' and name..'-'..realm or name..'-'..myrealm
end
local f=CreateFrame("frame")
f:RegisterEvent("GUILD_ROSTER_UPDATE")
f:RegisterEvent("NAME_PLATE_UNIT_ADDED")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent",function(self,event,unit)
if event=="GUILD_ROSTER_UPDATE" then
self.tguild,self.online=GetNumGuildMembers()
if self.oguild~=self.online then
self.oguild=self.online
wipe(guildie)
for i=1,self.tguild do
local name,_,_,_,_,_,_,_,online=GetGuildRosterInfo(i)
if name and online then
-- guilds on a connected realm list names with -realm in them
guildie[normalize(name)]=true
end
if #guildie==self.oguild then
return
end
end
end
elseif event=="NAME_PLATE_UNIT_ADDED" then
if guildie[normalize(UnitFullName(unit))] then
-- here's where the check happens
-- you can either do something with the nameplate
-- using C_NamePlate.GetNamePlateForUnit(unit) to get the frame
-- or something simple like play a sound to announce the guildie
end
elseif event=="PLAYER_LOGIN" then
local _,realm=UnitFullName("player")
myrealm=lower((gsub(realm,"[%p%s]","")))
end
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:
local guildie={}
local f=CreateFrame("frame")
f:RegisterEvent("GUILD_ROSTER_UPDATE")
f:RegisterEvent("NAME_PLATE_UNIT_ADDED")
f:SetScript("OnEvent",function(self,event,unit)
if event=="GUILD_ROSTER_UPDATE" then
local total=GetNumGuildMembers()
wipe(guildie)
for i=1,total do
local guid=select(17,GetGuildRosterInfo(i))
if guid then
guildie[guid]=true
end
end
elseif event=="NAME_PLATE_UNIT_ADDED" then
if guildie[UnitGUID(unit))] then
-- here's where the check happens
-- you can either do something with the nameplate
-- using C_NamePlate.GetNamePlateForUnit(unit) to get the frame
-- or something simple like play a sound to announce the guildie
end
end
end)