• 🏆 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!

Item Stacking

Status
Not open for further replies.
Level 20
Joined
Jul 6, 2009
Messages
1,885
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
Joined
Nov 22, 2009
Messages
181
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
Joined
Jan 21, 2006
Messages
2,552
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)

Code:
if  (both charges  "is greater than"  100) then
        set item charges A = item charges A - (100 - item charges B)
        set item charges B = 100
else
        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
Joined
Jan 21, 2006
Messages
2,552
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
Joined
Jan 21, 2006
Messages
2,552
I made this but it seems you solved your problem. Here you go anyway:

JASS:
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
    endif
    
    loop
        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
        endif
        set i=i+1
    endloop

    return false
endfunction


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
    loop
        exitwhen(i==size)
        
        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)
            else
                set bCharges = aCharges+bCharges
                // if the item transfers all of its charges, then it can be removed.
                call RemoveItem(GetManipulatedItem())
            endif
            // set the charges of the item in the slot
            call SetItemCharges(inventory, bCharges)
        endif
        set i=i+1
    endloop
           
    set trig        = null
    set pickedup    = null
    set inventory   = null

endfunction


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

endlibrary

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.
 

Attachments

  • ItemStackingLib.w3x
    18.6 KB · Views: 51
Status
Not open for further replies.
Top