From 8a4646bdb3c8528c563e8d777462446c43c6d8a8 Mon Sep 17 00:00:00 2001 From: eyakm1 Date: Mon, 5 Jun 2023 14:00:14 +0300 Subject: [PATCH] Forcefully destroy sessions before connecting or creating new --- Source/TurnBasedTutorial/BattleMode/GameOverWidget.cpp | 2 +- Source/TurnBasedTutorial/BattleMode/GameOverWidget.h | 2 +- Source/TurnBasedTutorial/MainMenu/MainMenuWidget.cpp | 6 ++++++ Source/TurnBasedTutorial/MainMenu/SessionListWidget.cpp | 3 +++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Source/TurnBasedTutorial/BattleMode/GameOverWidget.cpp b/Source/TurnBasedTutorial/BattleMode/GameOverWidget.cpp index e6c1180..4e25a46 100644 --- a/Source/TurnBasedTutorial/BattleMode/GameOverWidget.cpp +++ b/Source/TurnBasedTutorial/BattleMode/GameOverWidget.cpp @@ -11,7 +11,7 @@ void UGameOverWidget::NativeConstruct() { ButtonToMenu->OnClicked.AddDynamic(this, &ThisClass::QuitCurrentSession); } -void UGameOverWidget::QuitCurrentSession_Implementation() { +void UGameOverWidget::QuitCurrentSession() { const UGameInstance *GameInstance = UGameplayStatics::GetGameInstance( GetWorld()); USessionsGameInstanceSubsystem *GameInstanceSubsystem = GameInstance->GetSubsystem diff --git a/Source/TurnBasedTutorial/BattleMode/GameOverWidget.h b/Source/TurnBasedTutorial/BattleMode/GameOverWidget.h index 2a28630..82402fe 100644 --- a/Source/TurnBasedTutorial/BattleMode/GameOverWidget.h +++ b/Source/TurnBasedTutorial/BattleMode/GameOverWidget.h @@ -19,7 +19,7 @@ public: UFUNCTION(Client, Reliable) void SetWidgetText(bool HasWon); - UFUNCTION(Client, Reliable) + UFUNCTION() void QuitCurrentSession(); protected: diff --git a/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.cpp b/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.cpp index 3a13569..0baf22f 100644 --- a/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.cpp +++ b/Source/TurnBasedTutorial/MainMenu/MainMenuWidget.cpp @@ -16,11 +16,17 @@ void UMainMenuWidget::NativeConstruct() { } void UMainMenuWidget::OnHostOnlineGameButtonClicked() { + // Ensure we have left any session + GetMyGameSubsystem()->DestroySession(); + GetMyGameSubsystem()->CreateSession( "Lobby " + FString::FromInt(FMath::RandRange(1, 1e6)), 2, true); } void UMainMenuWidget::StartSessionWhenCreatingSessonComplete(bool bSuccess) { + if (!bSuccess) { + return; + } GetMyGameSubsystem()->StartSession(); } diff --git a/Source/TurnBasedTutorial/MainMenu/SessionListWidget.cpp b/Source/TurnBasedTutorial/MainMenu/SessionListWidget.cpp index a6794f9..af32814 100644 --- a/Source/TurnBasedTutorial/MainMenu/SessionListWidget.cpp +++ b/Source/TurnBasedTutorial/MainMenu/SessionListWidget.cpp @@ -22,6 +22,9 @@ void USessionListWidget::NativeConstruct() { MyGameInstanceSubsystem->OnJoinSessionCompleteEvent.AddUObject( this, &ThisClass::OnJoinSessionSuccess); + // Ensure we have left any session + GetMyGameSubsystem()->DestroySession(); + // Initiate search MyGameInstanceSubsystem->FindSessions(10, true); }