Skip to content

Save system various fixes #362

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 5 commits into from
Mar 24, 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
Expand Up @@ -3,6 +3,7 @@
#include "GameFramework/SaveGame/GBFSaveGameSubsystem.h"

#include <Engine/LocalPlayer.h>
#include <GameFramework/Character.h>

void UGBFSavableLocalPlayerSubsystem::Initialize( FSubsystemCollectionBase & collection )
{
Expand All @@ -22,4 +23,30 @@ void UGBFSavableLocalPlayerSubsystem::Deinitialize()
}

Super::Deinitialize();
}

void UGBFSavableLocalPlayerSubsystem::PlayerControllerChanged( APlayerController * new_player_controller )
{
Super::PlayerControllerChanged( new_player_controller );

if ( auto * save_system = UGBFSaveGameSubsystem::Get( GetLocalPlayer< ULocalPlayer >() ) )
{
save_system->RegisterSavable( this );
}

new_player_controller->OnPossessedPawnChanged.AddUniqueDynamic( this, &ThisClass::OnPlayerControllerPossessedPawnChanged );

if ( auto * pawn = new_player_controller->GetCharacter() )
{
OnPawnChanged( pawn );
}
}

void UGBFSavableLocalPlayerSubsystem::OnPlayerControllerPossessedPawnChanged( APawn * /*old_pawn*/, APawn * new_pawn )
{
OnPawnChanged( new_pawn );
}

void UGBFSavableLocalPlayerSubsystem::OnPawnChanged( APawn * /*pawn*/ )
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ bool IGBFSaveGameSystemSavableInterface::CanBeSerialized() const
return true;
}

void FGBFSavableData::Reset()
{
Savable->OnSaveGameReset();
Data.Reset();
}

void UGBFSaveGame::HandlePreSave()
{
Super::HandlePreSave();
Expand Down Expand Up @@ -82,7 +88,7 @@ void UGBFSaveGame::ResetToDefault()

for ( auto & savable_data : SavablesData )
{
savable_data.Data.Reset();
savable_data.Reset();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ void UGBFSaveGameSubsystem::Load()

auto * settings = GetDefault< UGameBaseFrameworkGameSettings >();

if ( SaveGame != nullptr )
{
for ( const auto & savable_data : SaveGame->SavablesData )
{
PendingSavables.Add( savable_data.Savable );
}
}

SaveGame = Cast< UGBFSaveGame >( UGBFSaveGame::LoadOrCreateSaveGameForLocalPlayer( settings->SaveGameClass, PrimaryPlayer.Get(), settings->SaveGameSlotName ) );

for ( const auto & pending_savable : PendingSavables )
Expand All @@ -48,6 +56,14 @@ void UGBFSaveGameSubsystem::Save()
}
}

void UGBFSaveGameSubsystem::Reset()
{
if ( SaveGame != nullptr )
{
SaveGame->ResetToDefault();
}
}

void UGBFSaveGameSubsystem::RegisterSavable( const TScriptInterface< IGBFSaveGameSystemSavableInterface > & savable )
{
if ( SaveGame != nullptr )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@

#include "GBFSavableLocalPlayerSubsystem.generated.h"

UCLASS()
UCLASS( abstract )
class GAMEBASEFRAMEWORK_API UGBFSavableLocalPlayerSubsystem : public ULocalPlayerSubsystem, public IGBFSaveGameSystemSavableInterface
{
GENERATED_BODY()

public:
void Initialize( FSubsystemCollectionBase & collection ) override;
void Deinitialize() override;
void PlayerControllerChanged( APlayerController * new_player_controller ) override;

protected:
virtual void OnPawnChanged( APawn * pawn );

private:
UFUNCTION()
void OnPlayerControllerPossessedPawnChanged( APawn * old_pawn, APawn * new_pawn );
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "GBFSavableWorldSubsystem.generated.h"

UCLASS()
UCLASS( abstract )
class GAMEBASEFRAMEWORK_API UGBFSavableWorldSubsystem : public UWorldSubsystem, public IGBFSaveGameSystemSavableInterface
{
GENERATED_BODY()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ struct FGBFSavableData
{
}

void Reset();

UPROPERTY( Transient )
TScriptInterface< IGBFSaveGameSystemSavableInterface > Savable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class GAMEBASEFRAMEWORK_API UGBFSaveGameSubsystem : public UGameInstanceSubsyste
UFUNCTION( BlueprintCallable )
void Save();

UFUNCTION( BlueprintCallable )
void Reset();

void RegisterSavable( const TScriptInterface< IGBFSaveGameSystemSavableInterface > & savable );
void UnRegisterSavable( const TScriptInterface< IGBFSaveGameSystemSavableInterface > & savable );

Expand Down