GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 GML Code Help » Inventory Errors » 2015-01-27 14:02:55

Kronosaurus
Replies: 1

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,0] = "Breaaad";
items[3,1] = true;
items[3,2] = -1;
items[3,3] = "a bread like foooood thing";


///adding items to the inventory
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)

Board footer

Powered by FluxBB