I have a game where the player can load their save file from any level, including a start menu. In my code I call UGameplayStatics::OpenLevel and pass in my saved level name. Then I set my characters info. Finally I set the location and rotation of the actor based on the saved game data.
I think the problem is that when the level is loaded all that data is cleared out because the level opens at where I have the player start actor and the location and rotation i set are zeroed out.
When I call my load game function the data I load from the slot exists and is what I should expect
Here is my save and load game functions:
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;
SetActorRotation(LoadGame->Rotation);
SetActorLocation(LoadGame->Location);
}
}