Destroy session on game end, random sessions names

pull/6/head
eyakm1 2 years ago
parent 024fb31ea1
commit efa99f3fd7

@ -1,13 +1,34 @@
// Fill out your copyright notice in the Description page of Project Settings. // Fill out your copyright notice in the Description page of Project Settings.
#include "GameOverWidget.h" #include "GameOverWidget.h"
#include "MyGameInstanceSubsystem.h"
#include "Components/Button.h"
#include "Components/TextBlock.h" #include "Components/TextBlock.h"
#include "Kismet/GameplayStatics.h"
void UGameOverWidget::NativeConstruct()
{
ButtonToMenu->OnClicked.AddDynamic(this, &ThisClass::QuitCurrentSession);
}
void UGameOverWidget::SetWidgetText_Implementation(bool HasWon) { void UGameOverWidget::QuitCurrentSession()
if (HasWon) { {
const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(GetWorld());
UMyGameInstanceSubsystem* GameInstanceSubsystem = GameInstance->GetSubsystem<UMyGameInstanceSubsystem>();
GameInstanceSubsystem->QuitCurrentSession();
}
void UGameOverWidget::SetWidgetText_Implementation(bool HasWon)
{
if (HasWon)
{
GameOverText->SetText(FText::FromString("You won!")); GameOverText->SetText(FText::FromString("You won!"));
} else { }
else
{
GameOverText->SetText(FText::FromString("You lose!")); GameOverText->SetText(FText::FromString("You lose!"));
} }
} }

@ -14,9 +14,14 @@ class TURNBASEDTUTORIAL_API UGameOverWidget : public UUserWidget {
GENERATED_BODY() GENERATED_BODY()
public: public:
virtual void NativeConstruct() override;
UFUNCTION(Client, Reliable) UFUNCTION(Client, Reliable)
void SetWidgetText(bool HasWon); void SetWidgetText(bool HasWon);
UFUNCTION()
void QuitCurrentSession();
protected: protected:
UPROPERTY(meta = (BindWidget)) UPROPERTY(meta = (BindWidget))
class UButton *ButtonToMenu; class UButton *ButtonToMenu;

@ -2,7 +2,9 @@
#include "MyGameInstanceSubsystem.h" #include "MyGameInstanceSubsystem.h"
#include "MyPlayerController.h"
#include "OnlineSubsystemUtils.h" #include "OnlineSubsystemUtils.h"
#include "GameFramework/GameModeBase.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
UMyGameInstanceSubsystem::UMyGameInstanceSubsystem() : CreateSessionCompleteDelegate( UMyGameInstanceSubsystem::UMyGameInstanceSubsystem() : CreateSessionCompleteDelegate(
@ -25,12 +27,13 @@ UMyGameInstanceSubsystem::UMyGameInstanceSubsystem() : CreateSessionCompleteDele
this, &ThisClass::OnFindSessionsCompleted)), this, &ThisClass::OnFindSessionsCompleted)),
JoinSessionCompleteDelegate( JoinSessionCompleteDelegate(
FOnJoinSessionCompleteDelegate::CreateUObject( 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()); const IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(GetWorld());
if (!SessionInterface.IsValid()) if (!SessionInterface.IsValid())
@ -50,7 +53,7 @@ void UMyGameInstanceSubsystem::CreateSession(int32 NumPublicConnections, bool bI
LastSessionSettings->bIsLANMatch = bIsLANMatch; LastSessionSettings->bIsLANMatch = bIsLANMatch;
LastSessionSettings->bShouldAdvertise = true; LastSessionSettings->bShouldAdvertise = true;
LastSessionSettings->Set(SETTING_MAPNAME, FString("Your Level Name"), LastSessionSettings->Set(SETTING_MAPNAME, SessionName,
EOnlineDataAdvertisementType::ViaOnlineService); EOnlineDataAdvertisementType::ViaOnlineService);
CreateSessionCompleteDelegateHandle = SessionInterface->AddOnCreateSessionCompleteDelegate_Handle( CreateSessionCompleteDelegateHandle = SessionInterface->AddOnCreateSessionCompleteDelegate_Handle(
@ -74,6 +77,8 @@ void UMyGameInstanceSubsystem::OnCreateSessionCompleted(FName SessionName, bool
SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle); SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle);
} }
bIsHost = true;
OnCreateSessionCompleteEvent.Broadcast(bSuccessful); OnCreateSessionCompleteEvent.Broadcast(bSuccessful);
} }
@ -150,7 +155,7 @@ void UMyGameInstanceSubsystem::OnStartSessionCompleted(FName SessionName, bool b
OnStartSessionCompleteEvent.Broadcast(bSuccessful); OnStartSessionCompleteEvent.Broadcast(bSuccessful);
// TODO: Move this from gameinstance subsystem. This should not be here. // 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); PlayerController->ClientTravel(ConnectString, TRAVEL_Absolute);
return true; 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();
}

