commit
024fb31ea1
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,35 @@
|
|||||||
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||||||
|
|
||||||
|
|
||||||
|
#include "MyMainMenu.h"
|
||||||
|
|
||||||
|
#include "Components/Button.h"
|
||||||
|
#include "Kismet/GameplayStatics.h"
|
||||||
|
|
||||||
|
void UMyMainMenu::NativeConstruct()
|
||||||
|
{
|
||||||
|
Super::NativeConstruct();
|
||||||
|
|
||||||
|
HostOnlineGameButton->OnClicked.AddDynamic(this, &ThisClass::UMyMainMenu::OnHostOnlineGameButtonClicked);
|
||||||
|
|
||||||
|
GetMyGameSubsystem()->OnCreateSessionCompleteEvent.AddDynamic(this, &ThisClass::StartSessionWhenCreatingSessonComplete);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMyMainMenu::OnHostOnlineGameButtonClicked()
|
||||||
|
{
|
||||||
|
GetMyGameSubsystem()->CreateSession(2, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMyMainMenu::StartSessionWhenCreatingSessonComplete(bool bSuccess)
|
||||||
|
{
|
||||||
|
GetMyGameSubsystem()->StartSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
UMyGameInstanceSubsystem* UMyMainMenu::GetMyGameSubsystem() const
|
||||||
|
{
|
||||||
|
const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(GetWorld());
|
||||||
|
UMyGameInstanceSubsystem* GameInstanceSubsystem = GameInstance->GetSubsystem<UMyGameInstanceSubsystem>();
|
||||||
|
return GameInstanceSubsystem;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
|||||||
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "Blueprint/UserWidget.h"
|
||||||
|
#include "MyGameInstanceSubsystem.h"
|
||||||
|
|
||||||
|
#include "MyMainMenu.generated.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
UCLASS()
|
||||||
|
class TURNBASEDTUTORIAL_API UMyMainMenu : public UUserWidget
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
public:
|
||||||
|
UPROPERTY(meta = (BindWidget))
|
||||||
|
class UButton* HostOnlineGameButton;
|
||||||
|
|
||||||
|
virtual void NativeConstruct() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
UFUNCTION()
|
||||||
|
void OnHostOnlineGameButtonClicked();
|
||||||
|
|
||||||
|
UFUNCTION()
|
||||||
|
void StartSessionWhenCreatingSessonComplete(bool bSuccess);
|
||||||
|
|
||||||
|
private:
|
||||||
|
UMyGameInstanceSubsystem* GetMyGameSubsystem() const;
|
||||||
|
};
|
@ -0,0 +1,22 @@
|
|||||||
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||||||
|
|
||||||
|
|
||||||
|
#include "MySessionListEntryWidget.h"
|
||||||
|
|
||||||
|
#include "OnlineSessionSettings.h"
|
||||||
|
#include "Interfaces/OnlineSessionInterface.h"
|
||||||
|
#include "Components/TextBlock.h"
|
||||||
|
|
||||||
|
void UMySessionListEntryWidget::Update(int SessionIndex, const FOnlineSessionSearchResult& Session)
|
||||||
|
{
|
||||||
|
SessionId = SessionIndex;
|
||||||
|
IndexText->SetText(FText::AsNumber(SessionIndex + 1));
|
||||||
|
// TODO: SessionNameText->SetText(FText::FromString(Session.Session.SessionSettings.Get(...)))
|
||||||
|
SessionNameText->SetText(FText::FromString("Test session name"));
|
||||||
|
|
||||||
|
int MaxPlayerCount = Session.Session.SessionSettings.NumPublicConnections;
|
||||||
|
int CurPlayerCount = MaxPlayerCount - Session.Session.NumOpenPublicConnections;
|
||||||
|
|
||||||
|
PlayersCountText->SetText(FText::AsNumber(CurPlayerCount));
|
||||||
|
PingText->SetText(FText::AsNumber(Session.PingInMs));
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "Interfaces/OnlineSessionInterface.h"
|
||||||
|
#include "Blueprint/UserWidget.h"
|
||||||
|
#include "MySessionListEntryWidget.generated.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
UCLASS()
|
||||||
|
class TURNBASEDTUTORIAL_API UMySessionListEntryWidget : public UUserWidget
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
public:
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget))
|
||||||
|
class UTextBlock* IndexText;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget))
|
||||||
|
class UTextBlock* SessionNameText;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget))
|
||||||
|
class UTextBlock* PlayersCountText;
|
||||||
|
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(BindWidget))
|
||||||
|
class UTextBlock* PingText;
|
||||||
|
|
||||||
|
void Update(int SessionIndex, const FOnlineSessionSearchResult& Session);
|
||||||
|
|
||||||
|
int SessionId;
|
||||||
|
FString SessionName;
|
||||||
|
};
|
@ -0,0 +1,81 @@
|
|||||||
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||||||
|
|
||||||
|
|
||||||
|
#include "MySessionListWidget.h"
|
||||||
|
|
||||||
|
#include "MyGameInstanceSubsystem.h"
|
||||||
|
#include "Components/VerticalBox.h"
|
||||||
|
#include "MySessionListEntryWidget.h"
|
||||||
|
#include "Components/Button.h"
|
||||||
|
#include "Kismet/GameplayStatics.h"
|
||||||
|
|
||||||
|
void UMySessionListWidget::NativeConstruct()
|
||||||
|
{
|
||||||
|
Super::NativeConstruct();
|
||||||
|
|
||||||
|
RefreshListButton->OnClicked.AddDynamic(this, &ThisClass::OnRefreshListButtonClicked);
|
||||||
|
ConnectToSelectedSessionButton->OnClicked.AddDynamic(this, &ThisClass::ConnectToFirstSession);
|
||||||
|
|
||||||
|
const auto MyGameInstanceSubsystem = GetMyGameSubsystem();
|
||||||
|
MyGameInstanceSubsystem->OnFindSessionsCompleteEvent.AddUObject(this, &ThisClass::RefreshList);
|
||||||
|
MyGameInstanceSubsystem->OnJoinSessionCompleteEvent.AddUObject(this, &ThisClass::OnJoinSessionSuccess);
|
||||||
|
|
||||||
|
// Initiate search
|
||||||
|
MyGameInstanceSubsystem->FindSessions(10, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMySessionListWidget::RefreshList(const TArray<FOnlineSessionSearchResult>& SessionResults,
|
||||||
|
bool bSuccessful)
|
||||||
|
{
|
||||||
|
if (!bSuccessful)
|
||||||
|
{
|
||||||
|
UE_LOG(LogTemp, Error, TEXT("Find sessions FAILED!!!!"));
|
||||||
|
// TODO: Mark find sessions error
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SessionListBox->ClearChildren();
|
||||||
|
for (const auto &Session: SessionResults)
|
||||||
|
{
|
||||||
|
auto *ItemWidget = CreateWidget<UMySessionListEntryWidget>(this, EntryClass);
|
||||||
|
ItemWidget->Update(SessionListBox->GetChildrenCount(), Session);
|
||||||
|
SessionListBox->AddChild(ItemWidget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UMySessionListWidget::OnRefreshListButtonClicked()
|
||||||
|
{
|
||||||
|
// TODO: Show that we started searching...
|
||||||
|
// Initiate search
|
||||||
|
SessionListBox->ClearChildren();
|
||||||
|
GetMyGameSubsystem()->FindSessions(10, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UMySessionListWidget::ConnectToFirstSession()
|
||||||
|
{
|
||||||
|
GetMyGameSubsystem()->JoinSession(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UMySessionListWidget::OnJoinSessionSuccess(EOnJoinSessionCompleteResult::Type Result)
|
||||||
|
{
|
||||||
|
if (Result != EOnJoinSessionCompleteResult::Success)
|
||||||
|
{
|
||||||
|
UE_LOG(LogTemp, Error, TEXT("Failed to connect to session!!"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!GetMyGameSubsystem()->TryConnectToCurrentSession())
|
||||||
|
{
|
||||||
|
UE_LOG(LogTemp, Error, TEXT("Failed to travel client to session!!"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
UE_LOG(LogTemp, Display, TEXT("Connected and travelled to session!!!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
UMyGameInstanceSubsystem* UMySessionListWidget::GetMyGameSubsystem() const
|
||||||
|
{
|
||||||
|
const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(GetWorld());
|
||||||
|
UMyGameInstanceSubsystem* GameInstanceSubsystem = GameInstance->GetSubsystem<UMyGameInstanceSubsystem>();
|
||||||
|
return GameInstanceSubsystem;
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
// Fill out your copyright notice in the Description page of Project Settings.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "CoreMinimal.h"
|
||||||
|
#include "MyGameInstanceSubsystem.h"
|
||||||
|
|
||||||
|
#include "Blueprint/UserWidget.h"
|
||||||
|
#include "MySessionListWidget.generated.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
UCLASS()
|
||||||
|
class TURNBASEDTUTORIAL_API UMySessionListWidget : public UUserWidget
|
||||||
|
{
|
||||||
|
GENERATED_BODY()
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void NativeConstruct() override;
|
||||||
|
|
||||||
|
UPROPERTY(meta = (BindWidget))
|
||||||
|
class UButton* ConnectToSelectedSessionButton;
|
||||||
|
|
||||||
|
UPROPERTY(meta = (BindWidget))
|
||||||
|
class UButton* GoBackToMainMenuButton;
|
||||||
|
|
||||||
|
UPROPERTY(meta = (BindWidget))
|
||||||
|
class UButton* RefreshListButton;
|
||||||
|
|
||||||
|
UPROPERTY(meta = (BindWidget))
|
||||||
|
class UVerticalBox* SessionListBox;
|
||||||
|
|
||||||
|
UPROPERTY(EditDefaultsOnly, Category="Session Info Class")
|
||||||
|
TSubclassOf<class UMySessionListEntryWidget> EntryClass;
|
||||||
|
|
||||||
|
void RefreshList(const TArray<FOnlineSessionSearchResult>& SessionResults, bool bSuccessful);
|
||||||
|
|
||||||
|
void OnJoinSessionSuccess(EOnJoinSessionCompleteResult::Type Result);
|
||||||
|
|
||||||
|
private:
|
||||||
|
UMyGameInstanceSubsystem* GetMyGameSubsystem() const;
|
||||||
|
|
||||||
|
UFUNCTION()
|
||||||
|
void OnRefreshListButtonClicked();
|
||||||
|
|
||||||
|
UFUNCTION()
|
||||||
|
void ConnectToFirstSession();
|
||||||
|
};
|
Loading…
Reference in new issue