# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #1 2015-01-27 14:02:55

Kronosaurus
Member
Registered: 2015-01-27
Posts: 1

### Inventory Errors

Hi everyone! I've been working on a survival game for the past few weeks but I have a problem. When an object is in the players inventory, even after it is used, it doesn't go away. This results in a broken game where the player can survive with one piece of meat. Every comment is a new script other than the first one.

//Inventory Creation

//[<item ID>, <number>, <health>]
globalvar invSlots;
invSlots = 15;
globalvar equip;
equip = -1;

globalvar inv;
for(i=0;i<invSlots;i+=1){
inv[i,0] = -1;
}

///Item Database Creation\\\
globalvar maxStack
global.maxStack = 10
//[<name>, <stackable?>, <maxpower>, <description>]
globalvar items;
items[0,0] = "Food";
items[0,1] = true;
items[0,2] = -1;
items[0,3] = "A red meat, tasty"

items[1,0] = "Stick with a Stone";
items[1,1] = false;
items[1,2] = 100;
items[1,3] = "A piece of wood with a stick attached"
items[1,4] = 1

items[2,0] = "Seeds";
items[2,1] = true;
items[2,2] = -1;
items[2,3] = "Seeds";

items[3,1] = true;
items[3,2] = -1;
items[3,3] = "a bread like foooood thing";

itemId = argument0
num = argument1
hlth = argument2
for(i=0;i<global.invSlots&&num>0;i+=1){
if(global.items[itemId,1]&&global.inv[i,0]==itemId&&global.inv[i,0]<global.maxStack){
if(global.inv[i,1]+num<=global.maxStack){
global.inv[i,1]+=num
num = 0
}else{
num-=global.maxStack-inv[i,1]
global.inv[i,1] = global.maxStack
}
}
if(inv[i,0]==-1){
global.inv[i,0]=itemId
if(num<=global.maxStack){
global.inv[i,1]=num
num = 0
}else{
num-=global.maxStack
global.inv[i,1] = global.maxStack
}
global.inv[i,2]=hlth
num=0
}
}

///equipping objects
if(equip != -1){
switch global.inv[global.equip,0]{
case 1:
instance_change(obj_playerwithstick,true)
break;
case 0:
instance_change(obj_playerwithmeat,true)

And finally, the script in question:
//Remove Items
var numToRemove = argument0;
var idToRemove = argument1;
sprite_index = spr_seeds
for(i=0;i<global.invSlots;i+=1){
if(global.inv[i,0]==argument1){
if(numToRemove < global.inv[i,1]){
global.inv[i,1] -= numToRemove;
return true;
}else if(numToRemove == global.inv[i,1]){
global.inv[i,0] = -1;
global.equip = -1;
return true;
}
else{
numToRemove -= global.inv[i,1];
global.inv[i,0] = -1;
global.equip = -1;
}
}
}
return false;
case 2:
instance_change(obj_playerwithseeds,true)
break;
case 3:
instance_change(obj_playerwithwood,true)
}
}

else instance_change(obj_player,true)

Offline

## #2 2015-01-28 21:55:39

xot
Registered: 2007-08-18
Posts: 1,201

### Re: Inventory Errors

Your code is a little bit hard to follow. If you could enclose your blocks of code inside [code][/code] tags, it would be a big help. I started to do this for you but I'm not sure what goes with what.

Also, near the end, you have function code in the middle of a switch statement. I don't know what happened there. Like I said, I didn't want to change anything because I didn't understand what you intended.

I saw what might be a couple of bugs but I don't think they are the bugs you are looking for. I can also give you some advice for making your code easier to debug but let's get it to a stage where I can read it first.

Abusing forum power since 1986.

Offline