View Single Post
12/27/14, 06:52 AM   #2
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
I think that the easiest solution is checking if arguments are valid before you use them.

Lua Code:
  1. --for our purposes are valid bags:
  2. -- BAG_WORN (0)
  3. -- BAG_BACKPACK (1)
  4. -- BAG_BANK (2)
  5. -- BAG_GUILDBANK(3)
  6. --Other bags are invalid:
  7. -- BAG_BUYBACK (4),
  8. -- BAG_TRANSFER (5)
  9. -- BAG_DELETE(255)
  10.  
  11. local function IsSlotOccupied(bagId, slotIndex)
  12.     if (type(bagId) == "number" and bagId >= 0 and bagId <= 3) then
  13.         if (type(slotIndex) == "number" and slotIndex >= 0 and slotIndex <= GetBagSize(bagId) - 1) then
  14.             local _, stackCount = GetItemInfo(bagId, slotIndex)
  15.             return stackCount > 0
  16.         end
  17.     end
  18. end
  19. --function returns:
  20. --true: bag slot is occupied
  21. --false: bag slot is empty
  22. --nil: invalid bagId or slotIndex arguments


This is how it does inventory manager:

Lua Code:
  1. function ZO_InventoryManager:IsSlotOccupied(bagId, slotIndex)
  2.     local inventoryType = self.bagToInventoryType[bagId]
  3.     local slot = self.inventories[inventoryType].slots[slotIndex]
  4.  
  5.     return ((slot ~= nil) and (slot.stackCount > 0))
  6. end

Global reference to that function is PLAYER_INVENTORY:IsSlotOccupied(bagId, slotIndex)

Last edited by Garkin : 12/28/14 at 06:27 AM. Reason: Typo, variable i should be slotIndex.
  Reply With Quote