Help with inventory system

goamalo

Member
Hello everybody! I have made an inventory system with item count and item count limit.

My problem is that is the max item count is 3 and I have 2 in the inventory, when I add 5 of the item; the thing that happen is:
slot 1 will be 3.
slot 2 will have 1.

it does not add the right amount of the item.

Inventory code:
Code:
    if (global.inventory[i] = argument0) //Check if item exists in slot
    {
        max_stack = Item_Max_Stock(argument0);
        if (max_stack > global.item_count[i]) //Check for item limit
        {
            if (max_stack < global.item_count[i]+argument1) //If the count + pickup count is bigger then max
            {
                last_count = abs(max_stack-global.item_count[i]);
                global.item_count[i] = max_stack; //Set the count to max
                Item_Pickup(argument0,last_count);
                return(1);
            }
            else //If the count + pickup count is lower then max
            {
                global.item_count[i] += argument1; //Add to count
                return(1);
            }
        }
    }
Inside the code I call the script again but with the new count:
Item_Pickup(argument0,last_count);

the script don't loop itself enough times to give the right amount of the item.
 

YoSniper

Member
Here's an idea, although its application is limited. If an inventory slot is overloaded, the excess inventory will be pushed to the next slot.
Code:
///add_inventory(item, number)

//First, find the first available slot for the item.
var current_slot, num_left, max_stack;
current_slot = 0;
num_left = argument1;

while current_slot < MAX_SLOTS and num_left > 0 { //MAX_SLOTS would have to be pre-defined
    if global.inventory[current_slot] != argument0 and global.inventory[current_slot] != noone {
        //If this slot is currently occupied by a different item, move to the next one.
        current_slot += 1;
        continue; //This keyword tells the program to go back to the start of the loop.
    }
    //If the code reaches this far, we have found an available slot.
    global.inventory[current_slot] = argument0; //In case the slot was empty before
    global.item_count[current_slot] += num_left;
    num_left = 0;
    while global.item_count[current_slot] > Item_Max_Stock(argument0) {
        global.item_count[current_slot] -= 1;
        num_left += 1;
    }
    current_slot += 1;
}
This may not be the most efficient way to do this, but I expect it would be accurate.
 

Azenris

Member
This is my adding of items. I never cared for super efficient since its used infrequently.

Basically I check first if there is even enough room. If not I just cancel out.

If there is room, I use a while loop, to continue adding the item until all the items are added.
So you can see the while loops, just adding what it can, reducing that amount and repeating until it gets to 0 ( which i know it can because of previous check )
It only earlies out if there isn't room when using all_or_none. So the while also checks for when its full ( for when it does 'add as many as you can' ).

Code:
/// @description Add an item to the container
/// @param   {ds_list}  container
/// @param   {ITEM}    item_id
/// @param   {int}        quantity
/// @param   {bool}     [all_or_none]
/// @return   {int}         quantity_added

var _container =       argument[ 0 ];
var _items =           _container[| CONTAINER_INFO.ITEMS ];
var _item_id =           argument[ 1 ];
var _item =               item_get( _item_id );
var _quantity =           argument[ 2 ];
var _quantity_attempt = _quantity;
var _container_max =   _container[| CONTAINER_INFO.MAX_STACK ];
var _max_stack =       ( _container_max == -1 ? _item[| ITEM_INFO.MAX_STACK ] : _container_max );
var _container_size =   container_get_size( _container );
var _has_stack =       false;
var _all_or_none =       ( argument_count > 3 ? argument[ 3 ] : false );

#region check container filter

if ( ( _container[| CONTAINER_INFO.FILTER ] & _item[| ITEM_INFO.TYPE ] ) == 0 )
   return 0;

#endregion

if ( _all_or_none and container_has_room_for_item( _container, _item_id, _quantity ) == false )
   return 0;

var _slot = 0;

#region try add it to other stacks, ( if it is stackable )

if ( _max_stack > 1 )
{
   while ( _quantity > 0 and _slot < _container_size )
   {
       var _entry = _items[| _slot ];

       if ( _entry != noone and _entry[| CONTAINER_ITEM_INFO.ITEM_ID ] == _item_id )
       {
           _has_stack = true;

           var _current_quan =   _entry[| CONTAINER_ITEM_INFO.QUANTITY ];
           var _available =          _max_stack - _current_quan;

           if ( _available > 0 )
           {
               var _to_add = min( _quantity, _available );

               _entry[| CONTAINER_ITEM_INFO.QUANTITY ] += _to_add;

               _quantity -= _to_add;
           }
       }
     
       ++_slot;
   }
}

#endregion

#region check if more stacks need creating ( needs to be the first stack, or allow multi-stacking )

var _con_full = false;

while ( _quantity > 0 and _con_full == false and ( _has_stack == false or ( _has_stack and _item[| ITEM_INFO.MULTI_STACK ] ) ) )
{
   var _slot = container_get_free_slot( _container );

   if ( _slot != noone )
   {
       var _to_add = min( _quantity, _max_stack );

       container_set_item( _container, _slot, _item_id, _to_add );

       _quantity -= _to_add;

       _has_stack = true;
   }
   else
       _con_full = true;
}

#endregion

return _quantity_attempt - _quantity;
 
Last edited:
Top