Why are we changing the "AddItemToSlot" function?

Hello,

In the end of the video, Sam added this bit of code to the AddItemToSlot function :

var i = FindStack(item);

if (i >= 0){
 slot = i;
}

I don’t understand why are we adding this, since if the slot is not empty we’re already calling AddToFirstEmptySlot which already takes care of finding a stack for the item.
And if the slot is empty well just add it to that empty slot.

I don’t understand why this logic is not enough and why do we need to add that bit of code?

Thank you :smiley:

    /// <summary>
    /// Will add an item to the given slot if possible. If there is already
    /// a stack of this type, it will add to the existing stack. Otherwise,
    /// it will be added to the first empty slot.

So the idea behind this method is for handling situations where you are dragging from one container to another container. You’ve released on a specific slot, but there is already a stack on another. The code you’ve highlighted will notice this and put the item in the stack if one exists.

AddToFirstEmptySlot is for situations like picking up or unequipping an item, where the slot is not specified.

1 Like

Blockquote You’ve released on a specific slot, but there is already a stack on another. The code you’ve highlighted will notice this and put the item in the stack if one exists.

I kind of understood this when watching the video but wasn’t sure, thank you for making it clear for me. I fully understand now what it’s doing.

The thing is I’m not sure this is an ideal behavior in a game in my opinion.

first, how would it be possible to have two separate stacks of the same item whilst our system is built in a way that this situation will never happen.

Then, if we take an example where is can of scenario can happen, the resident evil kind of inventory, they have a space management inventory system where it is possible to have to stacks of the same item. though if you move one of the stacks around, it won’t go looking for the stack that is not in the slot you’ve just released on, but rather put in exactly where you wanted to put it, and if you put one stack on top of the other, only then it will be merged under one big stack.

Here is an image from Deus Ex game which is similar to resident evil, illustrating the inventory management I’m referring to :

deus-ex-human-revolution-inventory

Of course each game can have it’s own mechanics, but I can’t see an example where the player would be forced to combine two stacks when he clearly is just moving one of the stacks to an empty slot rather then on top of the second stack, where in my opinion is the ideal scenario for this behavior.

Just sharing my idea here and would love to hear your thoughts :smiley:

Thanks again for answering my question :smiley:

1 Like

That is actually a very valid design choice! (Quite obviously because I’ve seen it in successful games). You can easily change it to this type of behavior by commenting out the FindStack logic

// var i = FindStack(item);
// if(I>=0)
// {
//     slot=i;
//  }
1 Like

Yes indeed. Thank you for taking the time to discuss this. Have a great day :grin:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms