Hey guys, I am trying to do seemingly simple task - destroy active parts of a live geometry collection. I want to destroy the broken pieces after some time, so only the hole in the wall remains (see image).
EDIT: I found the bRemoveOnMaxSleep and EnableRemovePiecesOnFracture switches, but I still need to serialize the GC with the “missing” pieces and de-serialize it on game load. How do I do that?
I am ofc using sleep fields, but my end goal is to save the collection like this and then load it without the debris pieces. Yesterday I was trying to find a way how to serialize and de-serialize the positions, but I had to dig deep into the Chaos core code, and that’s above my skill level. So I decided an easier approach - just delete the parts after some time.
My code right now does have some effect, it deletes the parts, but with a lot of exceptions in the core code and on certain GC objects it crashes entirely.
I must be missing some important step, like updating the chaos solver before getting the dynamic GC or something (I don’t know how to work with the dynamic and rest GC at all). I just want to delete the active pieces, but can’t…
// get the geometry collection on my GeometryCollectionActor
UGeometryCollectionComponent* gc_comp = GetGeometryCollectionComponent();
auto dynamic_collection = gc_comp->GetDynamicCollection();
const TManagedArray<FTransform>& gc_transforms = dynamic_collection->GetAttribute<FTransform>("Transform", FGeometryCollection::TransformGroup);
const TManagedArray<bool>& active_pieces = dynamic_collection->GetAttribute<bool>("Active", FGeometryCollection::TransformGroup);
// I was getting an exception "GlobalMatrices (Num=%d) cached on GeometryCollectionComponent are not in sync with ExemplarIndexArray (Num=%d) on underlying GeometryCollection; likely missed a dynamic data update" so I added this
auto GeometryCollectionEdit = GetGeometryCollectionComponent()->EditRestCollection(GeometryCollection::EEditUpdate::Dynamic);
auto result = GeometryCollectionEdit.GetRestCollection();
// here I get the active pieces ("Active" TManagedArray in the collection)
TArray<int32> to_delete;
for (int i = 0; i < gc_transforms.Num(); ++i)
{
if (active_pieces[i])
{
to_delete.Add(i);
UE_LOG(LogTemp, Warning, TEXT("Active piece: %s"), *(gc_transforms[i].GetLocation().ToString()));
}
}
// and here I try to remove it.
UE_LOG(LogTemp, Warning, TEXT("Running delete chaos"));
// result->RemoveElements("Geometry", to_delete);
result->RemoveElements("Transform", to_delete);