1.创建按钮和对应的回调函数


/* 大厅按钮 */
UPROPERTY(meta = (BindWidget))
UButton* HostButton;
/* 加入会话按钮 */
UPROPERTY(meta = (BindWidget))
UButton* JoinButton;
private:
UFUNCTION()
void OnHostButtonClicked();
UFUNCTION()
void OnJoinButtonClicked();
创建调试助手


#pragma once
#include "CoreMinimal.h"
/**
*
*/
namespace Debug
{
static void Print(const FString& Message, FColor Color = FColor::Blue)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, Color, Message);
UE_LOG(LogTemp, Warning, TEXT("%s"), *Message);
}
}
};
绑定按钮对应的回调函数

bool UMenu::Initialize()
{
if (!Super::Initialize())
{
return false;
}
if (HostButton)
{
HostButton->OnClicked.AddDynamic(this, &UMenu::OnHostButtonClicked);
}
if (JoinButton)
{
JoinButton->OnClicked.AddDynamic(this, &UMenu::OnJoinButtonClicked);
}
return true;
}

2.通过游戏实例获取在线会话子系统
private:
class UMultiplayerSessionsSubsystem* MultiplayerSessionsSubsystem;
// 通过获取游戏实例来获取在线会话子系统
UGameInstance* GameInstance = GetGameInstance();
if (GameInstance)
{
MultiplayerSessionsSubsystem = GameInstance->GetSubsystem<UMultiplayerSessionsSubsystem>();
}
3.创建会话
/* 创建会话 */
void UMultiplayerSessionsSubsystem::CreateSession(int32 NumPublicConnections, FString MatchType)
{
// 1.检查凭证是否有效
if (!OnlineSessionInterface)
return;
// 2.防止多次点击造成会话多次创建
auto ExitGameSession = OnlineSessionInterface->GetNamedSession(NAME_GameSession);
if (!ExitGameSession)
{
OnlineSessionInterface->DestroySession(NAME_GameSession);
}
// 3.绑定当会话创建完成的委托
OnCreateSessionCompleteDelegateHandle = OnlineSessionInterface->AddOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegate);
// 4.设置会话属性
SessionSettings = MakeShareable(new FOnlineSessionSettings);
SessionSettings->bIsLANMatch = IOnlineSubsystem::Get()->GetSubsystemName() == "Null" ? true : false;
SessionSettings->bAllowJoinViaPresence = true; // 中途加入
SessionSettings->NumPublicConnections = NumPublicConnections;
SessionSettings->bAllowJoinInProgress = true; // 好友
SessionSettings->bShouldAdvertise = true; // 广播此会话
SessionSettings->bUsesPresence = true; // 好友系统
SessionSettings->bUseLobbiesIfAvailable = true; // steam的聊天系统
SessionSettings->Set(FName("MatchType"), MatchType, EOnlineDataAdvertisementType::ViaOnlineServiceAndPing);
// 5.创建会话
ULocalPlayer* LocalPC = GetWorld()->GetFirstLocalPlayerFromController();
if (LocalPC)
{
// 会话创建失败清理委托
if (!OnlineSessionInterface->CreateSession(*LocalPC->GetPreferredUniqueNetId(), NAME_GameSession, *SessionSettings))
{
OnlineSessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegateHandle);
}
}
}
跳转到大厅并恢复移动设置
void UMenu::NativeDestruct()
{
MenuTearDown();
Super::NativeDestruct();
}
void UMenu::OnHostButtonClicked()
{
Print(TEXT("HostButton is Clicked!!!"));
if (MultiplayerSessionsSubsystem)
{
MultiplayerSessionsSubsystem->CreateSession(NumberOfPublicConnections, MatchType);
UWorld* World = GetWorld();
if (World)
{
World->ServerTravel("/Game/ThirdPerson/Maps/Lobby?listen");
}
}
}
void UMenu::MenuTearDown()
{
RemoveFromParent();
if (GetWorld())
{
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
if (PlayerController)
{
FInputModeGameOnly InputModeData;
PlayerController->SetInputMode(InputModeData);
PlayerController->SetShowMouseCursor(false);
}
}
}
创建委托,解耦
1.创建委托,并广播
/* 创建会话成功的委托 */
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMultiPlayerOnCreateSessionCompleted, bool, bWasSuccessful);
/* 自定义的委托 */
/* 当创建会话完成时 */
FMultiPlayerOnCreateSessionCompleted MultiPlayerOnCreateSessionCompleted;
// 5.创建会话
ULocalPlayer* LocalPC = GetWorld()->GetFirstLocalPlayerFromController();
if (LocalPC)
{
// 会话创建失败清理委托
if (!OnlineSessionInterface->CreateSession(*LocalPC->GetPreferredUniqueNetId(), NAME_GameSession, *SessionSettings))
{
OnlineSessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegateHandle);
MultiPlayerOnCreateSessionCompleted.Broadcast(false);
}
}
/* 创建会话完成对应的回调 */
void UMultiplayerSessionsSubsystem::OnCreateSessionComplete(FName SessionName, bool bWasSuccessful)
{
if (OnlineSessionInterface)
{
OnlineSessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegateHandle);
MultiPlayerOnCreateSessionCompleted.Broadcast(true);
}
}
2.绑定委托对应的回调函数
/* 当创建会话完成时 */
UFUNCTION()
void OnCreateSession(bool bWasSuccessful);
// 绑定在线会话子系统的委托
// 创建会话完成时
MultiplayerSessionsSubsystem->MultiPlayerOnCreateSessionCompleted.AddDynamic(this, &ThisClass::OnCreateSession);
void UMenu::OnCreateSession(bool bWasSuccessful)
{
if (bWasSuccessful)
{
UWorld* World = GetWorld();
if (World)
{
World->ServerTravel("/Game/ThirdPerson/Maps/Lobby?listen");
}
Print("Create Session Success!");
}
}
