View Single Post
05/26/14, 06:11 PM   #16
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
Wow, thanks Garkin. I totally missed this post and just assumed the CallbackManager was for accessing ESO's built in functions etc. Didn't realise it is how you can generate your own events. Will have to see if it is something that can be utilised in gatherer to improve its interaction functionality, which is still glitching out if the mouse moves at the wrong millisecond rofl.

Originally Posted by Garkin View Post
You can create something like "EVENT_CAMPAIGN_JOINED"/"EVENT_CAMPAIGN_LEFT". But it is more like function for library, so other addons/objects can listen to that event too.

wiki: How do I generate my own "events" in Lua?

If you want to use global object for events, use CALLBACK_MANAGER. If you want to create your own callback object use yourObject = ZO_CallbackObject:New()
Methods you can use:
Lua Code:
  1. object:RegisterCallback(eventName, callback, arg)
  2. object:UnregisterCallback(eventName, callback)
  3. object:FireCallbacks(eventName, ...)

A good example of callback object is ingame scene. Every time when scene changes its state (SCENE_SHOWN, SCENE_HIDDEN, SCENE_SHOWING, SCENE_HIDING), custom event "StateChange" is fired:
Lua Code:
  1. scene:FireCallbacks("StateChange", oldState, newState)
You can register for that event to do something in your addon:
Lua Code:
  1. scene = SCENE_MANAGER:GetScene("worldMap")
  2. scene:RegisterCallback("StateChange",
  3.    function(oldState, newState)
  4.       if(newState == SCENE_SHOWING) then
  5.          MyAddon:RefreshData()
  6.       end
  7.    end)

Another good example could be inventory. There is just one global event "EVENT_INVENTORY_SINGLE_SLOT_UPDATE" for all inventory types (worn, backpack, bank, guildbank, buyback). All objects working with any inventory needs to do the same check over and over. Inventory manager listents to the event and then using the callback it passes processed information to other objects, so they do not need to that again - eg.:
Lua Code:
  1. CALLBACK_MANAGER:FireCallbacks("BackpackSlotUpdate", slotIndex)
  2. CALLBACK_MANAGER:FireCallbacks("InventorySlotUpdate", GetControl(slot.slotControl, "Button"))
  Reply With Quote