# Instance Index Change

**URL:** <https://forums.unrealengine.com/t/instance-index-change/682910>\
**Category:** Blueprint\
**Tags:** unreal-engine, UE5-0, question, Blueprint\
**Created:** [November 3, 2022, 12:17am UTC](https://forums.unrealengine.com/t/instance-index-change/682910 "2022-11-03T00:17:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 3, 2022, 12:17am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/1 "2022-11-03T00:17:19Z")

</div>

Hello!  
I’m having a problem with removing instances. I spawn them through a blueprint and store every instance index in a map so I can ‘reference’ them. But if I remove an instance the whole map gets messed up.  
I just found out that’s because if I remove an instance the indexes will change for all the others (or at least for ones with the higher index values).

> [@4.17 Instance static mesh wrong index when removed](https://forums.unrealengine.com/t/4-17-instance-static-mesh-wrong-index-when-removed/405300/11):
>
> This is because each time you remove an item, the total array of those instances is re-sized. So if you have 50 blocks there and the first one is “0”, if you remove that “0” the next block in line will then become the new “0” and the total array will become 49 instead of 50. I am dealing with a bug caused from this same issue. In my game, a player can mine ores. Lets say there are 5 iron ore nodes on my map. If a player mines an iron ore, my code properly removes that specific iron node from t…

I read an idea that in the map I should decrease every higher up indexes than the removed one. I find this method quite cumbersome and since I’d possibly have hundreds of instances I’m not sure it’d be good for the performance doing it every time an instance is removed.

> [@Instanced Static Mesh, Removing Instance Issue](https://forums.unrealengine.com/t/instanced-static-mesh-removing-instance-issue/66187/11):
>
> Maybe im a little late to this party, sorry guys. Yes, removing an instance index from the component effectively makes every instance with a higher index than the one removed to go down one position. If you have your instances indexed (which i really recommend you to) with a class or struct that holds their data (component index, instance index, transform, etc), you should decrease every instance index that has a higher instance index than the one removed to keep your data on par. Cheers! I h…

I also read someone mentioned “Dynamic Instance Buffer”, but I couldn’t find that anywhere. Is this something that still exists and I just couldn’t find it or did it get removed or renamed or what about it?

_So just for clarification, my problem here is that the indexes change at every instance remove and because of this I can’t really keep track of them. Is there any solution to this like a way to prevent the indexes from changing?_

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [November 3, 2022, 12:26am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/2 "2022-11-03T00:26:09Z")

</div>

Have a look at HierarchicalInstancedStaticMeshes - when you remove an instance, it does the equivalent of a TArray.RemoveSwap so the last index gets swapped with the removing index

---

<div class="post-metadata">

**Author:** ![Mind-Brain](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mind-brain/32/906573_2.png) [@Mind-Brain](https://forums.unrealengine.com/u/Mind-Brain)\
**Post date:** [November 3, 2022, 12:27am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/3 "2022-11-03T00:27:06Z")

</div>

Hey @Tsybe!

I would suggest replacing your removed instances with an empty, a replacement. Just a placeholder! That way it will keep your array nice and neat, and also not throw null if you try to pull from an index that has been “removed”.

---

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 3, 2022, 1:25am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/4 "2022-11-03T01:25:29Z")

</div>

Hey @RecourseDesign!

I tried it with HISM instead of ISM but the problem still occurs. If I remove for example the 6. instance index from the instances then the old 7. becomes the 6. and the old 8. becomes the 7. and so on, and it’ll end up mismatched with my map variable.

> [@RecourseDesign](#):
>
> the last index gets swapped with the removing index

And to be honest I don’t know what this should mean and why it would be good for me.

---

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 3, 2022, 1:26am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/5 "2022-11-03T01:26:55Z")

</div>

Hi @Mind-Brain, thanks for your suggestions!

So instead of removing the instance you suggest replacing it? It sounds great, but I don’t know how to do it in practice. Unfortunately I can’t find any node like that, could you give me its name please?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [November 3, 2022, 2:11am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/6 "2022-11-03T02:11:17Z")

</div>

> [@Tsybe](#):
>
> And to be honest I don’t know what this should mean and why it would be good for me.

It doesn’t move the indexes, it just takes the last instance in your component and changes it’s index to that of the one you just removed - all you need to do is update your Map to reflect that one change.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/a/3/2a37dbca7ea0be67feda79e6653aa82edfbceef0.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/9/5/f953e4b4b432fa34cc5aca27fa0f8d0307de87d0.jpeg)

I’ve read of some people, rather than removing the instance, just setting it’s scale to 0,0,0

---

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 3, 2022, 2:46am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/7 "2022-11-03T02:46:38Z")

</div>

Hmm… it’s very interesting, I tried to do a similar thing:

 ![Screenshot 2022-11-03 033203](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/0/1/a0122cdfd1a201c97438f891276b063aaf137754.png)

The “Instances” is an actor that spawns at beginplay,- _it contains multiple instance components, like the HISM,_ - after that I spawn 9 instances to the HISM.

If I’m right and based on your images, my event should remove only one instance even if I run it numerous times. But what it actually does, it removes 4 instances of the 9 until the instance count goes under 5.

Any idea? Could this be some setting issue?

> [@RecourseDesign](#):
>
> I’ve read of some people, rather than removing the instance, just setting it’s scale to 0,0,0

Yeah, I also read, some said they moved the instances out of the playable area. These aren’t either that bad solutions, but it’d be great if there is a better one.

\*And one more question: what happens when you add another instance after removing those two? What index will it get?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [November 3, 2022, 2:54am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/8 "2022-11-03T02:54:11Z")

</div>

> [@Tsybe](#):
>
> If I’m right and based on your images, my event should remove only one instance even if I run it numerous times. But what it actually does, it removes 4 instances of the 9 until the instance count goes under 5.
> 
> Any idea? Could this be some setting issue?

It does still remove them, if you remove the same index number, it removes the last instance:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/0/1/f019e331e12200bdc50c10f750b8d771c58ce54a.png)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/0/d/00d60c6e1b6e5902d5604e40de93ed4a18ca255f.jpeg)

Yeah those were not really good options for me either - but I’m finding that the HISM works very well - and is fast and easy.

Adding another instance will yield the index of (NumInstances-1) as usual.

---

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 3, 2022, 3:12am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/9 "2022-11-03T03:12:07Z")

</div>

Yes, it’s great, in performance it’s amazing. It’d be even better if I could keep track of the instances. 😃

> [@RecourseDesign](#):
>
> ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/0/d/00d60c6e1b6e5902d5604e40de93ed4a18ca255f.jpeg)

I don’t really get this, if you remove HISM Instance Index 1 twice. How did “Instance 4” get removed? Shouldn’t it look like the ISM or shouldn’t only “Instance 2” get removed?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [November 3, 2022, 3:21am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/10 "2022-11-03T03:21:32Z")

</div>

> [@Tsybe](#):
>
> I don’t really get this, if you remove HISM Instance Index 1 twice. How did “Instance 4” get removed? Shouldn’t it look like the ISM or shouldn’t only “Instance 2” get removed?

When you remove an instance - it doesn’t delete it from the array, it just takes the last instance (NumInstances-1) and moves it into the index you are removing, then just removes the last index in the array.

Some pseudo code is:

```auto
RemoveInstance(index) { 
instanceList[index]=instanceList[instanceList.Num()-1]; instanceList.Remove(instanceList.Num()-1);
}

```

So if you have 4 instances, they will be:

0 - Instance1  
1 - Instance2  
2 - Instance3  
3 - Instance4

If you then remove index 1, that list becomes:

0 - Instance1  
1 - Instance4  
2 - Instance3

And if you then remove index 1 again, that list becomes:

0 - Instance1  
1 - Instance3

---

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 3, 2022, 3:41am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/11 "2022-11-03T03:41:32Z")

</div>

> [@RecourseDesign](#):
>
> So if you have 4 instances, they will be:
> 
> 0 - Instance1  
> 1 - Instance2  
> 2 - Instance3  
> 3 - Instance4
> 
> If you then remove index 1, that list becomes:
> 
> 0 - Instance1  
> 1 - Instance4  
> 2 - Instance3
> 
> And if you then remove index 1 again, that list becomes:
> 
> 0 - Instance1  
> 1 - Instance3

Ohh, okay, it makes sense now, thanks! So with HISM it swaps the index with the last and then removes it, but with ISM it removes it without swapping it, isn’t it? And that’s why all the Instances with the higher Index will decrease by one with ISM.  
Could I ask you what about the “Remove Instances” node, in both case with HISM and ISM?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [November 3, 2022, 3:48am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/12 "2022-11-03T03:48:55Z")

</div>

> [@Tsybe](#):
>
> Ohh, okay, it makes sense now, thanks! So with HISM it swaps the index with the last and then removes it, but with ISM it removes it without swapping it, isn’t it? And that’s why all the Instances with the higher Index will decrease by one with ISM.  
> Could I ask you what about the “Remove Instances” node, in both case with HISM and ISM?

Exactly!

Only the HISM has the RemoveInstances in 4.27 which I’m using (5.00 has it in ISM too), and it works in the same way as the RemoveInstance does:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/c/9/9c9efe7f0416975092d2cd782c0b42514de95bc1.png)  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/7/8/c78a98a8d98015a943e9afa6f0e4cf2b58647ae1.jpeg)

---

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 3, 2022, 4:00am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/13 "2022-11-03T04:00:07Z")

