Download

How to make UFoliageInstancedStaticMeshComponent with removed instances serializable?

Hey everyone. I’ve run into a problem that I couldn’t solve. Thus I hope anyone in here has some idea how to approach it :slight_smile:

I want to have a character ability that can remove certain foliage types. For that purpose I’ve implemented a new component (let’s call it [FONT=Courier New]MyCustomFoliageComponent) that inherits from [FONT=Courier New]UFoliageInstancedStaticMeshComponent and is set for the corresponding [FONT=Courier New]StaticMeshFoliages as [FONT=Courier New]ComponentClass. If the character performs its ability I detect affected FoliageActors by checking for [FONT=Courier New]MyCustomFoliageComponen and perform a combination of [FONT=Courier New]MyCustomFoliageComponent::GetInstancesOverlappingSphereand [FONT=Courier New]MyCustomFoliageComponent::RemoveInstances. This works quite well.

The problems occur when I try to make its state serializable/compatible with save games. I have a serialization framework and a corresponding interface that [FONT=Courier New]MyCustomFoliageComponent implements. The interface has a [FONT=Courier New]OnLoad function that is triggered properly (e.g. after a savegame is loaded). And when I check the number of instances with [FONT=Courier New]GetInstanceCount*()* it returns the correct number of instances (thus the count after some instances have been removed). But there are/were two problems:

[Solved]: Visually even the removed instances have been visible at this point. Thus I’ve looked into the implementation of [FONT=Courier New]RemoveInstances and have notices some code that helped to solve this issue. Adding the following piece of code to the [FONT=Courier New]OnLoad function solved the issue.



if (bAutoRebuildTreeOnInstanceChanges)
{
BuildTreeIfOutdated(true, true);
}

MarkRenderStateDirty();


This is just FYI in case someone has a similar problem.

Problem: Even if the code above fixes the visual problem… The colliders of the corresponding removed instances are still there. I’ve tried out some functions (e.g. [FONT=Courier New]UpdateCollisionFromStaticMesh) but it didn’t help. Now I am stuck and don’t know how to trigger the collision update for the underlaying foliage mesh properly. Does anyone have any hint or idea how to solve this?