System Bag System

Reaction score
341
Bag System

This is a little system I made that stores items, inside an item, also known as a bag.

I made this bag system because I have only really seen extended inventorys to heroes.

This system stores all the items into a bag, so if you drop that bag, and someone else picks it up, the items will still remain in the bag.

Features

  • MUI (or MII, Multi Item instancability :p)
  • Stores items depending on the bag, not the hero.
  • Stores the items UserData (Custom Value).
  • Simple Usage, by only calling one function.
  • You can store bags, inside bags!
  • Saves the items position in the inventory.
  • Stores item charges!

Code!

It's pretty simple, i'm sure theres a flaw in it, so comments and suggestions welcome!

JASS:
//***************************************************************************
//*
//*  Bag System - By TriggerHappy187
//*
//***************************************************************************
//*
//*  Installation
//*     Simply copy the BagSys trigger into your map.
//*     You can optionally copy the demo triggers, if they suite you.
//*     This system also requires JassHelper (included with JNGP)
//*     Also, copy the CloseBag and Bag items into your map, and edit the BagSys globals to fit the raw ID
//***************************************************************************
//*
//*  Documentation
//*     This is a little system I made that stores items, inside an item, also known as a bag.
//*  
//*     made this bag system because I have only really seen extended inventorys to heroes.
//*  
//*     This system stores all the items into a bag, so if you drop that bag, and someone else picks it up,
//*     the items will still remain in the bag.
//*
//***************************************************************************
//*
//*  The Functions
//*     They are self explanitory in my opinion.
//*  
//*     OpenBag opens the bag for the current person holding it.
//*     
//*     CloseBag closes the currently open bag for the owner
//*
//***************************************************************************

library BagSys
    
    globals
        private constant integer BAG = 'I000'
        private constant integer CLOSE_BAG = 'I001'
    endglobals
    
    private struct bag
    
        integer array items[7]
        integer array UserData[7]
        integer array bagitems[7]
        integer array Charges[7]
        boolean isOpen = false
        
        static method create takes item whichBag returns bag
            local bag b = bag.allocate()
            call SetItemUserData(whichBag, b)
            return b
        endmethod
        
    endstruct
    
    function CloseBag takes unit owner, item whichBag returns boolean
        local bag b = GetItemUserData(whichBag)
        local integer i = 0
        local item itm

        if b.isOpen == false then
            debug call BJDebugMsg(GetItemName(whichBag) + " is not open!")
            return false
        endif
        
        set b.isOpen = false
        call RemoveItem(UnitItemInSlot(owner, 5))

        loop
            exitwhen i == 7
            set b.bagitems<i> = GetItemTypeId(UnitItemInSlot(owner, i))
            set itm = CreateItem(b.items<i>, 0, 0)
            call SetItemUserData(itm, b.UserData<i>)
            call SetItemCharges(itm, b.Charges<i>)
            set b.UserData<i> = GetItemUserData(UnitItemInSlot(owner, i))
            set b.Charges<i> = GetItemCharges(UnitItemInSlot(owner, i))
            call RemoveItem(UnitItemInSlot(owner, i))
            call UnitAddItem(owner, itm)
            call UnitDropItemSlot(owner, itm, i)
            set i = i + 1
        endloop

        set itm = null
        return true
    endfunction
    
    function OpenBag takes unit owner, item whichBag returns boolean
        local bag b = GetItemUserData(whichBag)
        local integer i = 0
        local item itm
        
        if b.isOpen == true then
            debug call BJDebugMsg(GetItemName(whichBag) + &quot; is already open!&quot;)
            return false
        elseif GetItemTypeId(UnitItemInSlot(owner, 5)) == CLOSE_BAG then
            debug call BJDebugMsg(&quot;You cannot open two bags at once&quot;)
            return false
        endif
        
        loop
            exitwhen i == 6
            set b.items<i> = GetItemTypeId(UnitItemInSlot(owner, i))
            set itm = CreateItem(b.bagitems<i>, 0, 0)
            call SetItemUserData(itm, b.UserData<i>)
            call SetItemCharges(itm, b.Charges<i>)
            set b.UserData<i> = GetItemUserData(UnitItemInSlot(owner, i))
            set b.Charges<i> = GetItemCharges(UnitItemInSlot(owner, i))
            call RemoveItem(UnitItemInSlot(owner, i))
            call UnitAddItem(owner, itm)
            call UnitDropItemSlot(owner, itm, i)
            set i = i + 1
        endloop

        set b.isOpen = true
        set itm = CreateItem(CLOSE_BAG, 0, 0)   
        call SetItemUserData(itm, b)
        call UnitAddItem(owner, itm)
        call UnitDropItemSlot(owner, itm, 5)
        
        set itm = null
        return true
    endfunction
    
    function ToggleBag takes unit owner, item whichBag returns nothing
        if bag(GetItemUserData(whichBag)).isOpen == true then
            call CloseBag(owner, whichBag)
        else
            call OpenBag(owner, whichBag)
        endif
    endfunction
    
    function CreateBag takes item whichBag returns nothing
        call bag.create(whichBag)
    endfunction
    
    
