# Reverse for loop not removing isntances.

**URL:** <https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430>\
**Category:** Audio\
**Tags:** Instanced-Mesh, unreal-engine, remove, loop, question, Blueprint\
**Created:** [July 20, 2021, 4:06pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430 "2021-07-20T16:06:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9c129c2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9c129c2](https://forums.unrealengine.com/u/anonymous_user_b9c129c2)\
**Post date:** [July 20, 2021, 4:06pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/1 "2021-07-20T16:06:46Z")

</div>

Trying to remove hism instances with a reverse for each loop.

It only removes 3 then stops, a print string tells me the array is much bigger.

Why is it only looping through 3 times then stopping?

Any help appreciated.

 ![344592-reverseloop.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/f/5/ef5cc2f5b6b16f4f592982904296ff8cd99e9a09.png)

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [July 20, 2021, 4:14pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/2 "2021-07-20T16:14:44Z")

</div>

I don’t know exactly what’s happening here, you need to break your debugging down into stages.

What I can tell you, is you don’t need a reverse for loop. That’s for when you’re removing items from the array, which you aren’t doing here.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9c129c2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9c129c2](https://forums.unrealengine.com/u/anonymous_user_b9c129c2)\
**Post date:** [July 20, 2021, 4:14pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/3 "2021-07-20T16:14:44Z")

</div>

I have a print string printing the elements before the removal and I can confirm the array has 30+ instances in it with the correct hits… But the loop only removes like 3 then stops?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9c129c2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9c129c2](https://forums.unrealengine.com/u/anonymous_user_b9c129c2)\
**Post date:** [July 20, 2021, 4:15pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/4 "2021-07-20T16:15:33Z")

</div>

… I am trying to remove instances. I just clear the array at the end.

I have a print string printing the elements before the removal and I can confirm the array has 30+ instances in it with the correct hits… But the loop only removes like 3 then stops?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 20, 2021, 4:25pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/5 "2021-07-20T16:25:32Z")

</div>

If you remove an index, _some_ other indexes will be redistributed - jumbled. It does not matter here that you’re doing it in reverse - the indexing within the HISM does not work like a regular array where elements are neatly ordered; they cannot be discarded / (re)moved around while others keep their spot.

You cannot have a situation where there is no index 5 in HISM with 10 instances. The moment you remove 5, number 8 may become 6. 😑

If you want to remove a bunch of indexes, add them to an array and [bulk-remove][1] _in one go_ rather than one-by-one. There are linked examples of this in the other thread where we talked about casting.

* * *

Or even better, have a look at how [Sets][2] work - they offer very efficient operations that work really well with HISM indexes. As in:

 ![344583-screenshot-1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/7/f/b7f9682d8ebb36ae79c43166a5026f0396d9a2f8.png)

This way you do not even need to use AddUnique. A Set will ensure values are unique. And there’s no more iteration, it all happens in one go.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9c129c2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9c129c2](https://forums.unrealengine.com/u/anonymous_user_b9c129c2)\
**Post date:** [July 20, 2021, 4:29pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/6 "2021-07-20T16:29:40Z")

</div>

But you can’t bulk remove because you need both the component ref and the specific index, no?

Hmm remove instances node that takes the array still only removes 1 instance, but not the others.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 20, 2021, 4:32pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/7 "2021-07-20T16:32:06Z")

</div>

But you have both. You’ve build the Unique Array - Remove Instances instead of the 2nd reverse loop.

* * *

> Hmm remove instances node that takes  
> the array still only removes 1  
> instance, but not the others.

`Remove Instances` will remove all _valid_ instances from the target HISM that were listed in the provided array.

If you were to do it on-by-on in a loop, every time you remove an index from a HISM, their order will get updated. Then, were you to attempt removal of another element, the indexing data you originally had is now stale, the array no longer reflects the order of the HISM indexes. And you’ll be removing something at random, providing it even exists…

- build the array of indexes you need
- Remove Instances using said array (or Set!)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9c129c2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9c129c2](https://forums.unrealengine.com/u/anonymous_user_b9c129c2)\
**Post date:** [July 20, 2021, 4:51pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/8 "2021-07-20T16:51:52Z")

</div>

![344594-look.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/9/5/39550054ecc9385e2e622a5a107bea4d19a257d5.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 20, 2021, 5:04pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/9 "2021-07-20T17:04:42Z")

</div>

- what is the array length before Remove
- what is the instance count before Remove
- what is the instance count after Remove
- how are we counting the “only 1 gets removed”

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [July 20, 2021, 5:07pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/10 "2021-07-20T17:07:44Z")

</div>

Also, be careful with Instance Count, you’re doing 1 index too many in the For Loop. Ensure the last index is Instance Count-1.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9c129c2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9c129c2](https://forums.unrealengine.com/u/anonymous_user_b9c129c2)\
**Post date:** [July 20, 2021, 5:37pm UTC](https://forums.unrealengine.com/t/reverse-for-loop-not-removing-isntances/486430/11 "2021-07-20T17:37:50Z")

</div>

Hmm Okay, I ended up figuring it out.

Everything was working as intended but when the instances got removed they didn’t appear to visually update. I restarted my project and it works now? Hurray?

Thanks for the help anyways I ended up going with just the remove instances with the array plugged in. Until next time!
