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.
#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::SetWidgetText_Implementation(bool HasWon) {
if (HasWon) {
void UGameOverWidget::QuitCurrentSession()
{
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!"));
} else {
}
else
{
GameOverText->SetText(FText::FromString("You lose!"));
}
}

@ -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;

@ -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);
}
@ -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();
}

@ -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();
@ -52,6 +52,10 @@ public:
bool TryConnectToCurrentSession() const;
void QuitCurrentSession();
void UpdateSessionName(FString NewSessionName);
FMyOnCreateSessionComplete OnCreateSessionCompleteEvent;
FMyOnUpdateSessionComplete OnUpdateSessionCompleteEvent;
FMyOnStartSessionCompete OnStartSessionCompleteEvent;
@ -76,6 +80,8 @@ protected:
void OnJoinSessionCompleted(FName SessionName, EOnJoinSessionCompleteResult::Type Result);
private:
bool bIsHost;
FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate;
FDelegateHandle CreateSessionCompleteDelegateHandle;
TSharedPtr<FOnlineSessionSettings> LastSessionSettings;

@ -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)

@ -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;

Loading…
Cancel
Save