Unreal Engine – Matchmaking
void AMyPlayerState::BeginPlay(){
MyPlayerState.cpp
FString levelName = GetWorld()->GetMapName();
levelName.RemoveFromStart(GetWorld()->StreamingLevelsPrefix);
if (levelName == "StartMap"){
tcpClient = new TCPClient(this);
canConnectToGameServer = false;
if ((MatchmakingWidgetClass) && (tcpClient->IsConnected())) {
serversList = new TArray<FSessionInfo*>();
GetWorld()->GetTimerManager().SetTimer(serverListTimerHandle,
this, &AMyPlayerState::OnUpdateServerList, 2, true);
MatchmakingWidget = CreateWidget<UUserWidget>(GetWorld(),
MatchmakingWidgetClass);
MatchmakingWidget->AddToViewport();
serverListScrollBoxWidget = Cast<UScrollBox>(
MatchmakingWidget->GetWidgetFromName(TEXT("MyScrollBox")));
UButton* newSessionButton = Cast<UButton>(
MatchmakingWidget->GetWidgetFromName(TEXT("NewSession")));
if (newSessionButton) {
newSessionButton->SetIsEnabled(true);
newSessionButton->OnClicked.AddDynamic(this,
&AMyPlayerState::OnNewSessionClicked);
}
}
} }