• It's time to dig through Hive's model database and dust off some oldie but goldie models and enter Hive's Texturing Contest #31 : Repurposing! Click here to enter!
  • 🏆 Hive's 5th HD Modeling Contest: Creep Camp is now open! Click here to enter!

Item Stacking

Level 20
Jul 6, 2009
I wanted to make items stacking system,but single inventory slot shouldn't have more than 100 charges
I've done this far:
  • Arrows Stack
    • Events
      • Unit - A unit Acquires an item
    • Conditions
      • (Item-type of (Item being manipulated)) Equal to Arrows
      • ((Hero manipulating item) has an item of type (Item-type of (Item being manipulated))) Equal to True
      • (Item being manipulated) Not equal to (Item carried by (Hero manipulating item) of type (Item-type of (Item being manipulated)))
    • Actions
      • For each (Integer A) from 1 to 6, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Item-type of (Item carried by (Hero manipulating item) in slot (Integer A))) Equal to Arrows
              • (Charges remaining in (Item carried by (Hero manipulating item) in slot (Integer A))) Less than or equal to 50
            • Then - Actions
              • Item - Set charges remaining in (Item carried by (Hero manipulating item) of type (Item-type of (Item being manipulated))) to ((Charges remaining in (Item carried by (Hero manipulating item) of type (Item-type of (Item being manipulated)))) + (Charges remaining in (Item being manipulated)))
              • Item - Remove (Item being manipulated)
            • Else - Actions
But items still stack more than 100 in single inventory slot.
How to make items of type "arrows" stack up to 100 charges?

EDIT: Also,when buying item "arrows",hero gets 50 charges.
Level 5
Nov 22, 2009
Why is there a loop? Also for the else action put set charges remaining in (item carried by (hero manipulating item) of type (Item-type of (item being manipulated))) to 100
Level 18
Jan 21, 2006
The loop is to run through each inventory slot. It is necessary.

The reason it keeps stacking is because you're only filtering items with 50 or more charges. What you need to do is remove that condition altogether, and instead of just blindly adding the charges, you need to setup a real variable. You'll want to set the variable to the value of the two item charges combined.

Keep in mind: item charges A - (100 - item charges B) = item charges A (after)

if  (both charges  "is greater than"  100) then
        set item charges A = item charges A - (100 - item charges B)
        set item charges B = 100
        set item charges A = "both charges"
        remove item B

I can't copy your GUI, so here's some "pseudo" code. You should be able to understand the logic behind it.
Level 18
Jan 21, 2006
It really doesn't matter which is which. As long as one of the items has the 100 charges and the other has the correct amount left. If you have two items with 100 charges it really doesn't matter which one is which. Also you need a condition to make sure that the system ignores items unless their charge is less than 100.

The loop is to find out which item "is in the inventory". (Item being manipulated) refers to the item being picked up, but how do you intend on finding the item in the unit's inventory without using a loop?

Why is this page all wide? The other threads fit the size of my screen but this one spans way across.
Level 18
Jan 21, 2006
I made this but it seems you solved your problem. Here you go anyway:

library ItemStacking initializer init
// you need to manually set the value of "ItemStackTypeCount" and "ItemStackType" (its an array) in GUI. These variables 
// are declared in GUI Variables.

private function onItemPickupFilter takes nothing returns boolean
    local integer i = 0
    local integer id = GetItemTypeId(GetManipulatedItem())  // this is the item-type of the item being picked up
    if GetItemCharges(GetManipulatedItem())>=100 then
        // if the item being picked up has 100 (or more, some how) charges, then it is already full and 
        // cannot transfer charges.
        return false
        exitwhen(i == udg_ItemStackTypeCount)
        // checks to see whether or not the item-type of the item being acquired is included in the global stack
        // of item-types (integer ids)
        if (udg_ItemStackType[i] == id) then
            return true
        set i=i+1

    return false

private function onItemPickup takes nothing returns nothing
    local integer i
    local integer size
    local integer aCharges  // item being manipulated/acquired
    local integer bCharges  // item in slot
    local item pickedup 
    local item inventory
    local unit trig
    set pickedup    = GetManipulatedItem()
    set trig        = GetTriggerUnit()
    set size        = UnitInventorySize(GetTriggerUnit())
    set i = 0
        set inventory = UnitItemInSlot(trig, i)
        if (GetItemTypeId(inventory) == GetItemTypeId(pickedup)) and (inventory!=pickedup) then
            set aCharges = GetItemCharges(pickedup)
            set bCharges = GetItemCharges(inventory)
            if (aCharges + bCharges) > 100 then
                // if combined they are more than 100 charges, you need to split the charges between
                // the tewo items so that 1 has 100.
                set aCharges = aCharges - (100-bCharges)
                set bCharges = 100
                call SetItemCharges(pickedup, aCharges)
                set bCharges = aCharges+bCharges
                // if the item transfers all of its charges, then it can be removed.
                call RemoveItem(GetManipulatedItem())
            // set the charges of the item in the slot
            call SetItemCharges(inventory, bCharges)
        set i=i+1
    set trig        = null
    set pickedup    = null
    set inventory   = null


public function init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i = 0
        exitwhen(i == 16)
        call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_PICKUP_ITEM, null)
        set i=i+1
    call TriggerAddCondition(t, Filter(function onItemPickupFilter))
    call TriggerAddAction(t, function onItemPickup)


The attached map shows you how to use it. Its pretty easy, all you have to do is define global variables and the system will go off those.


  • ItemStackingLib.w3x
    18.6 KB · Views: 48