# Hierarchical instanced static mesh component Bugg

**URL:** <https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895>\
**Category:** Asset Creation\
**Tags:** Hierarchical-Instanced-Static-Mesh, unreal-engine, bug-report, question\
**Created:** [January 15, 2017, 3:08am UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895 "2017-01-15T03:08:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nameLive](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@nameLive](https://forums.unrealengine.com/u/nameLive)\
**Post date:** [January 15, 2017, 3:08am UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/1 "2017-01-15T03:08:50Z")

</div>

Hi, i think i’ve found a bugg with this component when you try to remove the 0 in its array.

I was making a Trail to follow an object, then i wanted to remove the trail starting from 0 in its array. It worked for the first field, but after it “removed” it, it replaced the 0, with the value that the last index in the array had.  
In other words, instead of 0 becoming what 1 was in the array, 0 became what the last value had.  
I checked it carefully frame by frame, it was quite puzzling. Then i redid the exact same thing but with instanced static meshes and it worked as it should.

I tried to redo this bugg in a clean project, but this time when i tried to remove the 0, i got an engine crash everytime.  
Here’s the project i redid it in, you add instances with T and Remove with R. Associated blueprints are in content browser root folder.

[https://drive.google.com/file/d/0B-hVYtzySBWTaVNJVGtNRzhuYVk/view?usp=sharing](https://drive.google.com/file/d/0B-hVYtzySBWTaVNJVGtNRzhuYVk/view?usp=sharing)

All the best!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [January 16, 2017, 6:15pm UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/2 "2017-01-16T18:15:43Z")

</div>

Hello nameLive,

Thank you for reporting this issue. I’m currently looking into it using the project you provided but I wanted to let you know why the project was crashing. When you went to populate the array, the Hierarchical Instanced Static Mesh didn’t have a Static Mesh set, so it was essentially creating blank instances. When the remove function tried to access them, it saw an empty array and crashed. Setting a static mesh fixes this.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [January 16, 2017, 7:48pm UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/3 "2017-01-16T19:48:03Z")

</div>

I’ve reproduced the problem that you reported using your project and it definitely does seem like a bug. I’ve placed a report in which you can find here: [UE-40693](https://issues.unrealengine.com/issue/UE-40693) You can track the bug’s progress there as well.

Have a nice day!

---

<div class="post-metadata">

**Author:** ![zEh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zEh](https://forums.unrealengine.com/u/zEh)\
**Post date:** [September 22, 2017, 11:19am UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/4 "2017-09-22T11:19:41Z")

</div>

Not only has HISM this ordering bug but ISM does not correctly remove instances all the time.  
I am currently using ISM and waiting for the fix in 4.18 for them.  
But I need HISM for per-instance LOD. Otherwise I won’t have any chance for near-distance geometry details. When will this bug be fixed? It’s crucial for performance on visually appealing instances.

Can’t this be done in the context of [the ISM bug](https://answers.unrealengine.com/questions/704641/bug-instanced-static-mesh-component-is-broken-in-4.html)?

---

<div class="post-metadata">

**Author:** ![bennetherwood](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bennetherwood](https://forums.unrealengine.com/u/bennetherwood)\
**Post date:** [October 25, 2017, 6:42pm UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/5 "2017-10-25T18:42:25Z")

</div>

Bump this also, Id like a fix please, and have seen it on as UE-40693 but its been around for ages.

Thanks,  
Ben.

---

<div class="post-metadata">

**Author:** ![superdreamgen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@superdreamgen](https://forums.unrealengine.com/u/superdreamgen)\
**Post date:** [January 1, 2018, 3:10am UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/6 "2018-01-01T03:10:57Z")

</div>

I ran into this issue as well, and I’ve come to the conclusion that this is not in fact a bug, but is in reality a lack of proper documentation for the HISMC RemoveInstance() method.

What’s happening is that internally, the HISMC array is removing the instance using TArray.RemoveAtSwap(int32 index). This is an array removal operation that indeed removes the array item at index, then replaces it with the item that is at the end of the array: [TArray::RemoveAtSwap | Unreal Engine Documentation](https://docs.unrealengine.com/latest/INT/API/Runtime/Core/Containers/TArray/RemoveAtSwap/2/index.html)

RemoveAtSwap() is far more efficient than RemoveAt(), which is probably why it was used.

One way to work with this is to store your reference data for the HISMC instances in a TArray, and simply use RemoveAtSwap() to remove your data in your array, to mirror the state of the HISMC internal array.

Cheers. 🙂

---

<div class="post-metadata">

**Author:** ![superdreamgen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@superdreamgen](https://forums.unrealengine.com/u/superdreamgen)\
**Post date:** [January 1, 2018, 3:11am UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/7 "2018-01-01T03:11:41Z")

</div>

I don’t think this is actually a bug, please see my answer below.

---

<div class="post-metadata">

**Author:** ![superdreamgen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@superdreamgen](https://forums.unrealengine.com/u/superdreamgen)\
**Post date:** [January 1, 2018, 8:27pm UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/8 "2018-01-01T20:27:33Z")

</div>

Hello Matthew,

**I do not believe this is a bug, please see my answer below.** In fact, if this “bug” were to be fixed, and HISMC instance arrays to switch to using RemoveAt() rather than RemoveAtSwap(), it would reduce the efficiency of not only the HISMC algorithms, but also of data structure algorithms that manage HISMC indices in game code! (e.g. only having to update the cached index of one instance, rather than ALL indices \> the index that was removed!).

**What should really be done are two things:**

**1.** Update the UE API documentation for HISMC RemoveInstance() to indicate the array operation used behind the scenes (RemoveAtSwap) so that developers understand why the last element of the array is being placed into the hole in the array.

**2.** Update the UE API documentation for TArray RemoveAtSwap() to explain that it moves the last item in the array to fill the hole created by the remove operation.

This behavior of RemoveAtSwap() is detailed here in the UE blog: [Optimizing TArray Usage for Performance - Unreal Engine](https://www.unrealengine.com/en-US/blog/optimizing-tarray-usage-for-performance)

The blog article states:

_“There’s a way to avoid the performance hit of shifting all elements to fill in a gap when an element is removed: RemoveAtSwap(). This removes the element at the requested index, then if possible replaces it with the last element in the array to fill in the hole, rather than shifting down all elements.”_

Using RemoveAtSwap() is far more efficient than RemoveAt() (both internall in the engine, and within developer game code) !!! Please do not fix this “bug”!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [January 5, 2018, 9:11pm UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/9 "2018-01-05T21:11:40Z")

</div>

Hello superdreamgen,

Thank you for pointing this out. I’m not now, nor was I ever, very familiar with HISM so I didn’t notice this but you’re correct. I’ll put a comment on the bug report that mentions that this shouldn’t exactly be “fixed” but an additional remove function for the functionality that the other posters were expecting would be useful.

---

<div class="post-metadata">

**Author:** ![superdreamgen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@superdreamgen](https://forums.unrealengine.com/u/superdreamgen)\
**Post date:** [January 9, 2018, 12:53am UTC](https://forums.unrealengine.com/t/hierarchical-instanced-static-mesh-component-bugg/381895/10 "2018-01-09T00:53:24Z")

</div>

Hello there Matthew,

If they do add a RemoveInstance function that doesn’t utilize RemoveAtSwap, the API doc should note that calling that function instead will lead to slightly reduced performance.
