• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • ✅ The POLL for Hive's Texturing Contest #33 is OPEN! Vote for the TOP 3 SKINS! 🔗Click here to cast your vote!

[JASS] Replacing Items from Specific Inventory Slots

Status
Not open for further replies.

q.b

q.b

Level 3
Joined
Apr 21, 2014
Messages
21
Before anything else, I'd like to apologize for posting this knowing that this has been asked many times before. I've read the same topics in the past myself, but I have forgotten their titles. Now, I have the same problem:

Suppose I have a usable item that replaces it with another item. Item A is in slot no. 4, for example, and when clicked, Item B would replace it in the exact same inventory slot. However, for some reason the new item is always placed at the topmost left corner of the inventory. It works on an empty map, but not on mine.

JASS:
function ItemReplace_C takes nothing returns boolean
    return (GetItemTypeId(GetManipulatedItem()) == 'ratf') or (GetItemTypeId(GetManipulatedItem()) == 'ckng')
endfunction

function ItemReplace_A takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local integer index = 0
    local integer max = 0
    local integer id
    local item i
    local boolean off = false
    loop
        set i = UnitItemInSlot(u, index)
        if (i == GetManipulatedItem()) then
            if (GetItemTypeId(i) == 'ratf') then
                set id = 'ckng'
            elseif (GetItemTypeId(i) == 'ckng') then
                set id = 'ratf'
            endif 
            call RemoveItem(i)
            call UnitAddItemToSlotById(u, id, index)
            set off = true
        endif
        exitwhen index > 5 or off == true
        set index = index + 1
    endloop
    set u = null
    set i = null
endfunction

function InitTrig_item takes nothing returns nothing
    local trigger trg = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(trg, EVENT_PLAYER_UNIT_USE_ITEM)
    call TriggerAddCondition(trg, Condition(function ItemReplace_C))
    call TriggerAddAction(trg, function ItemReplace_A)
    set trg = null
endfunction

This is the code on my empty test map, and it's the exact same (yes, even the item IDs just for the sake of testing it). However, it works exactly as intended on the empty map but not on my main map.

Please help. I've even tried calling out UnitAddItemToSlotById with a specific inventory number (calll UnitAddItemToSlotById(u, 'id', 4))but the new item would still pop out at the topmost left corner of the inventory. I've been trying to get around this for the past few days and nothing I've tried worked.
 

q.b

q.b

Level 3
Joined
Apr 21, 2014
Messages
21
disable other triggers that work with manipulated item one-by-one. some other trigger has to do with this stg. then post the trigger which may cause of said problem. if all your triggers are disabled and still problem occures then maybe u just miss something here

That sounds good. I'll go try it out. Thanks.

This is how to get the clicked item slot:
local integer clickedSlot = GetIssuedOrderId() - 852008

I'm quite sure that you need to run a 0.00 second timer after removing the item for the game to consider the item to be lost for the unit.

Wow, I didn't know something like that existed. Thanks.
 
Status
Not open for further replies.
Top