So, UItemSlot just holds reference to a spawned actor.
If i spawn actor but don’t assign a pointer to any of the UItemSlot’s Item in the array, it all works fine, but once i assign it to Item pointer, it crashes once i try to leave PIE saying that object from the PIE level is still referenced.
Tried manually destroying every item inside inventory’s and character’s BeginDestroy() but it didn’t help. Attaching log file, thank you in advanced.
If you spawn stuff and reference it you have to options, or manually releasing the references so that the GC can handle cleanup or you could add the transient keyword to your properties so that it wont get saved.
The manual cleanup should not be done in BeginDestroy because the GC might execute on the same tick and so the cleanup will not happen correctly. So to cleanup stuff before a map change happens will definitely work, for this you could bind yourself to the PreLoadMap delegate from a manager objects of yours or your custom engine instance:
FCoreUObjectDelegates::PreLoadMap.AddSP( this, &UMyCustomEngine::OnPreLoadMap );
void UMyCustomEngine::OnPreLoadMap()
{
FCoreUObjectDelegates::PostLoadMap.RemoveAll(this);
// Do your pre load cleanup here
}
You can also hook into OnPostLoadMap if you need to.
But i solved it by destroying items via FCoreUObjectDelegates::PreGarbageCollect, thank you for pointing out these delegates, didn’t actually know about them.
Still not sure what’s the right place to do post level start up code initialization in UObjects, thought. I just bind the function to the delegate in the class constructor, checking if it’s not a template.