Hi. I was wondering if there is an easy way to reset a level without reloading it. I know there is the AGameModeBase::ResetLevel method, but it seems I would then have to implement Reset() on all actors and track their initial states. I would like to avoid that at all costs. I also know I can do something like APlayerController::RestartLevel or this:
UGameplayStatics::OpenLevel(GetWorld(), FName(UGameplayStatics::GetCurrentLevelName(GetWorld())));
However, the problem with that method is in the packaged game, the textures seem to get unloaded when reloading the level and take a second to reload (for a second they are blurry). So, what I’m really asking is: “Is there a way to get the loaded assets to remain in memory when I reload the level”
I use Linux, and I can use C++ or Blueprints, whichever you like.
As far as I know, your only options are
-
Use ResetLevel
-
Record the changes in status of all actors during gameplay in the game instance, and re-load from that.
After digging around in the source code for a while, I found a solution.
Here’s what I found:
- The textures are being unloaded because they are being deleted by the garbage collector
- The garbage collector is deleting the textures from memory because they are temporarily unreferenced.
So my solution is this (C++):
- Put an array of UObjects in my Game Instance.
- Before reloading the level, find all texture objects in memory and add their pointers to the array in the Game Instance.
- After the map has been reloaded remove the references from the array.
**Keeping a reference to the textures in the GameInstance keeps them from being unloaded, so they do not have to be reloaded when the level is reloaded **
I found all textures in memory using this:
TArray<UObject*> Objects;
GetObjectsOfClass(UTexture2D::StaticClass(), Objects, true);
I hope this helps anyone who has this problem.