Skip to content

Conditional serialization for savables #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#include "GameFramework/SaveGame/GBFSaveGame.h"

#include "GameFramework/SaveGame/GBFSaveGameSubsystem.h"

#include <Serialization/MemoryReader.h>
#include <Serialization/MemoryWriter.h>
#include <Serialization/ObjectAndNameAsStringProxyArchive.h>

namespace
{
void LoadSavable( FGBFSavableData & savable_data )
void TryLoadSavable( FGBFSavableData & savable_data )
{
if ( savable_data.Savable == nullptr )
{
return;
}

if ( !savable_data.Savable->CanBeSerialized() )
{
return;
}

FMemoryReader memory_reader( savable_data.Data );
FObjectAndNameAsStringProxyArchive archive( memory_reader, false );
archive.ArIsSaveGame = true;
Expand All @@ -27,6 +35,11 @@ void IGBFSaveGameSystemSavableInterface::OnSaveGameReset()
{
}

bool IGBFSaveGameSystemSavableInterface::CanBeSerialized() const
{
return true;
}

void UGBFSaveGame::HandlePreSave()
{
Super::HandlePreSave();
Expand All @@ -38,6 +51,11 @@ void UGBFSaveGame::HandlePreSave()
continue;
}

if ( !savable_data.Savable->CanBeSerialized() )
{
continue;
}

FMemoryWriter memory_writer( savable_data.Data );
FObjectAndNameAsStringProxyArchive archive( memory_writer, false );
archive.ArIsSaveGame = true;
Expand All @@ -52,12 +70,7 @@ void UGBFSaveGame::HandlePostLoad()

for ( auto & savable_data : SavablesData )
{
if ( savable_data.Savable == nullptr )
{
continue;
}

LoadSavable( savable_data );
TryLoadSavable( savable_data );
}
}

Expand Down Expand Up @@ -85,7 +98,7 @@ void UGBFSaveGame::RegisterSavable( TScriptInterface< IGBFSaveGameSystemSavableI
} ) )
{
savable_ptr->Savable = savable;
LoadSavable( *savable_ptr );
TryLoadSavable( *savable_ptr );
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class GAMEBASEFRAMEWORK_API IGBFSaveGameSystemSavableInterface
GENERATED_IINTERFACE_BODY()

virtual void OnSaveGameReset();
virtual bool CanBeSerialized() const;
};

USTRUCT()
Expand Down