thumbnail blueprint

Dynamic Equip Index Array Search

  • Exposure: public
  • UE Version: 5.2


April 19, 2024, 12:00 am

DEPENDS ON: Current Weapons - Weapon class array with empty or weapon-filled slots.

Equip index [INT] - index of Current Weapons array

Bidirectional input [INT, 1 or -1] - (I am using the mouse wheel, but the code can be repurposed for just a single direction input too.)

FirstKeySet? [Boolean, false]

Lowest Equip Index Difference [INT to INT map]

LEI Key Select [INT]

FUNCTION BREAKDOWN: First we clear the Lowest Equip Index Difference and set "LEI key select" to 0. This is so the function can be repeated.

It takes a positive or negative input value (in our case the mouse wheel up/down, which must be promoted to a variable after each input down, then this function can be called) which begins a for each loop on the weapon array.

In the positive branch, we filter the array results that are less or equal to our current weapon, also excluding hands and empty slots.

With those filtered results, we get the absolute difference of each result index compared to the current weapon index, so we know how close or distant they are.

This also gives us a guaranteed unique value for every valid array result, which we can use as the key for a map.

So for each item that makes it past the filters, we add it to the Lowest Equip Index Difference map using the difference from the current index as a key.

Once complete, we get the keys and run a for each loop on the array. Here we set the first item, then check if each subsequent item is smaller. This will give us the closest item to our current item.

If this result returns greater than 0; meaning we found valid items, then the shortest difference-- we go ahead and set the matched index variable using the key select. (Lowest equip index difference->find item @ LEI Key Select) and the function is complete.

If this key result returns zero, that means we have hit the top of the array. We could simply set the matched index to zero (hands) and call it a day here, but we don't want to switch to hands if we have picked up a weapon. We only want to switch between weapons, leaving unarmed for when we have zero items. So now we need to search for the closest valid item in the bottom of the array.

First we clear the lowest equip index difference array so we can use it again without error. Then we run a for each loop on all the items in the weapon array-- filtering out unarmed or empty once again. Then we find the absolute difference from 1 on each index, repeating the operation of using it as a key to store the results in a map.

Finally, this should give us the item closest to the bottom of the array, which is found using (Lowest equip index difference->find item @ LEI Key Select) then the result is set as the matched equip index.

For the negative pathway it works basically the same, expect excluding greater/equal values. I invite you to review the code if you've made it this far in the explanation.

I'm not sure if this is a super common setup for game inventories already, but it took a few brain cells so I thought I'd share.

Cheers everybody, I hope this was helpful!


*Print strings/appends are included for debugging!

Click the button above, it will automatically copy blueprint in your clipboard. Then in Unreal Engine blueprint editor, paste it with ctrl + v
April 19, 2024
  1. Updated comment with explanation of input array

  2. First commit