Save Game causing crash

Hi all, so I added a save game c++ class in my game to save and load my high score. It works perfectly fine but whenever I try to launch my game or package it I get a fatal error that says failed to read HighScoreSaveSlot.sav. I also get this in the output log in red.

LogPlayLevel: Error: [2020.07.16-08.46.28:736] 0]LogWindows: Error: begin: stack for UAT
LogPlayLevel: Error: [2020.07.16-08.46.28:736] 0]LogWindows: Error: === Critical error: ===
LogPlayLevel: Error: [2020.07.16-08.46.28:736] 0]LogWindows: Error:
LogPlayLevel: Error: [2020.07.16-08.46.28:736] 0]LogWindows: Error: Fatal error!
LogPlayLevel: Error: [2020.07.16-08.46.28:736] 0]LogWindows: Error:
LogPlayLevel: Error: [2020.07.16-08.46.28:736] 0]LogWindows: Error:
LogPlayLevel: Error: [2020.07.16-08.46.28:736] 0]LogWindows: Error: end: stack for UAT
LogPlayLevel: Error: ERROR: Client exited with error code: 3

Also, if it helps, this is the code I used to save the high score:
void AInGameHUD::ResetHighScore()
{
UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
SaveGameInstance->HighScore = 0;
UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveSlotName, UserIndex);
}

void AInGameHUD::OnPlayerDeath_Implementation()
{
bUpdateScore = false;

// Save the Player’s High Score
UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass())); // Cast to MySaveGame class

if (SaveGameInstance->HighScore < CurrentScore) {
SaveGameInstance->HighScore = CurrentScore;
UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveSlotName, UserIndex);
}
}
Any help is much appreciated, thank you :slight_smile:

You gotta format that as code if you want people to read it my guy

Ok here :slight_smile:


void AInGameHUD::ResetHighScore()
{
    UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
    SaveGameInstance->HighScore = 0;
    UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveSlotName, UserIndex);
}

void AInGameHUD::OnPlayerDeath_Implementation()
{
    bUpdateScore = false;

    // Save the Player's High Score
    UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass())); // Cast to MySaveGame class

    if (SaveGameInstance->HighScore < CurrentScore) {
        SaveGameInstance->HighScore = CurrentScore;
        UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveSlotName, UserIndex); }
}

@anikataylor_ could you post the entire log when the game crashes? Your log in red does not show anything related to SaveGame.

Also, you’ll want to capture the actual stack trace, at least the first (top) few functions, to know where it’s crashing.
You could also just attach to the process with Visual Studio (assuming you install the optional debug symbols in the engine install screen in the epic manager) and set a breakpoint in the functions you call when saving. Then you should be able to step to the crash, and see what’s going on.

Okay so it gives me these warnings in the log:
LogPlayLevel: Warning: [2020.07.24-12.39.06:886] 0]LogStreaming: Warning: Failed to read file ‘…/…/…/DontStop/Saved/SaveGames/HighScoreSaveSlot.sav’ error.
LogPlayLevel: Warning: [2020.07.24-12.40.06:887] 0]LogWindows: Warning: CreateProc failed: The system cannot find the file specified. (0x00000002)
LogPlayLevel: Warning: [2020.07.24-12.40.06:887] 0]LogWindows: Warning: URL: …/…/…/Engine/Binaries/Win64/CrashReportClient.exe “C:/Unreal Projects/DontStop/Saved/StagedBuilds/WindowsNoEditor/DontStop/Saved/Crashes/UE4CC-Windows-BA120FF040F527338259E3986E9C85D9_0000” -AppName=UE4-DontStop -CrashGUID=UE4CC-Windows-BA120FF040F52733
8259E3986E9C85D9_0000 -DebugSymbols=…....\Engine\Intermediate\Symbols

And straight after if gives me these same errors:
LogPlayLevel: Error: [2020.07.24-12.40.06:887] 0]LogWindows: Error: begin: stack for UAT
LogPlayLevel: Error: [2020.07.24-12.40.06:887] 0]LogWindows: Error: === Critical error: ===
LogPlayLevel: Error: [2020.07.24-12.40.06:887] 0]LogWindows: Error:
LogPlayLevel: Error: [2020.07.24-12.40.06:887] 0]LogWindows: Error: Fatal error!
LogPlayLevel: Error: [2020.07.24-12.40.06:887] 0]LogWindows: Error:
LogPlayLevel: Error: [2020.07.24-12.40.06:887] 0]LogWindows: Error:
LogPlayLevel: Error: [2020.07.24-12.40.06:887] 0]LogWindows: Error: end: stack for UAT
LogPlayLevel: Error: ERROR: Client exited with error code: 3
LogPlayLevel: AutomationTool exiting with ExitCode=1 (Error_Unknown)
LogPlayLevel: Completed Launch On Stage: Run Task, Time: 62.941801
LogPlayLevel: BUILD FAILED
PackagingResults: Error: Launch failed! Unknown Error

Also when I try to launch the project I get a box that says Fatal Error! and then it closes. Maybe it isn’t to do with the save game but I just can’t think of what else it could be because everything works perfectly fine in my game.

I’m not entirely sure what you mean as I am still fairly new to ue4 would you mind explaining what the stack trace is?

The stack trace is the part of the debugging where the call stack is unwound – “function A was called by function B was called by function C was called …”

If you are new to C++ programming, starting C++ programming with the Unreal Engine as your first project is probably not wise, as there are many things to learn about Unreal; trying to learn C++_ development and debugging at the same time is like trying to learn calculus from a textbook written in Latin when you don’t yet know Latin. Not recommended!

@anikataylor_ are you calling Parent function in your blueprint for OnPlayerDeath?

I think you could change:


void AInGameHUD::OnPlayerDeath_Implementation()
{
bUpdateScore = false;

// Save the Player's High Score
UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass())); // Cast to MySaveGame class

if (SaveGameInstance->HighScore < CurrentScore)
{
SaveGameInstance->HighScore = CurrentScore;
UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveSlotName, UserIndex);
}
}

To:


void AInGameHUD::OnPlayerDeath_Implementation()
{
bUpdateScore = false;

**if (UGameplayStatics::DoesSaveGameExist(SaveSlotName, UserIndex))
{
if (UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::LoadGameFromSlot(SaveSlotName, UserIndex)))
{
if (SaveGameInstance->HighScore < CurrentScore)
{
SaveGameInstance->HighScore = CurrentScore;
UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveSlotName, UserIndex);
}
}
}**
else
{
// Save the Player's High Score
UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass())); // Cast to MySaveGame class

if (SaveGameInstance->HighScore < CurrentScore)
{
SaveGameInstance->HighScore = CurrentScore;
UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveSlotName, UserIndex);
}
}
}

You are not loading your SaveGame:

Thank you so much, my game now doesn’t crash!

Ok thanks for the advice I’ll go back to c++ for a while before I carry on with ue4