Skip to content

Use FFrequencyThrottler to correctly check for save / load frequencies #382

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
May 16, 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,6 +1,5 @@
#include "GameFramework/SaveGame/GBFSaveGameSubsystem.h"

#include "Async/Async.h"
#include "GBFTags.h"
#include "GameFramework/GBFWorldSettings.h"
#include "GameFramework/SaveGame/GBFSaveGame.h"
Expand Down Expand Up @@ -39,6 +38,15 @@ namespace
}
}

void UGBFSaveGameSubsystem::Initialize( FSubsystemCollectionBase & collection )
{
Super::Initialize( collection );

auto * settings = GetDefault< UGBFSaveGameSettings >();
SaveGameFrequencyThrottler.Reset( settings->MaxSaveFrequency, settings->MaxSaveFrequencyDuration );
LoadGameFrequencyThrottler.Reset( settings->MaxLoadFrequency, settings->MaxLoadFrequencyDuration );
}

void UGBFSaveGameSubsystem::NotifyPlayerAdded( ULocalPlayer * local_player )
{
if ( PrimaryPlayer == nullptr )
Expand Down Expand Up @@ -67,17 +75,16 @@ void UGBFSaveGameSubsystem::Load( FGBFOnSaveGameLoaded on_save_game_loaded )
return;
}

const auto current_time = GetWorld()->GetTimeSeconds();
auto * settings = GetDefault< UGBFSaveGameSettings >();

UE_LOG( LogGBFSaveGameSystem, VeryVerbose, TEXT( "Frequency check before loading" ) );
if ( !IsFrequencyRespected( LoadGameCallTimes, current_time, settings->MaxLoadFrequencyDuration, settings->MaxLoadFrequency ) )
if ( !LoadGameFrequencyThrottler.RecordEvent() )
{
UE_LOG( LogGBFSaveGameSystem, Warning, TEXT( "Too much calls to Load. Max calls : %i in %f seconds" ), settings->MaxLoadFrequency, settings->MaxLoadFrequencyDuration );
on_save_game_loaded.ExecuteIfBound( SaveGame, false );
return;
}
UE_LOG( LogGBFSaveGameSystem, VeryVerbose, TEXT( "Frequency check successful. Loaded %i times for the last %lld seconds" ), LoadGameCallTimes.Num(), FMath::RoundToInt( current_time - LoadGameCallTimes.First() ) );
UE_LOG( LogGBFSaveGameSystem, VeryVerbose, TEXT( "Frequency check successful. Loaded %i times for the last %i seconds" ), LoadGameFrequencyThrottler.GetEventCount(), FMath::RoundToInt( LoadGameFrequencyThrottler.GetTimeBetweenFirstAndLastEvents() ) );

if ( SaveGame != nullptr )
{
Expand Down Expand Up @@ -164,17 +171,16 @@ void UGBFSaveGameSubsystem::Save( FGBFOnSaveGameSaved on_save_game_saved )
return;
}

const auto current_time = GetWorld()->GetTimeSeconds();
auto * settings = GetDefault< UGBFSaveGameSettings >();

UE_LOG( LogGBFSaveGameSystem, VeryVerbose, TEXT( "Frequency check before saving" ) );
if ( !IsFrequencyRespected( SaveGameCallTimes, current_time, settings->MaxSaveFrequencyDuration, settings->MaxSaveFrequency ) )
if ( !SaveGameFrequencyThrottler.RecordEvent() )
{
UE_LOG( LogGBFSaveGameSystem, Warning, TEXT( "Failed to save : Too much calls to Save. Max calls : %i in %f seconds" ), settings->MaxSaveFrequency, settings->MaxSaveFrequencyDuration );
on_save_game_saved.ExecuteIfBound( SaveGame, false );
return;
}
UE_LOG( LogGBFSaveGameSystem, VeryVerbose, TEXT( "Frequency check successful. Saved %i times for the last %lld seconds" ), SaveGameCallTimes.Num(), FMath::RoundToInt( current_time - SaveGameCallTimes.First() ) );
UE_LOG( LogGBFSaveGameSystem, VeryVerbose, TEXT( "Frequency check successful. Saved %i times for the last %i seconds" ), SaveGameFrequencyThrottler.GetEventCount(), FMath::RoundToInt( SaveGameFrequencyThrottler.GetTimeBetweenFirstAndLastEvents() ) );

OnOperationTriggeredDelegate.Broadcast( EGBFSaveGameSubsystemOperation::Save, EGBFSaveGameSubsystemOperationEvent::Started );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Containers/Deque.h"
#include "FrequencyThrottler.h"
#include "GBFSaveGame.h"

#include <CoreMinimal.h>
Expand Down Expand Up @@ -34,6 +35,8 @@ class GAMEBASEFRAMEWORK_API UGBFSaveGameSubsystem : public UGameInstanceSubsyste
public:
FGBFOnOperationTriggeredDelegate & OnOperationTriggered();

void Initialize( FSubsystemCollectionBase & collection ) override;

void NotifyPlayerAdded( ULocalPlayer * local_player );

UFUNCTION( BlueprintCallable )
Expand Down Expand Up @@ -71,8 +74,8 @@ class GAMEBASEFRAMEWORK_API UGBFSaveGameSubsystem : public UGameInstanceSubsyste

TWeakObjectPtr< ULocalPlayer > PrimaryPlayer;

TDeque< float > LoadGameCallTimes;
TDeque< float > SaveGameCallTimes;
FFrequencyThrottler SaveGameFrequencyThrottler;
FFrequencyThrottler LoadGameFrequencyThrottler;
};

template < typename _SAVE_GAME_CLASS_ >
Expand Down