HISM being super tricky to remove. How do i remove them properly with a delay.

Hi,

As you found when you remove an instance in a ISMC (same for the HISM) the instance index will change, therefore you can’t cache the Instance index to refer to a specific instance later on.

There is a static global delegate you can bind to, that when any ISMC is about to update an instance index, you get the old index and the new one. With that you should be able to update any cached index you have.

FInstancedStaticMeshDelegates::OnInstanceIndexUpdated

There are probably better examples in the engine for runtime applications, but that is what I use for the Chaos Visual Debugger tool.

In CVD (for short) although it is an editor tool, depending on the map being debugged we have to render tens of thousands of meshes to represent collision shapes, and in the live debugging mode we have to do it at the same frame time the game is running. As most of these shapes are the same but with different transforms we use ISMC heavily.

Similar to what you do, we have a struct instance that the rest of the system uses to be able to access the specific mesh instance in the ISMC, but we keep the cached instance index up to date by binding to FInstancedStaticMeshDelegates::OnInstanceIndexUpdated.

Here is a place where that is handled in CVD.

https://github.com/EpicGames/UnrealEngine/blob/ue5-main/Engine/Plugins/ChaosVD/Source/ChaosVD/Private/Components/ChaosVDInstancedStaticMeshComponent.cpp#L261

Something worth mentioning: You should bind to that event in a manager object (or something that is instantiated only once), not the ISMC . The callback you get gives you the correct component where the update is happening.

The events you care about are

FInstancedStaticMeshDelegates::EInstanceIndexUpdateType::Relocated
FInstancedStaticMeshDelegates::EInstanceIndexUpdateType::Removed

In Relocated, you will get the old index (the one you have cached in your struct) and the new one (the one you need to update to).

That said, I think the trick of setting the instance transform scale to 0 is good and something we use in CVD as well, as that is usually faster than doing the removal.

You will need to find the balance between hiding them and fully remove them performance wise.

Implementing some kind of pool like @Everynone suggest to achieve that is a good idea.