Bag 0 is the backpack so checking there first for empty slots is probably not what you're looking for.
Edit:
In a tight loop, the bag inventory doesn't update in time so you the get the "That bag is full" notification which can cause the process to break. Best to wait for the bag update event before processing the next item:
Lua Code:
local LastBag, LastSlot, Remove
local function SaveToBags()
while Remove do
PickupInventoryItem(Remove)
Remove = Remove+1
if Remove > INVSLOT_LAST_EQUIPPED then
Remove = nil
return
end
if CursorHasItem() then -- only process picked up items
local Placed
for bag = LastBag, 4 do -- bag 0 is the backpack
local slots = GetContainerNumSlots(bag)
for LastSlot = LastSlot, slots do
local itemID = GetContainerItemID(bag, LastSlot)
if not itemID then
PutItemInBag(bag+19)
Placed = true -- so scram
break
end
end
if bag > LastBag then
LastBag = bag
LastSlot = 1
end
if Placed then break end
end
if not Placed then -- no empty slots in bags
local GotSpace
for i=1, GetContainerNumSlots(0) do -- make sure the backpack isn't full as well
if not GetContainerItemID(0, i) then
GotSpace = true
break
end
end
if not GotSpace then -- No more slots in backpack to put stuff either
print("Not enough backpack space!")
Remove = nil
return
end
PutItemInBackpack()
end
break
end
end
end
local f = CreateFrame("Frame")
f:RegisterEvent("BAG_UPDATE")
f:SetScript("OnEvent", function(self, event, ...)
if event == "BAG_UPDATE" then -- if you are using the handler for more than this event
if Remove then
SaveToBags()
return -- maybe, if you are doing other things with this event as well
end
-- elseif event == "SOME_EVENT" then
end
end)
SLASH_XXX1 = "/xx"
SlashCmdList.XXX = function()
LastBag = 1
LastSlot = 1
Remove = INVSLOT_FIRST_EQUIPPED
SaveToBags()
end
Edit: I didn't notice Kanegasi had made changes before doing this...