GML Logic Help, the inventory is only going up once per item

aristhemage

Member
Hi all I am making a sandbox game with an inventory with 9 slots, when picking up an item it should look for an empty slot, or a slot where it has the same item.
What it is doing is updating the inventory with the item, and unique items go into the next slot with nothing in it like it should, but when a item type is in the inventory, any other picked up items of the same type just disappear without updating anything.

Variables:
global.inventory = [0, 0, -1, block_air, 0, 0, -1, block_air, 0, 0, -1, block_air, 0, 0, -1, block_air, 0, 0, -1, block_air, 0, 0, -1, block_air, 0, 0, -1, block_air, 0, 0, -1, block_air, 0, 0, -1, block_air];
global.idee = when player touches and item, it changes the id (idee because I cant do global.id) to the block, this works.

Here are some code bits to help
Player step
GML:
if place_meeting(x,y,par_item){   
    while(global.inventory[global.invCheck] != global.idee){
if global.inventory[global.invCheck] = 0 || global.inventory[global.invCheck] = global.idee{
global.inventory[global.invCheck] = global.idee; global.inventory[global.invCheck + 1] += 1;}
else{
global.invCheck += 4
}}
}
    if global.invCheck = 36{
    global.invCheck = 0;   
    }
When an item touches the player, this one is a grass block. (Yes, this is bootleg Minecraft, more for me than the public)
the item error timer is because it changes the amount of items you have from 0 to 2 the first time, and then +1 like it should the rest of the time, not the issue I want to talk about though. It should not affect this issue
GML:
if place_meeting(x,y,obj_player){
    global.invCheck = 0;   
global.idee = 1
if itemErrorTimer = 0{
instance_destroy();
itemErrorTimer = 1;
}else{
itemErrorTimer--;   
}
}
Any help would be great, ill be in the comments to answer questions
 

TailBit

Member
Your while loop pretty much says .. if this item isn't in the slot, continue the loop .. so if you picked up a item with idee 1, then you will not be able to add more.

Instead loop through all slots and use break to end the loop after the amount have been added
GML:
if place_meeting(x,y,par_item){
  
    var i = 0;
    while(i<36){
      
        if global.inventory[i] = 0 || global.inventory[i] = global.idee{
            global.inventory[i] = global.idee;
            global.inventory[i + 1] += 1;
            break;
        }
      
        i+= 4
    }

}
there would also be problems if you global.idee gets set after the player have done its code .. check the value of it before adding it to the inventory, and after it have been added, then turn it off again..

I would also do 2 loops, first one to find existing slots .. and if found then move over the global.amount if you make a value like that
If global.amount still isn't empty, then do a loop to fill the remaining in empty slots


You could also get the instance id of the item from the player, and have the idee stored as a variable in the item, to avoid the items needing any collision event:
GML:
var ins = instance_place(x,y,par_item);

if ins != noone {
   
    var i = 0;
    while(i<36){
       
        if global.inventory[i] == 0 || global.inventory[i] == ins.idee{
            global.inventory[i] = ins.idee;
            global.inventory[i + 1] += 1;
            break;
        }
       
        global.invCheck += 4
    }
   
    // if you don't want to write ins. everywhere .. then use with(ins){ around it instead }
    if ins.itemErrorTimer-- == 0{
        instance_destroy(ins);
        ins.itemErrorTimer = 1;
    }
}
 
Last edited:

aristhemage

Member
Your while loop pretty much says .. if this item isn't in the slot, continue the loop .. so if you picked up a item with idee 1, then you will not be able to add more.

Instead loop through all slots and use break to end the loop after the amount have been added
GML:
if place_meeting(x,y,par_item){
 
    var i = 0;
    while(i<36){
     
        if global.inventory[i] = 0 || global.inventory[i] = global.idee{
            global.inventory[i] = global.idee;
            global.inventory[i + 1] += 1;
            break;
        }
     
        i+= 4
    }

}
there would also be problems if you global.idee gets set after the player have done its code .. check the value of it before adding it to the inventory, and after it have been added, then turn it off again..

I would also do 2 loops, first one to find existing slots .. and if found then move over the global.amount if you make a value like that
If global.amount still isn't empty, then do a loop to fill the remaining in empty slots


You could also get the instance id of the item from the player, and have the idee stored as a variable in the item, to avoid the items needing any collision event:
GML:
var ins = instance_place(x,y,par_item);

if ins != noone {
  
    var i = 0;
    while(i<36){
      
        if global.inventory[i] == 0 || global.inventory[i] == ins.idee{
            global.inventory[i] = ins.idee;
            global.inventory[i + 1] += 1;
            break;
        }
      
        global.invCheck += 4
    }
  
    // if you don't want to write ins. everywhere .. then use with(ins){ around it instead }
    if ins.itemErrorTimer-- == 0{
        instance_destroy(ins);
        ins.itemErrorTimer = 1;
    }
}
Thanks! Ill mess around a bit with this, this seems to sort of work, so ill use it!
 
Top