</div>

Thank you! Even if I didn’t get a direct solution, I found out very useful information about ISM and HISM. I highly appreciate your answers and time!

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [November 3, 2022, 7:07am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/14 "2022-11-03T07:07:11Z")

</div>

I don’t really understand the topic entirely, I don’t think, but you’re either using the wrong data structure, or you’re using the right data structure in an incorrect way.

If your code can’t handle a member of the data structure disappearing, then you can’t really expect to use a TArray. I think the problem is more on how you’re accessing the data in the list, than what the list is, though.

---

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 3, 2022, 8:47pm UTC](https://forums.unrealengine.com/t/instance-index-change/682910/15 "2022-11-03T20:47:49Z")

</div>

The problem isn’t with or about the map variable. It’s about the changing indexes of the instances after one (or more) is removed. But with @RecourseDesign’s help, after understanding what is really happening when an instance is removed, it’s easier to start making a solution to it.

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [November 4, 2022, 1:33am UTC](https://forums.unrealengine.com/t/instance-index-change/682910/16 "2022-11-04T01:33:51Z")

</div>

ok, so, there is a variable type called a Map, which has stable indices. Arrays do not have stable indices. But I’m realizing here that this looks like we’re not even talking about anything that is actually a map or an array, so I’m not sure my input is helpful at all. Apologies!

---

<div class="post-metadata">

**Author:** ![Mind-Brain](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mind-brain/32/906573_2.png) [@Mind-Brain](https://forums.unrealengine.com/u/Mind-Brain)\
**Post date:** [November 4, 2022, 4:05pm UTC](https://forums.unrealengine.com/t/instance-index-change/682910/17 "2022-11-04T16:05:17Z")

</div>

Hey @Tsybe! Sorry for the late reply!

So what I was saying was when you remove an item from the index you can use “Insert” directly after your “Remove” to put an invisible, hidden-in-game object with a given class in its place(you would make this yourself), with the “index” pin on the insert being filled with the one you removed. As long as the item added to the array is of the desired class, it can be an instance of something that has very very little data involved but still take the place of the removed item, to preserve the array indices. Like a filler.

I’m not saying it’s the most efficient method, but when you have gone so far in and there’s no looking back, workarounds can help get the game done. 🙂

---

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 4, 2022, 6:21pm UTC](https://forums.unrealengine.com/t/instance-index-change/682910/18 "2022-11-04T18:21:34Z")

</div>

I’m already using a Map variable exactly because of the reason that you mentioned. I also feel like we misunderstand each other. Sorry if my question wasn’t clear enough.

My problem wasn’t with my variable, it was about the indexes of the Instances when one instance gets removed the Instances change indexes. Fortunately, I already discussed it with @RecourseDesign above if you are interested.

Don’t feel sorry, help is always appreciated!

---

<div class="post-metadata">

**Author:** ![Tsybe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tsybe/32/590281_2.png) [@Tsybe](https://forums.unrealengine.com/u/Tsybe)\
**Post date:** [November 4, 2022, 6:22pm UTC](https://forums.unrealengine.com/t/instance-index-change/682910/19 "2022-11-04T18:22:59Z")

</div>

Hi @Mind-Brain, thanks for the reply, no worries.

I understand now! But my problem wasn’t with the variable’s indexes, it was with Instances’ indexes. Maybe I just didn’t clear this enough, sorry for that. @RecourseDesign helped me find out what is going on when an instance is removed and what’s the logic behind the index changes. So I’ve already thought of a workaround, but I couldn’t test it yet. I appreciate your answer.

---

<div class="post-metadata">

**Author:** ![Mind-Brain](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mind-brain/32/906573_2.png) [@Mind-Brain](https://forums.unrealengine.com/u/Mind-Brain)\
**Post date:** [November 4, 2022, 6:28pm UTC](https://forums.unrealengine.com/t/instance-index-change/682910/20 "2022-11-04T18:28:16Z")

</div>

@Tsybe No big deal! Sorry for the miscommunication, I thought I understood what was going on. If you get time, you should post your solution here once you figure it out, so others can get a fix if they run into this issue! 🙂

[Next page](https://forums.unrealengine.com/t/instance-index-change/682910.md?page=2)
