diff --git a/Content/BattleField/BP_EndOfGameWidget.uasset b/Content/BattleField/BP_EndOfGameWidget.uasset index 91d6502..0cdda72 100644 Binary files a/Content/BattleField/BP_EndOfGameWidget.uasset and b/Content/BattleField/BP_EndOfGameWidget.uasset differ diff --git a/Source/TurnBasedTutorial/GameOverWidget.cpp b/Source/TurnBasedTutorial/GameOverWidget.cpp index 92afb95..c1110ed 100644 --- a/Source/TurnBasedTutorial/GameOverWidget.cpp +++ b/Source/TurnBasedTutorial/GameOverWidget.cpp @@ -1,13 +1,34 @@ // Fill out your copyright notice in the Description page of Project Settings. #include "GameOverWidget.h" + +#include "MyGameInstanceSubsystem.h" +#include "Components/Button.h" #include "Components/TextBlock.h" +#include "Kismet/GameplayStatics.h" + + +void UGameOverWidget::NativeConstruct() +{ + ButtonToMenu->OnClicked.AddDynamic(this, &ThisClass::QuitCurrentSession); +} + +void UGameOverWidget::QuitCurrentSession() +{ + const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(GetWorld()); + UMyGameInstanceSubsystem* GameInstanceSubsystem = GameInstance->GetSubsystem(); + GameInstanceSubsystem->QuitCurrentSession(); +} -void UGameOverWidget::SetWidgetText_Implementation(bool HasWon) { - if (HasWon) { - GameOverText->SetText(FText::FromString("You won!")); - } else { - GameOverText->SetText(FText::FromString("You lose!")); - } +void UGameOverWidget::SetWidgetText_Implementation(bool HasWon) +{ + if (HasWon) + { + GameOverText->SetText(FText::FromString("You won!")); + } + else + { + GameOverText->SetText(FText::FromString("You lose!")); + } } diff --git a/Source/TurnBasedTutorial/GameOverWidget.h b/Source/TurnBasedTutorial/GameOverWidget.h index 5bee377..113402b 100644 --- a/Source/TurnBasedTutorial/GameOverWidget.h +++ b/Source/TurnBasedTutorial/GameOverWidget.h @@ -14,9 +14,14 @@ class TURNBASEDTUTORIAL_API UGameOverWidget : public UUserWidget { GENERATED_BODY() public: + virtual void NativeConstruct() override; + UFUNCTION(Client, Reliable) void SetWidgetText(bool HasWon); + UFUNCTION() + void QuitCurrentSession(); + protected: UPROPERTY(meta = (BindWidget)) class UButton *ButtonToMenu; diff --git a/Source/TurnBasedTutorial/MyGameInstanceSubsystem.cpp b/Source/TurnBasedTutorial/MyGameInstanceSubsystem.cpp index 3976783..15a858e 100644 --- a/Source/TurnBasedTutorial/MyGameInstanceSubsystem.cpp +++ b/Source/TurnBasedTutorial/MyGameInstanceSubsystem.cpp @@ -2,7 +2,9 @@ #include "MyGameInstanceSubsystem.h" +#include "MyPlayerController.h" #include "OnlineSubsystemUtils.h" +#include "GameFramework/GameModeBase.h" #include "Kismet/GameplayStatics.h" UMyGameInstanceSubsystem::UMyGameInstanceSubsystem() : CreateSessionCompleteDelegate( @@ -25,12 +27,13 @@ UMyGameInstanceSubsystem::UMyGameInstanceSubsystem() : CreateSessionCompleteDele this, &ThisClass::OnFindSessionsCompleted)), JoinSessionCompleteDelegate( FOnJoinSessionCompleteDelegate::CreateUObject( - this, &ThisClass::OnJoinSessionCompleted)) + this, &ThisClass::OnJoinSessionCompleted)), + bIsHost(false) { } -void UMyGameInstanceSubsystem::CreateSession(int32 NumPublicConnections, bool bIsLANMatch) +void UMyGameInstanceSubsystem::CreateSession(FString SessionName, int32 NumPublicConnections, bool bIsLANMatch) { const IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(GetWorld()); if (!SessionInterface.IsValid()) @@ -50,7 +53,7 @@ void UMyGameInstanceSubsystem::CreateSession(int32 NumPublicConnections, bool bI LastSessionSettings->bIsLANMatch = bIsLANMatch; LastSessionSettings->bShouldAdvertise = true; - LastSessionSettings->Set(SETTING_MAPNAME, FString("Your Level Name"), + LastSessionSettings->Set(SETTING_MAPNAME, SessionName, EOnlineDataAdvertisementType::ViaOnlineService); CreateSessionCompleteDelegateHandle = SessionInterface->AddOnCreateSessionCompleteDelegate_Handle( @@ -74,6 +77,8 @@ void UMyGameInstanceSubsystem::OnCreateSessionCompleted(FName SessionName, bool SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle); } + bIsHost = true; + OnCreateSessionCompleteEvent.Broadcast(bSuccessful); } @@ -150,7 +155,7 @@ void UMyGameInstanceSubsystem::OnStartSessionCompleted(FName SessionName, bool b OnStartSessionCompleteEvent.Broadcast(bSuccessful); // TODO: Move this from gameinstance subsystem. This should not be here. - UGameplayStatics::OpenLevel(GetWorld(),"BattleFieldMap", true, "listen"); + UGameplayStatics::OpenLevel(GetWorld(), "BattleFieldMap", true, "listen"); } @@ -346,3 +351,18 @@ bool UMyGameInstanceSubsystem::TryConnectToCurrentSession() const PlayerController->ClientTravel(ConnectString, TRAVEL_Absolute); return true; } + +void UMyGameInstanceSubsystem::QuitCurrentSession() +{ + if (bIsHost) + { + UGameplayStatics::GetGameMode(GetWorld())->ReturnToMainMenuHost(); + } + else + { + APlayerController* LocalController = GEngine->GetFirstLocalPlayerController(GetWorld()); + LocalController->ClientReturnToMainMenuWithTextReason(FText::FromString("Session ended")); + } + bIsHost = false; + DestroySession(); +} diff --git a/Source/TurnBasedTutorial/MyGameInstanceSubsystem.h b/Source/TurnBasedTutorial/MyGameInstanceSubsystem.h index 10dbc5b..7a08655 100644 --- a/Source/TurnBasedTutorial/MyGameInstanceSubsystem.h +++ b/Source/TurnBasedTutorial/MyGameInstanceSubsystem.h @@ -34,7 +34,7 @@ class TURNBASEDTUTORIAL_API UMyGameInstanceSubsystem : public UGameInstanceSubsy public: UMyGameInstanceSubsystem(); - void CreateSession(int32 NumPublicConnections, bool bIsLANMatch); + void CreateSession(FString SessionName, int32 NumPublicConnections, bool bIsLANMatch); void UpdateSession(); @@ -51,6 +51,10 @@ public: void JoinSession(const int32 Index); bool TryConnectToCurrentSession() const; + + void QuitCurrentSession(); + + void UpdateSessionName(FString NewSessionName); FMyOnCreateSessionComplete OnCreateSessionCompleteEvent; FMyOnUpdateSessionComplete OnUpdateSessionCompleteEvent; @@ -76,6 +80,8 @@ protected: void OnJoinSessionCompleted(FName SessionName, EOnJoinSessionCompleteResult::Type Result); private: + bool bIsHost; + FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate; FDelegateHandle CreateSessionCompleteDelegateHandle; TSharedPtr LastSessionSettings; diff --git a/Source/TurnBasedTutorial/MyMainMenu.cpp b/Source/TurnBasedTutorial/MyMainMenu.cpp index a80d116..3b0d757 100644 --- a/Source/TurnBasedTutorial/MyMainMenu.cpp +++ b/Source/TurnBasedTutorial/MyMainMenu.cpp @@ -17,7 +17,7 @@ void UMyMainMenu::NativeConstruct() void UMyMainMenu::OnHostOnlineGameButtonClicked() { - GetMyGameSubsystem()->CreateSession(2, true); + GetMyGameSubsystem()->CreateSession("Lobby " + FString::FromInt(FMath::RandRange(1, 1e6)),2, true); } void UMyMainMenu::StartSessionWhenCreatingSessonComplete(bool bSuccess) diff --git a/Source/TurnBasedTutorial/MySessionListEntryWidget.cpp b/Source/TurnBasedTutorial/MySessionListEntryWidget.cpp index da10049..67c5200 100644 --- a/Source/TurnBasedTutorial/MySessionListEntryWidget.cpp +++ b/Source/TurnBasedTutorial/MySessionListEntryWidget.cpp @@ -11,8 +11,9 @@ void UMySessionListEntryWidget::Update(int SessionIndex, const FOnlineSessionSea { SessionId = SessionIndex; IndexText->SetText(FText::AsNumber(SessionIndex + 1)); - // TODO: SessionNameText->SetText(FText::FromString(Session.Session.SessionSettings.Get(...))) - SessionNameText->SetText(FText::FromString("Test session name")); + + Session.Session.SessionSettings.Get(SETTING_MAPNAME, SessionName); + SessionNameText->SetText(FText::FromString(SessionName)); int MaxPlayerCount = Session.Session.SessionSettings.NumPublicConnections; int CurPlayerCount = MaxPlayerCount - Session.Session.NumOpenPublicConnections;