Thread Tools Display Modes
11/18/14, 10:08 AM   #1
brekal
Guest
Posts: n/a
FindFirstEmptySlotInBag not updating

Hey guys it's me again ;-)

I have some issues with the function FindFirstEmptySlotInBag(BAG_BACKPACK).

I want to move items from the bank into my inventory but it always just moves one item.
I dumped the values of the variables and found out that the Destination SlotID (slotId_Bag) always stays the same.
So I think that this is the issue - but unfortunately I cannot image why it is not updating because I call it in every loop of the for-loop so it should update correctly.

Lua Code:
  1. function LootManager.GetIngredientsFromBank()
  2.     local name
  3.     local item
  4.     local slotId
  5.     local count
  6.     local bagId = BAG_BANK
  7.     local slots = GetBagSize(bagId)
  8.     local slotId_Bag
  9.     local bag_Dest = BAG_BACKPACK
  10.  
  11.     for i=0, slots - 1 do
  12.         slotId = i
  13.         name = zo_strformat(SI_UNIT_NAME, GetItemName(bagId, slotId))
  14.         item, count = GetItemInfo(bagId, slotId)
  15.         slotId_Bag = FindFirstEmptySlotInBag(bag_Dest)
  16.  
  17.         if name == LootManager.savedVars.ingredient1 then
  18.             LootManager.MoveItem(bagId, slotId, bag_Dest, slotId_Bag, count)
  19.         end
  20.  
  21.         if name == LootManager.savedVars.ingredient2 then  
  22.             LootManager.MoveItem(bagId, slotId, bag_Dest, slotId_Bag, count)
  23.         end
  24.        
  25.         if name == LootManager.savedVars.ingredient3 then
  26.             LootManager.MoveItem(bagId, slotId, bag_Dest, slotId_Bag, count)
  27.         end
  28.  
  29.     end
  30.  
  31. end
  32.  
  33. ------------------------------------------------------------------------------------------
  34. ------------------------------------------------------------------------------------------
  35.  
  36. function LootManager.MoveItem(srcBag, srcSlot, destBag, destSlot, quantity)
  37.     ClearCursor()
  38.     zo_callLater(function()
  39.         if CallSecureProtected("PickupInventoryItem", srcBag, srcSlot, quantity) then
  40.             CallSecureProtected("PlaceInInventory",destBag, destSlot)
  41.         end
  42.     end, 500)  
  43.     ClearCursor()  
  44.     d("Moved: " .. zo_strformat(SI_UNIT_NAME, GetItemName(srcBag, srcSlot)) .. " x " .. quantity .. " to inventory")
  45.     d("SrcBag: " .. srcBag .. " SrcSlot: " .. srcSlot .. " DestBag: " .. destBag .. " DestSlot: " .. destSlot .. " Quantity: " .. quantity)
  46. end

Do you see any error here?

Thanks in advance
  Reply With Quote
11/18/14, 11:03 AM   #2
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
That's probably by design. The items are not actually moved immediately, your commands are carried out after your handler exits.

Some time ago I wrote my own function for finding an empty slot, which allows me to start the search after the previous one:
Lua Code:
  1. local function findEmptySlotInBag(bagId, prevIndex, lastIndex)
  2.     local slotIndex = prevIndex or -1
  3.     while slotIndex < lastIndex do
  4.         slotIndex = slotIndex + 1
  5.         if GetItemType(bagId, slotIndex) == ITEMTYPE_NONE then
  6.             return slotIndex
  7.         end
  8.     end
  9.     return nil
  10. end
  11.  
  12. -- usage
  13. local numSlots = GetBagSize(BAG_BANK)
  14. local emptyIndex = findEmptySlotInBag(BAG_BANK, nil, numSlots - 1)
  15. while emptyIndex do
  16.    -- we have an empty slot in bank
  17.    ... put something in there
  18.     emptyIndex = findEmptySlotInBag(BAG_BANK, emptyIndex, numSlots - 1)
  19. end
  20. -- bank full
  Reply With Quote
11/18/14, 12:29 PM   #3
brekal
Guest
Posts: n/a
Thanks for your reply.

I already thought about making my own Slotfinder but i hoped that there was some working built-in function ....
  Reply With Quote
11/18/14, 09:15 PM   #4
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
I'm pretty sure the keys in the slots table matches the slotIndices. So you could loop through the slots and check to see if each key exists in the table.

Lua Code:
  1. local inventory = PLAYER_INVENTORY.inventories[INVENTORY_BACKPACK].slots
  2. local bagSize = GetBagSize(BAG_BACKPACK)
  3.    
  4. for i = 0, bagSize-1 do
  5.    if not inventory[i] then
  6.       -- do whatever inventory[i] is an empty slot
  7.    end
  8. end

Or you could make it into a function and each time you find an empty slot throw it into a table, then return the table & you'll have all of the empty slots to do whatever you want with them.

Although merlights answer is probably better for what your doing, just giving some options.
  Reply With Quote
11/18/14, 10:05 PM   #5
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
This issue is because of 500ms delay in you function LootManager.MoveItems. All calls of the FindFirstEmptySlotInBag within this 500ms will return still the same slot. If you remove zo_callLater from the function, it should work correctly.

And I think it will be better if you use RequestMoveItem function instead of PickupInventoryItem and PlaceInInventory functions.
  Reply With Quote
11/19/14, 05:59 AM   #6
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Originally Posted by Garkin View Post
This issue is because of 500ms delay in you function LootManager.MoveItems. All calls of the FindFirstEmptySlotInBag within this 500ms will return still the same slot. If you remove zo_callLater from the function, it should work correctly.
They might have changed it, but I don't think so. I did the same thing some time ago (the addon in question still has APIVersion 100008), and FindFirstEmptySlotInBag kept returning the same value over and over.

Similar thing happens if you handle EVENT_LOOT_UPDATED. Functions GetLootMoney/GetNumLootItems will keep returning the values applicable when the handler was called, even after you call LootMoney/LootItemById inside the handler. After the handler exits, items are actually moved, and a new event generated with updated state.

Originally Posted by Garkin View Post
And I think it will be better if you use RequestMoveItem function instead of PickupInventoryItem and PlaceInInventory functions.
Good point, this one was added recently. And has a better name - request indicates the action is not carried out immediately.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » FindFirstEmptySlotInBag not updating


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