View Single Post
05-24-22, 11:25 PM   #3
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,878
Bag 0 is the backpack so checking there first for empty slots is probably not what you're looking for.

Code:
for bag = 1, 4 do
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:
  1. local LastBag, LastSlot, Remove
  2. local function SaveToBags()
  3.     while Remove do
  4.         PickupInventoryItem(Remove)
  5.         Remove = Remove+1
  6.         if Remove > INVSLOT_LAST_EQUIPPED then
  7.             Remove = nil
  8.             return
  9.         end
  10.         if CursorHasItem() then -- only process picked up items
  11.             local Placed
  12.             for bag = LastBag, 4 do -- bag 0 is the backpack
  13.                 local slots = GetContainerNumSlots(bag)
  14.                 for LastSlot = LastSlot, slots do
  15.                     local itemID = GetContainerItemID(bag, LastSlot)
  16.                     if not itemID then
  17.                         PutItemInBag(bag+19)
  18.                         Placed = true -- so scram
  19.                         break
  20.                     end
  21.                 end
  22.                 if bag > LastBag then
  23.                     LastBag = bag
  24.                     LastSlot = 1
  25.                 end
  26.                 if Placed then break end
  27.             end
  28.             if not Placed then -- no empty slots in bags
  29.                 local GotSpace
  30.                 for i=1, GetContainerNumSlots(0) do -- make sure the backpack isn't full as well
  31.                     if not GetContainerItemID(0, i) then
  32.                         GotSpace = true
  33.                         break
  34.                     end
  35.                 end
  36.                 if not GotSpace then  -- No more slots in backpack to put stuff either
  37.                     print("Not enough backpack space!")
  38.                     Remove = nil
  39.                     return
  40.                 end
  41.                 PutItemInBackpack()
  42.             end
  43.             break
  44.         end
  45.     end
  46. end
  47.  
  48. local f = CreateFrame("Frame")
  49. f:RegisterEvent("BAG_UPDATE")
  50. f:SetScript("OnEvent", function(self, event, ...)
  51.     if event == "BAG_UPDATE" then -- if you are using the handler for more than this event
  52.         if Remove then
  53.             SaveToBags()
  54.             return -- maybe, if you are doing other things with this event as well
  55.         end
  56. --  elseif event == "SOME_EVENT" then
  57.     end
  58. end)
  59.  
  60. SLASH_XXX1 = "/xx"
  61. SlashCmdList.XXX = function()
  62.     LastBag = 1
  63.     LastSlot = 1
  64.     Remove = INVSLOT_FIRST_EQUIPPED
  65.     SaveToBags()
  66. end

Edit: I didn't notice Kanegasi had made changes before doing this...
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 05-25-22 at 10:39 AM. Reason: Previous C_Timer code was a bit iffy
  Reply With Quote