x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

alt text

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.

alt text

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 :'(

alt text

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.

Product Version: UE 4.21
Tags:
more ▼

asked Apr 24 '19 at 09:02 PM in Blueprint Scripting

avatar image

bathorysthroat
15 4 5 11

avatar image Nebula Games Inc Apr 24 '19 at 10:55 PM

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.

https://www.youtube.com/playlist?list=PLyu-W38DvZhqOdaCkB4hGWYQzuKH2gALZ

avatar image bathorysthroat Apr 25 '19 at 03:56 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

There are nodes 'max of int array', 'min of int array'...

more ▼

answered Apr 25 '19 at 05:00 PM

avatar image

ClockworkOcean
210 3 4 4

avatar image bathorysthroat Apr 26 '19 at 10:40 PM

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

alt text

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?

minintarray.png (88.7 kB)
avatar image ClockworkOcean Apr 27 '19 at 10:21 AM

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.

avatar image ClockworkOcean Apr 27 '19 at 10:26 AM

alt text

bp.jpg (77.2 kB)
avatar image ClockworkOcean Apr 27 '19 at 10:27 AM

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

avatar image ClockworkOcean Apr 27 '19 at 10:45 AM

Like this:alt text

( haven't even compiled that, but you get the idea... )

bp2.jpg (150.0 kB)
avatar image bathorysthroat Apr 28 '19 at 01:53 AM

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.

avatar image ClockworkOcean Apr 28 '19 at 08:19 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question