Setting array element while cycling through the array

Ok, bear with me, I know the title probably doesn’t make any sense, so, allow me to elaborate…

I assigned a key to increment through the indexes of an array. For example, if I keep pressing the key, it takes me to the next index, until I reach the last and it loops back.

I resized the array to, let’s say 5. So, the cycle starts from index 0 to 4 and back again.

I created an integer variable for the incremented value, let’s call it “Current Index”, which is the index that I stopped at the last time I pressed the key.

So, if I’m starting at Index 0 and I press the key twice, I reach 2. Index 2 is now my “Current Index”.

My goal is, if I picked up an item, its information goes straight to the element of the “Current Index”. I achieved this by using “Set Array Elem” and connected the index to the variable “Current Index”.

So, if my current index is 2, and I picked up an apple. Cycling through the array would go as such;

0 = none
1= none
2 = Apple
3 = None
4 = None

This is really cool, because I get to choose which index I want to add an element to.

Here’s the problem though…

If I pick up a different item, banana, while my current index element is occupied with apple. It replaces it. I don’t want that… I want it so that if my current index is occupied with Apple, Banana would find any vacancy in the array and settle there. How do I achieve this?

I tried many different ways to figure that out but failed miserably…

I know I can I use Insert, Add, Add Unique and whatnot, but it takes away the freedom and flexibility to choose my own desired order of elements in the index. I did find a way to get my idea to actually work using these functions, the issue though is that they add to the size of the array… and I only want the array to remain at a specified size.

Any help is appreciated!

Here is a quick test on how it could work:

The BP:

  • Set defaults on BeginPlay. ActorsToBePicked array is of type Actor with hard references to Static Mesh Actors in the level:
  • CycleArray event loops every 2 seconds. Used soft reference array considering actors are destroyed on pickup and might need to be loaded:
  • ActorToBePickedBeginOverlap has the logic to set array elements without overwriting.
  • This is the macro that returns an index that has no valid pointer (that are ‘none’)

Result:
LoopArray

Hope it helps.