[Bug] UHierarchicalInstancedStaticMeshComponent Can not use indices if RemoveInstance is used

In UHierarchicalInstancedStaticMeshComponent::RemoveInstance, RemoveAtSwap is used, which doesn’t preserve order.

As far as I can tell, the instance indices become unpredictable when removing other instances.

My usage: I was using UInstancedStaticMeshComponents for grid tiles, where tiles would be swapped out and replaced when interacted with. In 4.7, UInstancedStaticMeshComponents have become very slow when calling RemoveInstance, so I’ve been attempting to use UHierarchicalInstancedStaticMeshComponent (in my tests, while unpredictable and incorrect, they are much quicker)

Hi Grogger,

To keep things quick, HISMC uses the Array RemoveAtSwap function which removes an instance in the middle of the array by moving the instance from the end of the array into the place of the one you removed. So if you have 10 items numbered 0 to 9, removing item 4 works by moving item 9’s data into item 4 and then reducing the array count. I realize this is less than ideal if you’re remembering the instance indices yourself.

How are you getting the instance to remove? Are you remembering them in the order you added them?
If you’re using a collision test then they should be automatically updated.

Hi ,

I am keeping track of the index for each grid tile and all other interactions with the grid works through the tile index.

Knowing how RemoveAtSwap works really helps though, I fixed my problem by keeping track of the last item and swapping it out along with RemoveInstance.

Thank you very much!