Skip to content

Commit

Permalink
Fix for Session Settings not syncing (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
mklierman authored Jun 22, 2024
1 parent 6bf0052 commit b462bf7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "FGGameMode.h"
#include "FGPlayerController.h"
#include "Subsystem/SubsystemActorManager.h"
#include "Settings/FGUserSettingApplyType.h"
#include "Net/UnrealNetwork.h"

ASessionSettingsSubsystem::ASessionSettingsSubsystem() {
ReplicationPolicy = ESubsystemReplicationPolicy::SpawnOnServer_Replicate;
Expand All @@ -11,6 +13,14 @@ void ASessionSettingsSubsystem::Init() {
USessionSettingsManager* SessionSettingsManager = GetWorld()->GetSubsystem<USessionSettingsManager>();
check(SessionSettingsManager);

if (!HasAuthority())
{
// We're a client, so we only have our own player controller
AFGPlayerController* PlayerController = Cast<AFGPlayerController>(GetWorld()->GetFirstPlayerController());
USMLSessionSettingsRemoteCallObject* RCO = PlayerController->GetRemoteCallObjectOfClass<USMLSessionSettingsRemoteCallObject>();
RCO->Server_RequestAllSessionSettings();
}

OnOptionUpdatedDelegate = FOnOptionUpdated::CreateUObject(this, &ASessionSettingsSubsystem::OnSessionSettingUpdated);
SessionSettingsManager->SubscribeToAllOptionUpdates(OnOptionUpdatedDelegate);
}
Expand Down Expand Up @@ -65,3 +75,35 @@ bool USMLSessionSettingsRemoteCallObject::Server_RequestSessionSettingUpdate_Val

return SessionSettingsManager != NULL && SessionSettingsManager->FindSessionSetting(SessionSettingName) != NULL;
}

void USMLSessionSettingsRemoteCallObject::Server_RequestAllSessionSettings_Implementation()
{
USessionSettingsManager* SessionSettingsManager = GetWorld()->GetSubsystem<USessionSettingsManager>();
TMap<FString, UFGUserSettingApplyType*> Settings = SessionSettingsManager->GetAllSessionSettings();
for (TPair<FString, UFGUserSettingApplyType*> Setting : Settings)
{
FVariant AppliedValue = Setting.Value->GetAppliedValue();
FString ValueString = USessionSettingsManager::VariantToString(AppliedValue);
Client_SendSessionSetting(Setting.Key, ValueString);
}
}

bool USMLSessionSettingsRemoteCallObject::Server_RequestAllSessionSettings_Validate()
{
USessionSettingsManager* SessionSettingsManager = GetWorld()->GetSubsystem<USessionSettingsManager>();
return SessionSettingsManager != NULL;
}

void USMLSessionSettingsRemoteCallObject::Client_SendSessionSetting_Implementation(const FString& SessionSettingName, const FString& ValueString)
{
ASessionSettingsSubsystem* SessionSettingsSubsystem = ASessionSettingsSubsystem::Get(GetWorld());
check(SessionSettingsSubsystem);

SessionSettingsSubsystem->PushSettingToSessionSettings(SessionSettingName, USessionSettingsManager::StringToVariant(ValueString));
}

bool USMLSessionSettingsRemoteCallObject::Client_SendSessionSetting_Validate(const FString& SessionSettingName, const FString& ValueString)
{
const USessionSettingsManager* SessionSettingsManager = GetWorld()->GetSubsystem<USessionSettingsManager>();
return SessionSettingsManager != NULL && !SessionSettingName.IsEmpty() && !ValueString.IsEmpty();
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class SML_API USessionSettingsManager : public UWorldSubsystem, public IFGOption
void SubscribeToAllOptionUpdates(const FOnOptionUpdated& onOptionUpdatedDelegate);
void UnsubscribeToAllOptionUpdates(const FOnOptionUpdated& onOptionUpdatedDelegate);

TMap<FString, UFGUserSettingApplyType*> GetAllSessionSettings() { return SessionSettings; };

UFUNCTION(BlueprintCallable, Category = "Session Settings Manager")
void InitializeForMap(const TSoftObjectPtr<UWorld>& World, bool bAttemptPreserveValues);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class SML_API ASessionSettingsSubsystem : public AModSubsystem {

virtual void Init() override;

// TODO: Use this to sync the settings to the client on join

static ASessionSettingsSubsystem* Get(UWorld* World);

void OnSessionSettingUpdated(const FString StrID, FVariant value);
Expand All @@ -36,6 +34,13 @@ class SML_API USMLSessionSettingsRemoteCallObject : public UFGRemoteCallObject {

UFUNCTION(Server, Reliable, WithValidation)
void Server_RequestSessionSettingUpdate(const FString& SessionSettingName, const FString& ValueString);

UFUNCTION(Server, Reliable, WithValidation)
void Server_RequestAllSessionSettings();

UFUNCTION(Client, Reliable, WithValidation)
void Client_SendSessionSetting(const FString& SessionSettingName, const FString& ValueString);

private:
UPROPERTY(Replicated)
bool mForceNetField_USMLSessionSettingsRemoteCallObject;
Expand Down

0 comments on commit b462bf7

Please sign in to comment.