Thread Tools Display Modes
07/02/17, 05:22 PM   #1
BoarGules
 
BoarGules's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2017
Posts: 34
EVENT_GUILD_DATA_LOADED puzzle

I want to know when a player switches guilds, that is, goes to the guild home page and chooses another guild from the drop-down list.

In the source code at esoui\ingame\guild\guildhsharedinfo.lua I thought I had found exactly what I wanted. It sets up a global variable GUILD_SHARED_INFO which is an instance of class GuildSharedInfo. This responds to the EVENT_GUILD_DATA_LOADED event and helpfully provides a .guildId attribute. Not surprisingly, this all works. If I switch guilds on the guild home page, GUILD_SHARED_INFO.guildId will tell me what the new guildId (integer, 1..5) is.

What I do not understand is that, when I subscribe to it, the event EVENT_GUILD_DATA_LOADED only fires once, at login (and not at /reloadui). Switching guilds on the guild home page does not cause it to fire again, or if it does, my subscription to the event doesn't report it. But GUILD_SHARED_INFO.guildId nonetheless reflects the change, so the class GuildSharedInfo clearly is getting the event reported even though my code doesn't.

I'm clearly missing some crucial point here. Anyone care to explain?
  Reply With Quote
07/02/17, 06:43 PM   #2
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
So, generally anything like EVENT_SOMETHING_HAPPENED is something that was done by the C code. So they are generally fired by things in the game. For example, ability use, fall damage, starting a duel, selling stuff, etc.

On the other hand, stuff to do with the UI does not have an event like the above. So opening the guild window, inventory, collections window, those kind of things generally don't fire an event. These aren't hard rules exactly, but they're general things you can go by. So EVENT_GUILD_DATA_LOADED is (probably) loading all the data of the guilds you are in.


In this case, you're likely looking for the Lua function that is called when you swap guilds, and that handles changing the UI from displaying one guild to another guild. Try using zgoo to look at functions that are in the guild scene or something. You could probably also use zgoo to find the name of the label for the guild, and then overwrite the setText function so it'll throw an error - then you can see which line of code in the esoui code has the function call yuou're looking for.

Once you find the function that's called, you could attempt to hook it to fire an 'event'

Last edited by Dolgubon : 07/02/17 at 06:46 PM.
  Reply With Quote
07/03/17, 03:05 AM   #3
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,566
All you need to do is prehook the OnGuildChanged function:
Lua Code:
  1. ZO_PreHook(GUILD_ROSTER_MANAGER, "OnGuildIdChanged", function(self)
  2.    local newGuildId = self.guildId
  3.    -- do something
  4. end)
That's what I do in GuildHallList and it works fine.

EVENT_GUILD_DATA_LOADED is only fired once at login and signals that the initial guild data has been loaded. Before that event, the guild screen won't show anything.
  Reply With Quote
07/03/17, 02:27 PM   #4
BoarGules
 
BoarGules's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2017
Posts: 34
The ZO_PreHook() call works brilliantly ... except for one thing. That calls my function before the guild id changes, and I would like to know what the new value is. I already know the current one.

Do I need to set up a callback for this or is there a simpler way?
  Reply With Quote
07/03/17, 02:37 PM   #5
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,566
Not sure what you are doing, but for me it works correctly. Otherwise my addon wouldn't work.
  Reply With Quote
07/03/17, 11:33 PM   #6
Shadowfen
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 83
How about

Code:
    ZO_PreHook(GUILD_ROSTER_MANAGER, "OnGuildIdChanged", function(self)
       local newGuildId = GetGuildId()
       -- do something
    end)
  Reply With Quote
07/04/17, 06:59 AM   #7
BoarGules
 
BoarGules's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2017
Posts: 34
Nailed it.

The prehook function needs to be:

Lua Code:
  1. function OnGuildIdChanged(self)
  2.     local guild_id = self.guildId
  3.     ...etc...
  4.     end

For some reason the function GetGuildId() always returns 1 in this context. And GUILD_SHARED_INFO.guildId is always one behind, because the prehook function gets called before the handler for EVENT_GUILD_DATA_LOADED in the GuildSharedInfo API class, and that handler is what changes the value in GUILD_SHARED_INFO.

Thanks for pointing me in the right direction.
  Reply With Quote
07/04/17, 11:38 AM   #8
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,566
Of course GetGuildId() will always return 1 when you use it like that. It requires the guild index as a parameter:
Lua Code:
  1. for i = 1, GetNumGuilds() do
  2. local guildId = GetGuildId(i)
  3. end

And I don't know why you use GUILD_SHARED_INFO.guildId when I showed you a working piece of code?
In my example I use self.guildId, where self is GUILD_ROSTER_MANAGER. GUILD_ROSTER_MANAGER.guildId is updated before OnGuildIdChanged is called, so in the prehook you already get the new id that way.

Lua Code:
  1. function ZO_GuildRosterManager:SetGuildId(guildId)
  2.     self.guildId = guildId
  3.     self.guildName = GetGuildName(guildId)
  4.     self.guildAlliance = GetGuildAlliance(guildId)
  5.  
  6.     self:OnGuildIdChanged()
  7.     self:RefreshAll()
  8. end
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » EVENT_GUILD_DATA_LOADED puzzle

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off