Implemented available session list

pull/6/head
eyakm1 2 years ago
parent e70d03fec5
commit d9b81914dc

Binary file not shown.

@ -15,7 +15,7 @@ void UMyMainMenu::NativeConstruct()
void UMyMainMenu::OnHostOnlineGameButtonClicked() void UMyMainMenu::OnHostOnlineGameButtonClicked()
{ {
GetMyGameSubsystem()->CreateSession(2, true); GetMyGameSubsystem()->CreateSession(1, true);
UGameplayStatics::OpenLevel(GetWorld(), FName(TEXT("BattleFieldMap"))); UGameplayStatics::OpenLevel(GetWorld(), FName(TEXT("BattleFieldMap")));
this->RemoveFromParent(); this->RemoveFromParent();
} }

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

@ -4,6 +4,8 @@
#include "MySessionListWidget.h" #include "MySessionListWidget.h"
#include "MyGameInstanceSubsystem.h" #include "MyGameInstanceSubsystem.h"
#include "Components/VerticalBox.h"
#include "MySessionListEntryWidget.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
void UMySessionListWidget::NativeConstruct() void UMySessionListWidget::NativeConstruct()
@ -11,19 +13,40 @@ void UMySessionListWidget::NativeConstruct()
Super::NativeConstruct(); Super::NativeConstruct();
const auto MyGameInstanceSubsystem = GetMyGameSubsystem(); const auto MyGameInstanceSubsystem = GetMyGameSubsystem();
MyGameInstanceSubsystem->OnFindSessionsCompleteEvent.AddUObject(this, &ThisClass::FillSessionListFromDelegate); MyGameInstanceSubsystem->OnFindSessionsCompleteEvent.AddUObject(this, &ThisClass::RefreshList);
// Initiate search // Initiate search
MyGameInstanceSubsystem->FindSessions(10, true); MyGameInstanceSubsystem->FindSessions(10, true);
} }
void UMySessionListWidget::FillSessionListFromDelegate(const TArray<FOnlineSessionSearchResult>& SessionResults, void UMySessionListWidget::RefreshList(const TArray<FOnlineSessionSearchResult>& SessionResults,
bool bSuccessful) bool bSuccessful)
{ {
UE_LOG(LogTemp, Warning, TEXT("FIND SESSION DELEGATED %d %d"), bSuccessful, SessionResults.Num()); 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
GetMyGameSubsystem()->FindSessions(10, true);
}
UMyGameInstanceSubsystem* UMySessionListWidget::GetMyGameSubsystem() const UMyGameInstanceSubsystem* UMySessionListWidget::GetMyGameSubsystem() const
{ {
const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(GetWorld()); const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(GetWorld());

@ -26,10 +26,19 @@ protected:
class UButton* GoBackToMainMenuButton; class UButton* GoBackToMainMenuButton;
UPROPERTY(meta = (BindWidget)) UPROPERTY(meta = (BindWidget))
class UListView* AvailableSessionsList; class UButton* RefreshListButton;
void FillSessionListFromDelegate(const TArray<FOnlineSessionSearchResult>& SessionResults, bool bSuccessful); UPROPERTY(meta = (BindWidget))
class UVerticalBox* SessionListBox;
UPROPERTY(EditDefaultsOnly, Category="Session Info Class")
TSubclassOf<class UMySessionListEntryWidget> EntryClass;
void RefreshList(const TArray<FOnlineSessionSearchResult>& SessionResults, bool bSuccessful);
private: private:
UMyGameInstanceSubsystem* GetMyGameSubsystem() const; UMyGameInstanceSubsystem* GetMyGameSubsystem() const;
UFUNCTION()
void OnRefreshListButtonClicked();
}; };

Loading…
Cancel
Save