Cannot Adjust for the Index shift when Removing from Array During ForLoop

Hello there!

So, I still have the problem that when removing elements one by one from an array (or instanced static mesh component) during a for loop I’m unable to adjust the new index correctly to take in account that the array shrinks each time it has removed an element.

Initially I wanted to solve this problem with a reverse for loop but with this method I noticed that I am unable to update my reference which is imperative for my system to work correctly.

As you can see in the picture above:

  • Fist I’m adding a bunch of instances to an Instanced Static mesh Component and right after that I’ve created a Transform Array to reference its content (this is the reference I need to update after removing an instance).
  • Second I’m going through this very Transform Array and use its index as my remove value in order to remove the instances in my Instanced Static Mesh Component one by one.

As you can see I’m using a variable “MinusOne” because I want to adjust the Remove Value after an Instance has been removed (because of the index shifting as a result of that). At the beginning the value is 0 then, when the first instance has been removed, I set it to 1 so it calculates the new remove value -1 in order to get the correct index for the next element again… however it doesn’t seem to work like that.

Maybe I’m overlooking something? Is this not the correct way to adjust for the index shifting?
My system is almost done and I still haven’t figured this out, so I would be eternally grateful for some advice!

Thanks again for your help!

You should have a quick re-read of the comments in the old topic.
If it’s still not clear, stick some print string nodes in there and print out the index you’re getting from the foreach loop and the index you’re passing into the remove instance node. I think once you see the numbers you’ll see what’s going on.

Why are you so reluctant to give me an answer…?
I will try some further but I’d really appreciate some help…

If I would have understood it in the first place I wouldn’t be here desperately looking for an explanation. I’ve already discussed the issue with my designer co-worker but she’s just a designer after all and wasn’t really able to help me. I gave this matter serious thought but still I don’t seem to get it!

So please, if you do know what I’m doing wrong, if you know the correct answer in my case (as shown in the picture), it would help me much more if you could give me the solution right away so I can have this famed “aha” effect and move on…

There are a lot of other things I have to take care of as well and I’m dwelling on this for far too long now without any success.

Thanks a lot,
Kind Regards Gull Jemon

Make an array variable of integers and store the index values you want to remove. Then on complete go through that array and remove the items of the TargetArrayList.

In your foreach loop, you’ve got all the items. So for this example say there are 10.
It loops over each of them, so index 0 to 9.
At the start, the foreach ‘array index’ will match up with your instance index, since it also has 10 items.

First the first loop the index will be 0, so you’ll remove instance 0.
Second loop you’ll go to remove index 1, and with this loop you’ll subtract 1, so the index is 0, that will work
Third loop the index will be 2, you’ll subtract one, making the remove index 1 which is now wrong, because you’re skipping 0 thanks to the reorder of the instances after a removal

So, keep track of the number of items removed, decrement your removal index by that value.

I was reluctant to just give you the answer because you learn more from working on these things yourself. Especially true for this situation where you had all the info you needed from the docs and from the previous topics.

Anyway, hope that fixes your issue and good luck with your project.

Cool thanks a lot, that’s exactly what I need. You see I’ve tried this already and bumped into a problem, so I thought it was wrong…

UPDATE: This is the correct answer though!
My system didn’t work due to some other error I’ve made prior to these calculations…

This here is the Solution without reverse for each loop:

Like that some instances get removed and some not, but the index will always be correct no matter what happens.

Thanks a lot for your help,

Kind Regards Gull Jemon