Hotbar Making new stack when one is filled

I am working on an invintory system using arrays for: which Ids are in a spot in the hotbar and how many items are in each slot.

Here is my current code:

Invetory20.prototype.addItem = function (id) {
    var checkSame = hotbarArray.findIndex(item => item === id);
    var fullStack = globalMaxStacks[checkSame] < hotbarPerSlot[checkSame]
   // alert('globalMaxStacks : ' + globalMaxStacks[checkSame])
    //alert('hotbarPerSlot : ' + hotbarPerSlot[checkSame])
    //alert('fullstack: ' + fullStack)


    if (((checkSame === -1) && (fullStack === false)) || ((checkSame >= 0) && fullStack === true)) {
        console.log('newStack' + id + fullStack + checkSame)
        var itemIndex = hotbarArray.findIndex(item => item === 'blank');
        hotbarArray[itemIndex] = id;
        hotbarPerSlot[itemIndex]++;
    }
    if ((checkSame > -1) && (fullStack === false)) {
        console.log('addToStack' + id + fullStack + checkSame)
        var itemIndex = hotbarArray.findIndex(item => item === id);
        hotbarPerSlot[itemIndex]++;
    }

};

For some reason, even if the maxStack for checksame is greater than perslot for checksame, it just does lines 15-20.

I can supply any more info you might need.
Editor Scene: PlayCanvas | HTML5 Game Engine

Does anyone have any ideas on how to fix this?

Hi @Codeknight999!

Unfortunately I don’t understand your logic, so maybe you can explain that a bit more.

What are the results of line 16?

For lighter:addToStacklighterfalse0, for the R1: addToStackr1false1 for testCrop: addToStackcropTestfalse2

Doesn’t sound like an expected result to me? I expect a result of id, fullStack and checkSame. I guess this should be a value, a value and a boolean?

Not sure why checkSame is a value to be honest. What does it do exactly?

Checksame is supposed to check if there is already an item in the hotbar that has the same id.

id is just the id of the item that the script is trying to add to the hotbar.
Fullstack should return true or false.
Checksame should return either -1 (not found) or 0 or higher. (>=0)

Also here is a quick breakdown of my logic:

    var checkSame = hotbarArray.findIndex(item => item === id);
    var fullStack = globalMaxStacks[checkSame] < hotbarPerSlot[checkSame]

Values to check if there is a same ID in the hotbar already, and maxStack sees if that slot is full.

    if (((checkSame === -1) && (fullStack === false)) || ((checkSame >= 0) && fullStack === true)) {
        console.log('newStack' + id + fullStack + checkSame)
        var itemIndex = hotbarArray.findIndex(item => item === 'blank');
        hotbarArray[itemIndex] = id;
        hotbarPerSlot[itemIndex]++;
    }

Adds a new item stack if there is no id found already or and item is found but its stack is full

    if ((checkSame > -1) && (fullStack === false)) {
        alert('addToStack' + id + fullStack + checkSame)
        var itemIndex = hotbarArray.findIndex(item => item === id);
        hotbarPerSlot[itemIndex]++;
    }

If checksame found a slot and its stack isn’t full, just add to the existing item stack,

Alright, I tried this yesterday and for me it seems to work as expected. Could you clarify the exact issue and how I can reproduce this in your scene? I’m also curious when and where fullStack is set to true.

I probably should’ve mentioned this earlier—sorry about that. In the root, there’s a script called itemDatabase with three attributes: id, texture, and maxStack. They’re arrays, and each item corresponds to an object number. For example, if index 0 in the id array is blank, then index 0 in the other arrays (like texture and maxStack) also corresponds to the blank item.

These arrays are turned into global variables, including maxStack. When the script tries to add an item, it checks if the item’s id is already in the hotbar array. If it is, the script checks how many items are in the current stack. If that number is equal to or greater than the item’s maxStack value, it should create a new stack.

Your logic is quite complicated, so it’s hard for others to understand.

From what I understand, your current problem is that the items are stacked while this is not allowed? What do you expect to happen instead?

Looking at the results of your log, fullStack is in all cases false, so it looks like this is not set to true correctly? Did you check if line 3 of the code in your first post works as expected?

Honestly, I think I am just going to rework it so it isn’t as complicated and so it works better.

Thanks for trying to help though!

1 Like

This functionality now works. I need to work on an actual inventory besides the hotbar, then ill release it to the community.
Working editor link:
PlayCanvas | HTML5 Game Engine