Yes, there is nothing more to say. What I need to do is to instantly delete UObject after it’s being used. How to do it ?
Here’s the function I use, after having done much research on this matter.
I have an in-game editor so I have to be able to reliably delete entire levels worth of stuff instantly.
I routinely delete material instances (Dynamic), static mesh actors, characters, and more using this method
This function of mine works, I guarantee you
#My Delete UObject / AActor Function For You
void AYourClass: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);
}
#IsValidLowLevel()
When you have objects that could have been recently deleted and Garbage Collection purged, it is VERY important that you do a second check for such actors that could be / have been recently destroyed
so you have a UObject* MyObj;
Here’s the check
if(!MyObj) return;
if(!MyObj->IsValidLowLevel()) return;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MyObject->DoStuff();
You can create a convenience method to do both of these checks, and return false if the reference is not valid.
Enjoy!
Rama