View Single Post
09/14/14, 12:51 PM   #2
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
You can use a few different way how to detect change in equiped items. First is event EVENT_INVENTORY_SINGLE_SLOT_UPDATE, second is registering for callback "WornSlotUpdate" - CALLBACK_MANAGER:RegisterCallback("WornSlotUpdate", function(slotControl) end), third is hooking EquipItem(...) function and maybe you can find some other ways.

I'd try to use event first:
Lua Code:
  1. local function OnSlotUpdate(eventCode, bagId, slotId, isNewItem, itemSoundCategory, updateReason)
  2.     if bagId == BAG_WORN and updateReason == INVENTORY_UPDATE_REASON_DEFAULT then
  3.         d("Equipped item changed.")
  4.     end
  5. end
  6. EVENT_MANAGER:RegisterForEvent("MyAddon", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, OnSlotUpdate)
I'm not really sure if the code above will work, I have typed it off the top of my head. What I'm not sure about is if this will be enough or if you will have to store item instance IDs first - GetItemInstanceId(bagId, slotIndex) - and then compare if the item was actually changed.

If you want to use callback, it will look like this:
Lua Code:
  1. local slotNames = {
  2.     [EQUIP_SLOT_HEAD] = "Head",
  3.     [EQUIP_SLOT_NECK] = "Neck",
  4.     [EQUIP_SLOT_CHEST]= "Chest",
  5.     [EQUIP_SLOT_SHOULDERS] = "Shoulders",
  6.     [EQUIP_SLOT_MAIN_HAND] = "Main Hand",
  7.     [EQUIP_SLOT_OFF_HAND] = "Off Hand",
  8.     [EQUIP_SLOT_WAIST] = "Waist",
  9.     [EQUIP_SLOT_LEGS] = "Legs",
  10.     [EQUIP_SLOT_FEET]= "Feet",
  11.     [EQUIP_SLOT_COSTUME] = "Costume",
  12.     [EQUIP_SLOT_RING1] = "Ring 1",
  13.     [EQUIP_SLOT_RING2] = "Ring 2",
  14.     [EQUIP_SLOT_HAND] = "Gloves",
  15.     [EQUIP_SLOT_BACKUP_MAIN] = "Backup Main Hand",
  16.     [EQUIP_SLOT_BACKUP_OFF] = "Backup Off Hand",
  17. }
  18.  
  19.  
  20. local function WornSlotUpdate(slotControl)
  21.     d("Equip slot changed: " .. slotNames[slotControl.slotIndex])
  22. end
  23.  
  24. CALLBACK_MANAGER:RegisterCallback("WornSlotUpdate", WornSlotUpdate)

Last edited by Garkin : 09/17/14 at 08:33 PM. Reason: added missing eventCode argument to the code
  Reply With Quote