endlibrary
</i></i></i></i></i></i></i></i></i></i></i></i>


For all you gui'ers, don't be afraid, try the demo map and you will see how easy it is to use.
 

Attachments

  • BagSys.w3x
    17.9 KB · Views: 315
  • bagsys.JPG
    bagsys.JPG
    5.9 KB · Views: 270

Azlier

Old World Ghost
Reaction score
461
Hah, that snippet was just for when I was learning structs. It didn't even use a proper create method. This is different.
 

Flare

Stops copies me!
Reaction score
662
Tag changed to [System]

JASS:
        integer array items[7]
        integer array UserData[7]
        integer array bagitems[7]
        integer array Charges[7]

Why is the array size 7? To my knowledge, item slots work from 0 to 5 or, at least, they did when I wrote this:
JASS:
//Yes, it works, don&#039;t even think about asking <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue    :p" loading="lazy" data-shortname=":p" />
globals
    integer array ItemIds[2][6]
    integer array ItemCharges[2][6]
endglobals

function SwapItems takes unit first, unit second returns nothing
    local integer i = 0
    local item itm
    loop
    exitwhen i == 6
        set itm = UnitItemInSlot (first, i)
        set ItemIds[0]<i> = GetItemTypeId (itm)
        set ItemCharges[0]<i> = GetItemCharges (itm)
        call RemoveItem (itm)
        set itm = UnitItemInSlot (second, i)
        set ItemIds[1]<i> = GetItemTypeId (itm)
        set ItemCharges[1]<i> = GetItemCharges (itm)
        call RemoveItem (itm)
        set i = i + 1
    endloop
    set i = 0
    loop
    exitwhen i == 6
        set itm = UnitAddItemById (first, ItemIds[1]<i>)
        call SetItemCharges (itm, ItemCharges[1]<i>)
        set itm = UnitAddItemById (second, ItemIds[0]<i>)
        call SetItemCharges (itm, ItemCharges[0]<i>)
        set i = i + 1
    endloop
    set itm = null
endfunction

</i></i></i></i></i></i></i></i>

Seems silly to have an extra, unused array slot where it's not needed (since, as it is, you're acquiring data for 7 items (0, 1, 2, 3, 4, 5, 6 - there be 7 numbers thar), and I'm sure you know that standard inventories don't have 7 slots)
 
Reaction score
341
Did you reduce the 'exitwhen i == 7' to i == 6 as well? Not sure if that's the problem, or if it'd solve it, but it's worth a shot if it's not already done. Might take a look at things myself later, I'm tired right now :(

I'm pretty sure I tried that, I will take a second and maybe third look :thup:

EDIT: alright, it works, don't know how the bug occured before.

I will update it once I add a dragNdrop demo.
 

Tooblet

Active Member
Reaction score
6
heyooooo

I love this system, but I can't get it to be MUI(MII)..

It works like a charm, I create the bag item when I select a hero in my map, and then give it to the hero after setting variable and calling CreateBag(udg_variable).

But when I play my map multiplayer and we all choose heroes, we share the same bag.
when one person opens the bag, others cannot open "their" bags.
and when anyone puts an item in his bag, it comes to the same bag.
It's like everyone has a bagitem that leads to the same bag :p

any idéa why this occurs? I can swear I've done everything right :/
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Would be nice if the items that are in a bag gives you the effect they normally, do. (I can understand, it would be such hard to do, but would be very enjoyable).
 

Tooblet

Active Member
Reaction score
6
well it's MII in the testmap, But I can't seem to get it goin right.
any solutions or changes I should make?
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top