Thank you, i had the same issue. It was indeed the way to go, nicely done!
Small correction: You don’t have to recreate a new USaveGame each time, but you have to reload it each time you want to modify it.
In my case, i did a GetLibrary functions that does the following:
#define MYSAVELIB_SLOT FString("M_SAVE_LIB")
....
UMySaveLib* ULibrary::get_library()
{
if (UGameplayStatics::DoesSaveGameExist(MYSAVELIB_SLOT, 0))
{
return Cast<UMySaveLib>(UGameplayStatics::LoadGameFromSlot(MYSAVELIB_SLOT, 0));
}
return nullptr;
}
void ULibrary::load_library()
{
UMySaveLib* lib = get_library();
if (!lib)
{
lib = Cast<UMySaveLib>(UGameplayStatics::CreateSaveGameObject(UMySaveLib::StaticClass()));
lib->creation = FDateTime::Now().ToUnixTimestamp();
save_library(lib);
}
}
bool ULibrary::save_library(UMySaveLib* lib)
{
if (lib == nullptr)
{
return false;
}
lib->modified = FDateTime::Now().ToUnixTimestamp();
// and now overwriting
return UGameplayStatics::SaveGameToSlot(lib, MYSAVELIB_SLOT, 0);
}
You have to call load_library at least once at object initialisation for instance. After that, you can access the UMySaveLib by calling get_library() and NOT store the pointer outiside of the function body.