[Question] How to destroy an actor properly in C++?

#MY Entire Delete Function For You

I just did rigorous testing using UE_LOG and tick function for my 3d HUD Static Mesh Actors

I create and destroy them very frequently, several times per minute

I ran many tests

the function I use is working perfectly, instantly removing prior actors after I call my function on them

#VDestroy(UObject * ToDestroy)

void AVictoryGamePlayerController::VDestroy(UObject * ToDestroy)
{
	if (!ToDestroy) return;
	if (!ToDestroy->IsValidLowLevel()) return;
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	//Actor?
	AActor * AnActor = Cast(ToDestroy);
	if (AnActor)
	{
		AnActor->K2_DestroyActor();
		ToDestroy = NULL;
	}
	
	//Object - BeginDestroy
	else
	{
		//Begin Destroy
		ToDestroy->ConditionalBeginDestroy();
		ToDestroy = NULL;
	}
	
	//GC
	GetWorld()->ForceGarbageCollection(true);
	
}
2 Likes