I’m trying to make a simple saving and loading system and have been following the Docs for help. I have the following code:
// Declaration in the .h file
UCustomSave* saveGame;
.cpp file
// Loads the topology from a struct that can be saved to a save game object.
void UNeuralNetwork::Load(FString customSlotName) {
FString test;
if (UGameplayStatics::DoesSaveGameExist(customSlotName, 0)) {
UE_LOG(LogTemp, Warning, TEXT("Save game exists in slot: %s"), *customSlotName);
// Retrieve and cast the USaveGame object to UCustomSave.
this->saveGame= Cast<UCustomSave>(UGameplayStatics::LoadGameFromSlot(customSlotName, 0));
test = this->saveGame->prevSaved ? "true" : "false";
// The operation was successful, so LoadedGame now contains the data we saved earlier.
UE_LOG(LogTemp, Warning, TEXT("Has been Previously Saved? %s"), *test);
}
}
void UNeuralNetwork::Save(FString customSlotName) {
FString test;
if (this->saveGame== nullptr) {
this->saveGame= Cast<UCustomSave>(UGameplayStatics::CreateSaveGameObject(UCustomSave::StaticClass()));
}
// Set data on the savegame object.
this->saveGame->prevSaved = true;
// Save the data immediately.
if (UGameplayStatics::SaveGameToSlot(this->saveGame, customSlotName, 0))
{
UE_LOG(LogTemp, Warning, TEXT("Save was Successful in slot: %s"), *customSlotName);
}
}
I’m using prevSaved as a boolean that will be set on the .h file of the save game object UCustomSave as false. This is purely for testing, but it is always the default value.
I get the following results after calling Save and then Load:
LogTemp: Warning: Save was Successful in slot: Test
LogTemp: Warning: Save game exists in slot: Test
LogTemp: Warning: Has been Previously Saved? true
I’m not sure where I am going wrong so any help would be much appreciated