Now we can connect to the first session from the list

pull/6/head
eyakm1 2 years ago
parent d9b81914dc
commit 7f8fce48cd

@ -3,6 +3,7 @@
#include "MyGameInstanceSubsystem.h" #include "MyGameInstanceSubsystem.h"
#include "OnlineSubsystemUtils.h" #include "OnlineSubsystemUtils.h"
#include "Kismet/GameplayStatics.h"
UMyGameInstanceSubsystem::UMyGameInstanceSubsystem() : CreateSessionCompleteDelegate( UMyGameInstanceSubsystem::UMyGameInstanceSubsystem() : CreateSessionCompleteDelegate(
FOnCreateSessionCompleteDelegate::CreateUObject( FOnCreateSessionCompleteDelegate::CreateUObject(
@ -45,7 +46,6 @@ void UMyGameInstanceSubsystem::CreateSession(int32 NumPublicConnections, bool bI
LastSessionSettings->bAllowJoinInProgress = true; LastSessionSettings->bAllowJoinInProgress = true;
LastSessionSettings->bAllowJoinViaPresence = true; LastSessionSettings->bAllowJoinViaPresence = true;
LastSessionSettings->bAllowJoinViaPresenceFriendsOnly = true; LastSessionSettings->bAllowJoinViaPresenceFriendsOnly = true;
LastSessionSettings->bIsDedicated = false;
LastSessionSettings->bUsesPresence = true; LastSessionSettings->bUsesPresence = true;
LastSessionSettings->bIsLANMatch = bIsLANMatch; LastSessionSettings->bIsLANMatch = bIsLANMatch;
LastSessionSettings->bShouldAdvertise = true; LastSessionSettings->bShouldAdvertise = true;
@ -147,8 +147,10 @@ void UMyGameInstanceSubsystem::OnStartSessionCompleted(FName SessionName, bool b
{ {
SessionInterface->ClearOnStartSessionCompleteDelegate_Handle(StartSessionCompleteDelegateHandle); SessionInterface->ClearOnStartSessionCompleteDelegate_Handle(StartSessionCompleteDelegateHandle);
} }
OnStartSessionCompleteEvent.Broadcast(bSuccessful); OnStartSessionCompleteEvent.Broadcast(bSuccessful);
// TODO: Move this from gameinstance subsystem. This should not be here.
UGameplayStatics::OpenLevel(GetWorld(),"BattleFieldMap", true, "listen");
} }
@ -235,7 +237,7 @@ void UMyGameInstanceSubsystem::FindSessions(int32 MaxSearchResults, bool bIsLANQ
LastSessionSearch->bIsLanQuery = bIsLANQuery; LastSessionSearch->bIsLanQuery = bIsLANQuery;
// Disable dedicated server search (maybe enable later, when dedicated server is implemented) // Disable dedicated server search (maybe enable later, when dedicated server is implemented)
LastSessionSearch->QuerySettings.Set(SEARCH_PRESENCE, true, EOnlineComparisonOp::Equals); // LastSessionSearch->QuerySettings.Set(SEARCH_PRESENCE, true, EOnlineComparisonOp::Equals);
const ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController(); const ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController();
if (!SessionInterface->FindSessions(*LocalPlayer->GetPreferredUniqueNetId(), LastSessionSearch.ToSharedRef())) if (!SessionInterface->FindSessions(*LocalPlayer->GetPreferredUniqueNetId(), LastSessionSearch.ToSharedRef()))
@ -286,6 +288,33 @@ void UMyGameInstanceSubsystem::JoinSession(const FOnlineSessionSearchResult& Ses
} }
} }
void UMyGameInstanceSubsystem::JoinSession(const int32 Index)
{
const IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(GetWorld());
if (!SessionInterface.IsValid())
{
OnJoinSessionCompleteEvent.Broadcast(EOnJoinSessionCompleteResult::UnknownError);
return;
}
if (!LastSessionSearch.IsValid() || Index >= LastSessionSearch->SearchResults.Num())
{
OnJoinSessionCompleteEvent.Broadcast(EOnJoinSessionCompleteResult::UnknownError);
return;
}
JoinSessionCompleteDelegateHandle = SessionInterface->AddOnJoinSessionCompleteDelegate_Handle(
JoinSessionCompleteDelegate);
const ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController();
if (!SessionInterface->JoinSession(*LocalPlayer->GetPreferredUniqueNetId(), NAME_GameSession,
LastSessionSearch->SearchResults[Index]))
{
SessionInterface->ClearOnJoinSessionCompleteDelegate_Handle(JoinSessionCompleteDelegateHandle);
OnJoinSessionCompleteEvent.Broadcast(EOnJoinSessionCompleteResult::UnknownError);
}
}
void UMyGameInstanceSubsystem::OnJoinSessionCompleted(FName SessionName, EOnJoinSessionCompleteResult::Type Result) void UMyGameInstanceSubsystem::OnJoinSessionCompleted(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{ {

@ -48,6 +48,10 @@ public:
void JoinSession(const FOnlineSessionSearchResult& SessionSearchResult); void JoinSession(const FOnlineSessionSearchResult& SessionSearchResult);
void JoinSession(const int32 Index);
bool TryConnectToCurrentSession() const;
FMyOnCreateSessionComplete OnCreateSessionCompleteEvent; FMyOnCreateSessionComplete OnCreateSessionCompleteEvent;
FMyOnUpdateSessionComplete OnUpdateSessionCompleteEvent; FMyOnUpdateSessionComplete OnUpdateSessionCompleteEvent;
FMyOnStartSessionCompete OnStartSessionCompleteEvent; FMyOnStartSessionCompete OnStartSessionCompleteEvent;
@ -71,8 +75,6 @@ protected:
void OnJoinSessionCompleted(FName SessionName, EOnJoinSessionCompleteResult::Type Result); void OnJoinSessionCompleted(FName SessionName, EOnJoinSessionCompleteResult::Type Result);
bool TryConnectToCurrentSession() const;
private: private:
FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate; FOnCreateSessionCompleteDelegate CreateSessionCompleteDelegate;
FDelegateHandle CreateSessionCompleteDelegateHandle; FDelegateHandle CreateSessionCompleteDelegateHandle;

@ -11,15 +11,21 @@ void UMyMainMenu::NativeConstruct()
Super::NativeConstruct(); Super::NativeConstruct();
HostOnlineGameButton->OnClicked.AddDynamic(this, &ThisClass::UMyMainMenu::OnHostOnlineGameButtonClicked); HostOnlineGameButton->OnClicked.AddDynamic(this, &ThisClass::UMyMainMenu::OnHostOnlineGameButtonClicked);
GetMyGameSubsystem()->OnCreateSessionCompleteEvent.AddDynamic(this, &ThisClass::StartSessionWhenCreatingSessonComplete);
} }
void UMyMainMenu::OnHostOnlineGameButtonClicked() void UMyMainMenu::OnHostOnlineGameButtonClicked()
{ {
GetMyGameSubsystem()->CreateSession(1, true); GetMyGameSubsystem()->CreateSession(2, true);
UGameplayStatics::OpenLevel(GetWorld(), FName(TEXT("BattleFieldMap")));
this->RemoveFromParent();
} }
void UMyMainMenu::StartSessionWhenCreatingSessonComplete(bool bSuccess)
{
GetMyGameSubsystem()->StartSession();
}
UMyGameInstanceSubsystem* UMyMainMenu::GetMyGameSubsystem() const UMyGameInstanceSubsystem* UMyMainMenu::GetMyGameSubsystem() const
{ {
const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(GetWorld()); const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(GetWorld());

@ -26,6 +26,9 @@ protected:
UFUNCTION() UFUNCTION()
void OnHostOnlineGameButtonClicked(); void OnHostOnlineGameButtonClicked();
UFUNCTION()
void StartSessionWhenCreatingSessonComplete(bool bSuccess);
private: private:
UMyGameInstanceSubsystem* GetMyGameSubsystem() const; UMyGameInstanceSubsystem* GetMyGameSubsystem() const;
}; };

@ -6,15 +6,19 @@
#include "MyGameInstanceSubsystem.h" #include "MyGameInstanceSubsystem.h"
#include "Components/VerticalBox.h" #include "Components/VerticalBox.h"
#include "MySessionListEntryWidget.h" #include "MySessionListEntryWidget.h"
#include "Components/Button.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
void UMySessionListWidget::NativeConstruct() void UMySessionListWidget::NativeConstruct()
{ {
Super::NativeConstruct(); Super::NativeConstruct();
RefreshListButton->OnClicked.AddDynamic(this, &ThisClass::OnRefreshListButtonClicked);
ConnectToSelectedSessionButton->OnClicked.AddDynamic(this, &ThisClass::ConnectToFirstSession);
const auto MyGameInstanceSubsystem = GetMyGameSubsystem(); const auto MyGameInstanceSubsystem = GetMyGameSubsystem();
MyGameInstanceSubsystem->OnFindSessionsCompleteEvent.AddUObject(this, &ThisClass::RefreshList); MyGameInstanceSubsystem->OnFindSessionsCompleteEvent.AddUObject(this, &ThisClass::RefreshList);
MyGameInstanceSubsystem->OnJoinSessionCompleteEvent.AddUObject(this, &ThisClass::OnJoinSessionSuccess);
// Initiate search // Initiate search
MyGameInstanceSubsystem->FindSessions(10, true); MyGameInstanceSubsystem->FindSessions(10, true);
@ -43,9 +47,31 @@ void UMySessionListWidget::OnRefreshListButtonClicked()
{ {
// TODO: Show that we started searching... // TODO: Show that we started searching...
// Initiate search // Initiate search
SessionListBox->ClearChildren();
GetMyGameSubsystem()->FindSessions(10, true); 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 UMyGameInstanceSubsystem* UMySessionListWidget::GetMyGameSubsystem() const
{ {

@ -36,9 +36,14 @@ protected:
void RefreshList(const TArray<FOnlineSessionSearchResult>& SessionResults, bool bSuccessful); void RefreshList(const TArray<FOnlineSessionSearchResult>& SessionResults, bool bSuccessful);
void OnJoinSessionSuccess(EOnJoinSessionCompleteResult::Type Result);
private: private:
UMyGameInstanceSubsystem* GetMyGameSubsystem() const; UMyGameInstanceSubsystem* GetMyGameSubsystem() const;
UFUNCTION() UFUNCTION()
void OnRefreshListButtonClicked(); void OnRefreshListButtonClicked();
UFUNCTION()
void ConnectToFirstSession();
}; };

Loading…
Cancel
Save