look into structs and arrays
structs are easy ways to store/retrieve data
and also look into inventory systems plenty of tutorials on youtube to follow
Well, just to answer your question, you use a loop here as well.
If amount == 0 then add item into the slot and end the loop
Do you store the slots in an array or something so you have a way to separate what is a inventory slot and what is a shop slot? Could make different child objects, but reading from the list directly would be far more effective.
GML:
var i,ins;
for(i=0;i<instance_number(obj_inv_slot);i++){
ins = instance_find(obj_inv_slot,i);
if(ins.amount == 0){
ins.amount = 1;
ins.name = "Sword";
break; // found a empty slot, so no need to search further
}
}
would use "i<array_length(inv)" and "ins = inv;" with arrays
With instances you could also use a with loop, which would be more effective then instance_find:
But when you get further you might want to improve your loop into:
Code:
if this item can stack{
find a existing slot of this item
if found then add as much as the slot can hold
if some amount left to place then keep searching, if not .. break
}
if any amount left to place{
find empty slots
add as much as the slot can carry
if some amount left to place then keep searching, if not .. break
}