How to optimize the process of respawning a Breakable Actor?

I created a Breakable Actor with Choas Destruction in UE5.
After this Actor is destroyed, it respawns.
But frame drops occur in the process of Destroying the Actor and SpawnActor.
Do you know how to optimize the process of respawning a Breakable Actor?

1 Like

A single actor?

1 Like

Yes! A Single Actor
Is the optimization method much different for one actor and multiple actors?

1 Like

Can you show how you’re doing it? And explain more about the actor. This should not be happening in the first place.

1 Like

Breakable Actor only has GeometryCollection Component.
When Character is attacking, Character’s Field System is activated and Breakable Actor is broken.
OnBreakEvent → SetCollisionResponse and Respawn using lambda function.
When Breakable actor is respanwed, frame drop occurs.
I think, this Breakable actor has a lot of fragments(51 fragments currently) :sweat_smile:
so when this actor destroyed and respawn, it seems to affect the frame.
but I want to put a lot of these actors.
So I want to know how to efficiently respawn breakable actor.

void ABreakableItem::OnBreakEvent(const FChaosBreakEvent& BreakEvent)
{
	if (bIsDestructed)
	{
		return;
	}

	GeometryCollection->EnableClustering = false;
	GeometryCollection->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
	GeometryCollection->OnActorEnableCollisionChanged();

	const float respawnTime = RespawnTime.X * 60 + RespawnTime.Y + DestroyDelayTime;
	Respawn(respawnTime);
	bIsDestructed = true;
}

void ABreakableItem::Respawn(float DelayTime)
{
	FTimerHandle DelayTimerHandle;
	GetWorld()->GetTimerManager().SetTimer(DelayTimerHandle, FTimerDelegate::CreateLambda([&]()
		{
			if (bCanRespawn)
			{
				FActorSpawnParameters spawnParams;
				spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
				FTransform transform = GetActorTransform();
				transform.SetLocation(SpawnLocation);
				GetWorld()->SpawnActor<ABreakableItem>(GetClass(), transform, spawnParams);
			}
			Destroy();
		}), DelayTime, false);
}

Finally I knew why frame drop occurs when my actor respawned!!
The problem was changing the values ​​in GeometryCollection in the constructor. :joy:
Thanks for your kindness and help.

void ABreakableActor::OnConstruction(const FTransform& Transform)
{
	Super::OnConstruction(Transform);

	UGeometryCollection* GeometryCollectionObject = GeometryCollection->EditRestCollection().GetRestCollection();
	if (IsValid(GeometryCollectionObject))
	{
		//remove effect
		GeometryCollectionObject->bRemoveOnMaxSleep = true;
		GeometryCollectionObject->MaximumSleepTime = FVector2D(0, DestroyDelayTime);
		GeometryCollectionObject->RemovalDuration = FVector2D(0, 0.5f);
		//can break when landing
		GeometryCollectionObject->bMassAsDensity = false;
	}
}
1 Like

Honestly, :medal_sports: to you for bringing it up, solving it and posting about it!

1 Like