• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [SOLVED] Help with inventory system

goamalo

Member
Hello everyone!
I want to build an inventory system with stacking limit.
My problem is that when the added item has a greater count than the limit it is not adding it correctly in the next available slot.

I tried everything that I could but no luck.

Here is my code:
Code:
for (i = 0; i < MAX_SLOTS; i += 1)
{
    //===================
    //    Add To New Slot
    //===================
    if (o_Inv_Ctrl.inv_slot[i].item = 0)
    {
        o_Inv_Ctrl.inv_slot[i].item = argument0;
        o_Inv_Ctrl.inv_slot[i].item_count += argument1;
        o_Inv_Ctrl.max_items_check += 1;
        
        max_stack = Item_Max_Stock(argument0);
        if (argument1 > max_stack)
        {
            var last_count = o_Inv_Ctrl.inv_slot[i].item_count-max_stack;
            o_Inv_Ctrl.inv_slot[i].item_count = max_stack;
            Item_Pickup(argument0,last_count);
            done1 = 1;
            show_message("yay")
        }
        return(1);
    }
    
    //================
    //    Add To Count
    //================
    if (o_Inv_Ctrl.inv_slot[i].item = argument0)
    {
        max_stack = o_Inv_Ctrl.inv_slot[i].max_count;
        if (max_stack > o_Inv_Ctrl.inv_slot[i].item_count)
        {
            if (argument1+o_Inv_Ctrl.inv_slot[i].item_count > max_stack)
            {
                var last_count = abs(argument1-o_Inv_Ctrl.inv_slot[i].item_count);
                o_Inv_Ctrl.inv_slot[i].item_count = max_stack;
                Item_Pickup(argument0,last_count+1);
                return(1);
            }
            else
            {
                o_Inv_Ctrl.inv_slot[i].item_count += argument1;
                return(1);
            }
        }
    }
}
return(0);
 

Fabseven

Member
hello i am using gm1.4 and dont understand your code.

First :
o_Inv_Ctrl.inv_slot.item ?!
o_Inv_Ctrl is an obj
while inv_slot is an array
but .item ?!
sounds weird to me.

=> Why donot you use 2d array or ds map ?

Second :
what's the meaning behind the first for loop ?
I donot understand why you loop in this case,

for me in the beginning you have an empty inventory, run a script inventory_init , this script will create and array or dsmap or whatever thing you want with empty value for each slot, also : define max size.
Then when you pick up an element , let's say a potion, you have to first search in your inventory if you already have some of theses potions => for loop to search (if array)
if item founded, in this position, search for max stack and qte already in inventory, you may add some potions in the founded slot, if max stack is reached => add items to a new slot (search for a free slot with loop)
if item is not founded => add items to a new slot.

Did you do something like this ?
 
A

Annoyed Grunt

Guest
Could you please explain what the current issue is? What do you mean "it is not added correctly"? I've traced your code and at a first glance it looks like it should do what it does, it would be easier to find the mistake if we know what the glitch is, exactly.
 

goamalo

Member
hello i am using gm1.4 and dont understand your code.

First :
o_Inv_Ctrl.inv_slot.item ?!
o_Inv_Ctrl is an obj
while inv_slot is an array
but .item ?!
sounds weird to me.

=> Why donot you use 2d array or ds map ?

Second :
what's the meaning behind the first for loop ?
I donot understand why you loop in this case,

for me in the beginning you have an empty inventory, run a script inventory_init , this script will create and array or dsmap or whatever thing you want with empty value for each slot, also : define max size.
Then when you pick up an element , let's say a potion, you have to first search in your inventory if you already have some of theses potions => for loop to search (if array)
if item founded, in this position, search for max stack and qte already in inventory, you may add some potions in the founded slot, if max stack is reached => add items to a new slot (search for a free slot with loop)
if item is not founded => add items to a new slot.

Did you do something like this ?
I'm not so good at coding. I followed 2 tutorials to get this far.
I tried to do what you said but I get the same result.

Could you please explain what the current issue is? What do you mean "it is not added correctly"? I've traced your code and at a first glance it looks like it should do what it does, it would be easier to find the mistake if we know what the glitch is, exactly.
Lets say that the item limit is 3.
I add 4 to the count everytime that I click a button.

the first time you add the items the inventory looks like this:
3 | 1

when I do it again it should look like this:
3 | 3 | 2

but instead it looks like this:
3 | 3 | 3 | 1

it gives me 10 items instead of 8.
I tried everything that I could think of but no luck.
 

goamalo

Member
I did some testing and I found that when I use low numbers it is working correctly.

the problem begins when I use large numbers and the script needs to fill more then 1 free slot.

like if the limit is 4 and you give it 11 the script runs several times in order to spread the given number among the slots and I think that it multiplay it and give wrong numbers.

This script is called "Item_Pickup" and inside the script I call it again and giving it the remaining count that it needs to add.
Item_Pickup(argument0,last_count);

this is where the problem begins. I did it that way because I don't know how to make it check the script again.
 

goamalo

Member
I finally found the solution. it only took me a few months XD
the problem was on this line:
var last_count = abs(argument1-o_Inv_Ctrl.inv_slot.item_count);

changed it to:
var last_count = abs(argument1+o_Inv_Ctrl.inv_slot.item_count-max_stack);

and now it is working.
 

Fabseven

Member
I finally found the solution. it only took me a few months XD
the problem was on this line:
var last_count = abs(argument1-o_Inv_Ctrl.inv_slot.item_count);

changed it to:
var last_count = abs(argument1+o_Inv_Ctrl.inv_slot.item_count-max_stack);

and now it is working.
great !
 
Top