• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Limit Items

Status
Not open for further replies.
Level 33
Joined
Mar 27, 2008
Messages
8,035
A test map is attached.
A system that prevents a unit from holding more than 1 of the same item at a time.

The Event can also be related to an Event where the unit bought an item from the shop, not just collect from ground, okay ?
So don't worry about it, works for both collect item on ground and buys an item from shop Event
 

Attachments

  • Simple Prevent Same Item System.w3x
    13.4 KB · Views: 61
Level 7
Joined
Apr 27, 2011
Messages
272
Here is a simple JASS snippet for you ^.^

JASS:
//===========================================================================
// I made a snippet for you to use ^.^
//
// [1] Backup your map first!
//
// [2] Make a trigger in your map and convert it to custom text and clear its
// contents.
//
// [3] Copy paste everything in here and paste it inside the new trigger
//
// [4] Rename it as "Item Limiter"
//
// [5] And your ready to go!
//===========================================================================

function Display_Duration takes nothing returns real
    return 12.00 // <==this is how long the message will display itself
endfunction

function Message takes nothing returns string
    return "|cffff0000WARNING!: You can only have one weapon of the same type!" // <==this is the error message shown
    //when someone violates the item limit
endfunction

function Maximum_Item_Limit takes nothing returns integer
     return 1 // <== this is the maximum item limit
endfunction

function Item_Limiter takes nothing returns boolean
    local integer nloops       =0
    local integer nloopsmax    =5
    local integer ncounter     =0
    local unit    ItemOwner    =GetManipulatingUnit()
    local item    ItemAcquired =GetManipulatedItem()
    local item    ItemInSlot
    local integer ItemTypeId   =GetItemTypeId(ItemAcquired)
    
    loop
        exitwhen nloops>nloopsmax
        set ItemInSlot=UnitItemInSlot(ItemOwner,nloops)
        if(ItemInSlot!=null and GetItemTypeId(ItemInSlot)==ItemTypeId)then
            set ncounter=ncounter+1
        endif
        set nloops=nloops+1
    endloop
    
    if(ncounter>Maximum_Item_Limit())then
        call UnitRemoveItem(ItemOwner,ItemAcquired)
        DisplayTimedTextToPlayer(GetOwningPlayer(ItemOwner),0,0,Display_Duration(),Message())
    endif
    set ItemOwner=null
    set ItemInSlot=null
    return false
endfunction
//===========================================================================
function InitTrig_Item_Limiter takes nothing returns nothing
    local trigger Item_Limiter_Trg = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(Item_Limiter_Trg,EVENT_PLAYER_UNIT_PICKUP_ITEM )
    call TriggerAddCondition(Item_Limiter_Trg,Condition(function Item_Limiter) )
endfunction

You can now configure the warning message, its display time, and the maximum item limit.
 
Last edited:
Status
Not open for further replies.
Top