WoW API: GetNumGuildMembers

WoW API < GetNumGuildMembers

Returns the number of guild members.

numGuildMembers = GetNumGuildMembers([includeOffline]);

Parameters

Arguments

includeOffline
Boolean - include offline players in the headcount?

Returns

numGuildMembers
Integer - number of people in the guild (online / both online and offline) or 0 if the player is not in a guild.

Example

local numOnline, numTotal = (GetNumGuildMembers()), (GetNumGuildMembers(true));
DEFAULT_CHAT_FRAME:AddMessage(numTotal .. " guild members: " .. numOnline .. " online, " .. (numTotal - numOnline) .. " offline.");

Result

Displays the number of people online, offline, and the total headcount of your guild in the default chat frame.

Notes

You may need to call GuildRoster() first in order to obtain correct data. May return wrong values immediately after quitting a guild. Maximum returned value is 500.

There is another problem with the return values as they depend on SetGuildRosterShowOffline() setting. A simple program like

   DEFAULT_CHAT_FRAME:AddMessage("Firing SetGuildRosterShowOffline(false)");
   SetGuildRosterShowOffline(false)
   DEFAULT_CHAT_FRAME:AddMessage("GetNumGuildMembers(): "..tostring(GetNumGuildMembers()));
   DEFAULT_CHAT_FRAME:AddMessage("GetNumGuildMembers(true): "..tostring(GetNumGuildMembers(true)));
   DEFAULT_CHAT_FRAME:AddMessage("Firing SetGuildRosterShowOffline(true)");
   SetGuildRosterShowOffline(true)
   DEFAULT_CHAT_FRAME:AddMessage("GetNumGuildMembers(): "..tostring(GetNumGuildMembers()));
   DEFAULT_CHAT_FRAME:AddMessage("GetNumGuildMembers(true): "..tostring(GetNumGuildMembers(true)));

will show the difference.

Only total members is reliable. Online members changes depending on the setting of GetGuildRosterShowOffline(). If set to true, the number of online members is the same as total.

Take care, if your function called by GUILD_ROSTER_UPDATE event handling wants to set the show offline status via SetGuildRosterShowOffline(). This may cause, due to guild roster updates, a loop, where a user that has opened the guild panel can't switch the status anymore, even if you reset the original status in your function.

This is because SetGuildRosterShowOffline() fires an GUILD_ROSTER_UPDATE event if changed.

A function that gets reliable values (after at least one GUILD_ROSTER_UPDATE event happened) would not consider the flag at all and build its count on roster table. It can probably look like this

-- iterate over guild members list     
local total = GetNumGuildMembers(true)
local onlineTotal = 0
local offlineTotal = 0
for i = 1, total 
   do 
   -- get info
   local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName = GetGuildRosterInfo(i)
   if online
   then
      onlineTotal = onlineTotal + 1
   else
      offlineTotal = offlineTotal + 1
   end
end
return onlineTotal, offlineTotal, total