How to find the index inside an array which contains min value?

Hi, is there a way to find the index that has the min value inside an Int array?

I have built an inventory system much like Resident Evil 2 (remake). Where there is a grid of inventory slots which hold items. I would then use a reload key (InventoryCheck) to remove 1 from the quantity of the slot.

275105-inventoryscreen.png

I then set up a For each loop with break that will scan through all the items inside the inventory. Then an enumeration of Ammo types checks if any of the items match the ammo of the currently equipped weapon.

For now, I have all the (Array Index) which match the currently equipped ammo condition being added to an array.
I then was trying to find the Max Int inside the array and remove (-1) from that slots quantity amount… I then use that Int as the Slot Index to change plugged into the “get (a copy)”. This way the ammo amount is only taken from the inventory slot with the specified index number. For example: Index 0 would take ammo from Slot 1, Index 1 would take from Slot 2 and so on… But this gives an unwanted result :’(

This was meant to find the last slot containing ammo inside the inventory and remove from that. But when I press the reload key (InventoryCheck) it takes 1 away from the first ammo bow, then takes away 1 from the second ammo box each time it is pressed after that.

So what I really need to do here is somehow take all of the for loop ArrayIndex found that match the Equppied ammo type and add them to an array. I then will have to set each Array Elements value to that of the Slots quantity (Ammo Amount). Then out of that array of index (Slot Index), I need to find which on of those has the min value.

So then when I press the Reload Key, I want it to scan the inventory and if It finds ammo boxes that match the currently equipped weapon. Out of those slots take away 1 from the slot index that has the lowest amount of ammo or in this case the Index number which contains the smallest value.

Any help with this would be great. I really don’t know how it could be done myself. I hope this makes sense, sorry if it’s a bit long winded. It was the best way I could think of explaining the problem.

Thanks, everyone.

Don’t know if this will help you or not but the first video in the link will allow you to create a node that will sort an array of integers for you. You can sort high to low or low to high. So in your case, sort low to high and take the “0” index of the sorted array. That will be the index with the lowest amount.

Thank you! This is very cool but not quite what I need. You see in my second image on the ForLoopWithBreak, “Array Index” represents the slot numbers of each inventory slot which contains matching ammo. “The Array Element Quantity” represents the amount of ammo stored in each slot. So I understand that the node you had me make sorts the array. But how would I go about finding the exact “Array Index” for lowest "The Array Element Quantity?

Thanks again

There are nodes ‘max of int array’, ‘min of int array’…

Thanks, ok that’s what I’m doing now.

The thing is if I have an ammo box with a quantity of 5 in the first slot and an ammo box with a quantity of 3 in the second slot it takes 1 away from the slot containing the lowest quantity which is great!

BUT if I have a box of 3 in the first slot and a box of 5 in the second slot, it will take 1 from the lowest quantity and once that slot (with a quantity of 3) is empty. the next time I loop it doesn’t then take away from the slot with the next lowest quantity (with the quantity of 5) because the index of the “Slot To Take From” doesn’t get updated for some odd reason. Do you have any ideas?

Your problem then is that the min of the array is zero, the slot with no ammo… you need to take the zero slot out.

… something like that…otherwise you have to write your own ‘lowest non zero index’ function.

Like this:

( haven’t even compiled that, but you get the idea… )

Thanks, I thought one of these tricks might just get but it’s acting strange.
I’m using a Forloopwithbreak to scan my inventory then ADD all the quantities found to an Array. But then when I go from there to the WhileLoop the game crashes, I’m guessing I can’t use a WhileLoop while I’m using a ForLoopWithBreak… If I try to replace the WhileLoop with a Branch I have to hit the reload button twice before it takes any amount away from the second slot. I suppose because the branch is removing the 0 slot on the first click then -1 on the next click.

Alo the ‘lowest non zero index’ function. That will remove from the first slot properly but after that is empty it doesn’t start to subtract for the next slot.

I like what you have here, it really seems like it should work but I don’t know. Sorry for the frustration.

Keep going buddy, you’ll get there. Just take it completely apart and put it back together again, it will happen…