@ -34,7 +34,7 @@ class TURNBASEDTUTORIAL_API UMyGameInstanceSubsystem : public UGameInstanceSubsy
public: public:
UMyGameInstanceSubsystem(); UMyGameInstanceSubsystem();
void CreateSession(int32 NumPublicConnections, bool bIsLANMatch); void CreateSession(FString SessionName, int32 NumPublicConnections, bool bIsLANMatch);
void UpdateSession(); void UpdateSession();
@ -52,6 +52,10 @@ public:
bool TryConnectToCurrentSession() const; bool TryConnectToCurrentSession() const;
void QuitCurrentSession();
void UpdateSessionName(FString NewSessionName);
FMyOnCreateSessionComplete OnCreateSessionCompleteEvent; FMyOnCreateSessionComplete OnCreateSessionCompleteEvent;
FMyOnUpdateSessionComplete OnUpdateSessionCompleteEvent; FMyOnUpdateSessionComplete OnUpdateSessionCompleteEvent;
FMyOnStartSessionCompete OnStartSessionCompleteEvent; FMyOnStartSessionCompete OnStartSessionCompleteEvent;
@ -76,6 +80,8 @@ protected:
void OnJoinSessionCompleted(FName SessionName, EOnJoinSessionCompleteResult::Type Result); void OnJoinSessionCompleted(FName SessionName, EOnJoinSessionCompleteResult::Type Result);
private: private:
bool bIsHost;
FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate; FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate;
FDelegateHandle CreateSessionCompleteDelegateHandle; FDelegateHandle CreateSessionCompleteDelegateHandle;
TSharedPtr<FOnlineSessionSettings> LastSessionSettings; TSharedPtr<FOnlineSessionSettings> LastSessionSettings;

@ -17,7 +17,7 @@ void UMyMainMenu::NativeConstruct()
void UMyMainMenu::OnHostOnlineGameButtonClicked() void UMyMainMenu::OnHostOnlineGameButtonClicked()
{ {
GetMyGameSubsystem()->CreateSession(2, true); GetMyGameSubsystem()->CreateSession("Lobby " + FString::FromInt(FMath::RandRange(1, 1e6)),2, true);
} }
void UMyMainMenu::StartSessionWhenCreatingSessonComplete(bool bSuccess) void UMyMainMenu::StartSessionWhenCreatingSessonComplete(bool bSuccess)

@ -11,8 +11,9 @@ void UMySessionListEntryWidget::Update(int SessionIndex, const FOnlineSessionSea
{ {
SessionId = SessionIndex; SessionId = SessionIndex;
IndexText->SetText(FText::AsNumber(SessionIndex + 1)); 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 MaxPlayerCount = Session.Session.SessionSettings.NumPublicConnections;
int CurPlayerCount = MaxPlayerCount - Session.Session.NumOpenPublicConnections; int CurPlayerCount = MaxPlayerCount - Session.Session.NumOpenPublicConnections;

Loading…
Cancel
Save