WoW:API PickupContainerItem: Difference between revisions

found flaw in swap items example; marked it as dangerous; added example that is actually safe
(Details + {{wowapi}})
(found flaw in swap items example; marked it as dangerous; added example that is actually safe)
Line 23: Line 23:
:* If the cursor currently contains an item (check with [[API_CursorHasItem|CursorHasItem()]]), calling this will place the item currently on the cursor into the specified bag slot. If there is already an item in that bag slot, the two items will be exchanged.
:* If the cursor currently contains an item (check with [[API_CursorHasItem|CursorHasItem()]]), calling this will place the item currently on the cursor into the specified bag slot. If there is already an item in that bag slot, the two items will be exchanged.
:* If the cursor is set to a spell (typically enchanting and poisons, check with [[API_SpellIsTargeting|SpellIsTargeting()]]), calling this specifies that you want to cast the spell on the item in that bag slot.
:* If the cursor is set to a spell (typically enchanting and poisons, check with [[API_SpellIsTargeting|SpellIsTargeting()]]), calling this specifies that you want to cast the spell on the item in that bag slot.
: Trying to pickup the same item twice in the same "time tick" does not work (client seems to flag the item as "modified" and waits for the server to sync). To get around the problem, you need to use an _OnUpdate function, check that at least .1 time has elapsed since the last time before picking up the same item.
: Trying to pickup the same item twice in the same "time tick" does not work (client seems to flag the item as "locked" and waits for the server to sync). This is only a problem if you might move a single item multiple times (i.e., if you are changing your character's equipped armor, you are not likely to move a single piece of armor more than once).  If you might move an object multiple times in rapid succession, you can check the item's 'locked' flag by calling [[API GetContainerItemInfo|GetContainerItemInfo]].  If you want to do this, you should leverage OnUpdate to help you.  Avoid constantly checking the lock status inside a tight loop.  If you do, you risk getting into a race condition.  Once the repeat loop starts running, the client will not get any communication from the server until it finishes.  However, it will not finish until the server tells it that the item is unlocked.  Here is some sample code that illustrates the problem.
 
function DangerousSwapItems(bag1, slot1, bag2, slot2)
    ClearCursor()
    repeat
        _, _, locked1 = [[API GetContainerItemInfo|GetContainerItemInfo]](bag1, slot1)
        _, _, locked2 = [[API GetContainerItemInfo|GetContainerItemInfo]](bag2, slot2)
        --[[ DANGER! At this point, locked1 and locked2 will not change.  They will not change
              until the server tells us that the items in question have become unlocked, and that  
              will not happen until we finish this call stack (i.e. return from this function,
              then his caller, then his caller, all the way up our lua code). ]]
    until not locked1 and not locked2
    [[API PickupContainerItem|PickupContainerItem]](bag1, slot1)
    [[API PickupContainerItem|PickupContainerItem]](bag2, slot2)
end
DangerousSwapItems(1, 1, 1, 2)
DangerousSwapItems(1, 2, 1, 3)  --DANGER!  Item in (1, 2) is likely still locked, and this function will never return!
 
: A potentially better way to do this is to use coroutines:
 
function SaferSwapItems(bag1, slot1, bag2, slot2)
    ClearCursor()
    while true do                    -- This is not really an infinite loop, as you will see in a few lines
        local _, _, locked1 = [[API GetContainerItemInfo|GetContainerItemInfo]](bag1, slot1)
        local _, _, locked2 = [[API GetContainerItemInfo|GetContainerItemInfo]](bag2, slot2)
        if locked1 or locked2 then    -- During each iteration of the loop, we will either...
            coroutine.yield()        --    yield, thus returning from SaferSwapItems
        else                          --  or
            break                    --    break, thus returning from SaferSwapItems
        end
    end
    [[API PickupContainerItem|PickupContainerItem]](bag1, slot1)
    [[API PickupContainerItem|PickupContainerItem]](bag2, slot2)
end
co = coroutine.create(SaferSwapItems)
function OnUpdate()
    coroutine.resume(co)    -- We should actually look at the return value from resume, because
                            -- it will be false when the coroutine is actually finished
end
Anonymous user