I've also had this problem before, with one of my addons
From my own experience:
- GetGuildLevel already exists (defined somewhere in Blizzard's C code) on the PLAYER_LOGIN event, or any of the ADDON_LOADED events, but does not return the correct value yet (returns a zero)
- Loading the Blizzard_GuildUI with LoadAddOn("Blizzard_GuildUI"), and/or ## OptionalDeps: Blizzard_GuildUI, and additionally calling GuildFrame_UpdateLevel(), doesn't have any effect on GetGuildLevel. It would still return a zero the first time a player logs in
- This only occurs on the first game session whenever WoW has been started up; afterwards (after the first OnUpdate) it would always immediately return the correct value, for any character/guild.
TL;DR:
On the first game session,
GetGuildLevel already exists, however it only returns the correct value after the first iteration of OnUpdate
Code:
LoadAddOn("Blizzard_GuildUI") -- no effect on GetGuildLevel
local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:RegisterEvent("VARIABLES_LOADED")
f:RegisterEvent("PLAYER_LOGIN")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:SetScript("OnEvent", function(...)
print(GetGuildLevel, GetGuildLevel(), ...)
-- prints "function:", "0" (on first game session)
end)
Code:
local timeElapsed, iterations = 0, 0
local function OnUpdate(self, elapsed)
timeElapsed = timeElapsed + elapsed
iterations = iterations + 1
if GetGuildLevel() > 0 then
print(GetGuildLevel(), timeElapsed, iterations)
-- prints "25", "0.017000000923872", "1"
self:SetScript("OnUpdate", function() end)
end
end
local f = CreateFrame("Frame")
f:SetScript("OnUpdate", OnUpdate)
I would just accept the "0" dummy value, and try to update it later afterwards