help with a item slot system

hello i wanted to know how do i do a item slot system that works like this:

- the slot is a single object
- a for loop is done to create the slots
- every slot has a separate item on creation


(i already done the part for creation the slots but i wanted to know how do i put a item on each slot)
 

Jakylgamer

Member
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
 

TailBit

Member
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:
GML:
with(obj_inv_slot){
    if(amount == 0){
        ins.amount = 1;
        ins.name = "Sword";
        break;
    }
}
I would make this into some sort of script

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
}
 
Top