I’m trying to implement a feature in my save mechanic where the player can open a loading menu from any level in my game and load their last save file. The problem I seem to be having though is that when I call UGameplayStatics::OpenLevel(GetWorld(), LoadGame->LevelName), opening the level will destroy my character class when I leave the current level and then construct it when I enter the new level. This in turn is resetting all my character values to their defaults or whatever the editor defaults are. Does anyone have any advise as to how I can maintain my loaded slot data between levels?
Here is my saving and loading functions. The properties in my MeatSaveGame class all have the UPROPERTY(VisibleAnywhere, Category = Basic)
void AMeatCharacter::SaveGame()
{
UMeatSaveGame* SaveGame = Cast<UMeatSaveGame>(UGameplayStatics::CreateSaveGameObject(UMeatSaveGame::StaticClass()));
if(SaveGame)
{
SaveGame->PlayerStats.MaxHealth = PlayerStats.MaxHealth;
SaveGame->PlayerStats.Health = PlayerStats.Health;
SaveGame->PlayerStats.MaxStamina = PlayerStats.MaxStamina;
SaveGame->PlayerStats.Stamina = PlayerStats.Stamina;
SaveGame->Rotation = GetActorRotation();
SaveGame->Location = GetActorLocation();
FString LevelName = GetWorld()->GetMapName();
LevelName.RemoveFromStart(GetWorld()->StreamingLevelsPrefix);
SaveGame->LevelName = LevelName;
UGameplayStatics::SaveGameToSlot(SaveGame, SaveGame->SlotName, 0);
}
}
void AMeatCharacter::LoadGame()
{
// This is fine for now however, this is going to be problematic when the need for saving levels will be necessary
UMeatSaveGame* LoadGame = Cast<UMeatSaveGame>(UGameplayStatics::CreateSaveGameObject(UMeatSaveGame::StaticClass()));
LoadGame = Cast<UMeatSaveGame>(UGameplayStatics::LoadGameFromSlot(LoadGame->SlotName, LoadGame->SlotIndex));
if (LoadGame)
{
UGameplayStatics::OpenLevel(GetWorld(), *LoadGame->LevelName);
if(PlayerController)
{
FInputModeGameOnly Input;
PlayerController->SetInputMode(Input);
}
PlayerStats = LoadGame->PlayerStats;
// If I debug from here I can see that LoadGame does get the rotation from the save game fucntion
SetActorRotation(LoadGame->Rotation);
SetActorLocation(LoadGame->Location);
}
}