Warcraft III resources & community, 2003–2006 · archived

UnitReplaceItemsById

not rated
Submitted by AltSubmitterFunctions0 downloads
This nifty little function will replace any number of items of the same type in a unit or hero's inventory while keeping slot status the same.
*Explanation of parameters*
takes:
unit who = Unit whom you wish to replace items on

integer oldId = The item Id of the old item you wish to replace

integer newId = The item Id of the new item you wish to replace the old items with

integer howMany = The amount of items you wish to switch. Ex. Unit has 4 items of type oldId, if you pass 1 as the howMany variable, it will only replace 1 of the items, whereas if you pass 6 it will replace all.

Source

function UnitReplaceItemsById takes unit who, integer oldId, integer newId, integer howMany returns nothing
        local integer x = 0
        local integer y = 1
        local boolean array isKey
        local item temp
        loop
                exitwhen x == 6
                set isKey[x] = false
                if (GetItemTypeId(UnitItemInSlot(who,x)) == 'kysn') then
                        set isKey[x] = true                     
                elseif (GetItemTypeId(UnitItemInSlot(who,x)) == oldId) and (y <= howMany) then
                        set y = y + 1
                        set temp = UnitItemInSlot(who,x)
                        call UnitRemoveItem(who,temp)
                        call RemoveItem(temp)
                        call UnitAddItemById(who,newId)
                elseif (UnitItemInSlot(who,x) == null) then
                        call UnitAddItemById(who,'kysn')
                endif
                set x = x + 1
        endloop
        set x = 0
        loop
                exitwhen x == 6
                if (GetItemTypeId(UnitItemInSlot(who,x)) == 'kysn') and (isKey[x] == false) then
                        set temp = UnitItemInSlot(who,x)
                        call UnitRemoveItem(who,temp)
                        call RemoveItem(temp)
                endif
                set x = x + 1
        endloop
        set temp = null
endfunction

Comments

1
personally i would use something like this :wink: :

function UnitReplaceItemsById takes unit who, integer old, integer new, integer n returns nothing
    local integer i = 0
    local integer j = 0
    local item temp
    loop
        exitwhen i > 5 or n == j
        set temp = UnitItemInSlot(who,i)
        if GetItemTypeId(temp) == old then
            call RemoveItem(temp)
            call UnitAddItemToSlotById(who, new, i)
            set j = j + 1
        endif
        set i = i + 1
    endloop
    set temp = null
endfunction