Save game not working.

I’m trying to save and load using c++, I want to store a integer, in this case 23 just for the test, I Load the game from slot, cast it to my custom SaveGame class, change the variable value to 23 and then I SaveGameToSlot. But the variable is never saved, and every time I load the game from slot the integer have the default value, 0.

I’m doing something wrong? The same function, “SaveGame” is called everytime I press an UMG button, and everytime it loads the game from “Slot1” with it’s default values.

void UQuiz::SaveGame()
{
	if (!UGameplayStatics::DoesSaveGameExist("Slot1",0))
	{
UGameplayStatics::SaveGameToSlot(UGameplayStatics::CreateSaveGameObject(UQuizSaveGame::StaticClass()), "Slot1", 0);
	}

	UQuizSaveGame* SaveGame= Cast<UQuizSaveGame>(UGameplayStatics::LoadGameFromSlot("Slot1", 0));


	SaveGame->TestInt = 23;

	UGameplayStatics::SaveGameToSlot(SaveGame, "Slot1",0);
}

Sorry for my bad english, still learning. Thanks for the help.

Found the solution!! Just mark all the variables you want to save with UPROPERTY(), otherwise it’ll not work.

If you are saving USTRUCTS, remember to mark all the members as UPROPERTY too.

To be precise it should be UPROPERTY(SaveGame) for it to be marked as a saved property.

1 Like