WoW:API GetNumGuildMembers: Difference between revisions

m
(typo. was online instead of offline)
Line 17: Line 17:
==Notes==
==Notes==
You may need to call [[API_GuildRoster|GuildRoster()]] first in order to obtain correct data. May return wrong values immediately after quitting a guild. Maximum returned value is 500.
You may need to call [[API_GuildRoster|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 [[API_SetGuildRosterShowOffline|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 [[API_GetGuildRosterShowOffline|GetGuildRosterShowOffline()]]. If set to true, the number of online members is the same as total.
Take care, if your function called by [[G#GUILD_ROSTER_UPDATE|GUILD_ROSTER_UPDATE]] event handling wants to set the show offline status via [[API_SetGuildRosterShowOffline|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 [[API_SetGuildRosterShowOffline|SetGuildRosterShowOffline()]] fires an [[G#GUILD_ROSTER_UPDATE|GUILD_ROSTER_UPDATE]] event if changed.
A function that gets reliable values (after at least one [[G#GUILD_ROSTER_UPDATE|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
Anonymous user