SaveGameToSlot and/or LoadGameFromSlot not working. Why?

After a lot of frustrating failures in my project, I created a small project to test saving and loading. In the player controller’s BeginPlay I have this:

	UTestSaveGame* NewSave = Cast<UTestSaveGame>(UGameplayStatics::CreateSaveGameObject(UTestSaveGame::StaticClass()));
	NewSave->TestString = "Save me!";
	NewSave->TestInt = 42;
	UGameplayStatics::SaveGameToSlot(NewSave, "Slot1", 1);
	UE_LOG(LogTemp, Log, TEXT("TestString:  %s"), *NewSave->TestString);
	UE_LOG(LogTemp, Log, TEXT("TestInt:  %d"), NewSave->TestInt);

Then I call this:

void ATestPlayerController::TryLoading()
{
	UTestSaveGame* ReloadedSave = Cast<UTestSaveGame>(UGameplayStatics::CreateSaveGameObject(UTestSaveGame::StaticClass()));
	ReloadedSave = Cast<UTestSaveGame>(UGameplayStatics::LoadGameFromSlot("Slot1", 1));
	UE_LOG(LogTemp, Log, TEXT("TestString:  %s"), *ReloadedSave->TestString);
	UE_LOG(LogTemp, Log, TEXT("TestInt:  %d"), ReloadedSave->TestInt);
}

Here are the pertinent lines from the log:

[2018.11.12-07.43.16:971][  0]LogTemp: TestString:  Save me!
[2018.11.12-07.43.16:973][  0]LogTemp: TestInt:  42
[2018.11.12-07.43.20:432][206]LogTemp: TestString:
[2018.11.12-07.43.20:432][206]LogTemp: TestInt:  0

I create a new instance of a custom SaveGame, I set a couple class variables, and save. Then I load up an instance of it, and the information is gone. What’s going on?

Turns out I had to declare the variables in question with

 UPROPERTY(VisibleAnywhere, Category = Basic)
1 Like

Thanks for your reply! You actually only need UPROPERTY() by the way.

1 Like