View Single Post
05/26/14, 05:46 AM   #6
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by lyravega View Post
Meanwhile, can I create such a custom event? (Garkin and Seerah, you may now remember my name as I've asked you two how to create custom events, thanks to your helps but I still have no clue for some reason )
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