Removing multiple instances which are referenced by their index

Hey, I have a problem which is almost funny because it’s not really a bug but to find a solution for this is like solving an impossible puzzle :wink:

I have one Instanced Static Mesh Component. And I have let’s say 3 instances I want to remove. So it’s an integer array with 3 integers, the indexes to the instances. Now I call “Remove Instance” in the loop and the index is the first, second or third number out of the array.

Now the problem is: The only way to reference an instance is with the index. But after I called “Remove Instance” once, all the instances which had a higher index than the instance I removed will reduce their index by one! So the index reference to the other 2 instances I want to remove in this array is completely worthless because they could reference any instance instead of the instance I originally meant.

I need this because I do a multi sphere trace and I want to remove all instances this trace hits. I do a For Each loop on the Hit Result Array of the Trace, and every hit result has the index of the instance which was hit, but after removing one of them the reference to all the others are just trash. I would have to do a new trace after removing an instance to get the new index. This is surely not the way it is supposed to work.

So, do you have a solution for this puzzle? :cool:

I don’t know if there is a way to sort the array from high to low because that would solve your problem. I’m using transform location as a unique value to find the instances i need, perhaps instead of storing the indexes you could find another unique value to reference your instances.

Maybe you should have them all have their own components, this way all of them are index 0, and you can keep track of components rather than instances. I’m working on a game like terraria, and all of my instances have their own components, so I don’t have problems like this.

If your instances are in each their own Instanced Static Mesh Component then there is not sense to use an Instanced Static Mesh Component at all, you could just use a Static Mesh Component.

Thommie, how do you reference an instance with the location? There is only one “Remove Instance” node and it only accept’s an index.

I myself put each different instance in their separate folders in the project.


I have it working now, but it needs a lot of nodes to make this work. I am basically calculating which instances need to get removed based on which ones got removed before. So if I want to remove {4,2,9,3} in this order, I basically need to remove {4,2,7,2} to remove the correct ones.

This is how I do it:

And since I need this to work for a trace which might hit different Instanced Static Mesh Components, I had to add a way to combine all the hits in arrays based on which component was hit, I have not yet cleaned up these nodes:

So it’s a lot of work just to remove some instances. I hope Epic will make this easier in the future.

I ran into the same problem and I am glad to get behind the problem by this post.
Thank you!

In C++ you can sort TArray something like this:

OutHits.Sort(&](FHitResult A, FHitResult B)
    return A.Item > B.Item;

and then remove:

for (int i = 0; i < OutHits.Num(); i++)

that’s all! :smiley:

This is very impressive. Thank you, I was thinking I would have to invent this myself. You saved me like a day at least.