Bank matching backpack items

From ESOUI Wiki

Jump to: navigation, search

The code below will bank all items whose item IDs are stored inside the table mySavedVars[itemId]

local function findTargetSlotId(targetItemId, bagId)
    for slotId=0, GetBagSize(bagId) do
            local itemId    = GetItemId(bagId, slotId)
        if itemId == targetItemId then return slotId end
    end
end
 
for slotId=0, GetBagSize(BAG_BACKPACK) do
    local itemId    = GetItemId(BAG_BACKPACK, slotId)
    if mySavedVars[itemId] then
        local targetBagSlot = findTargetSlotId(itemId, BAG_BANK)
        if targetBagSlot then
            local stackSize = GetSlotStackSize(BAG_BACKPACK, slotId)
            if IsProtectedFunction("RequestMoveItem") then
                CallSecureProtected("RequestMoveItem", BAG_BACKPACK, slotId, BAG_BANK, targetBagSlot, stackSize)
            else
                RequestMoveItem(BAG_BACKPACK, slotId, BAG_BANK, targetBagSlot, stackSize)
            end
        end
    end
end



The code above will use the bag's uncached data. You are able to build & use a cached bag contents table via this source code below:

local bagCache = SHARED_INVENTORY:GetOrCreateBagCache(BAG_BACKPACK)
        --For each item in that bag
        for _, data in pairs(bagCache) do
            local bagId     = data.bagId
            local slotIndex = data.slotIndex
          --do your stuff here
         end
Personal tools
Namespaces
Variants
Actions
Menu
Wiki
Toolbox