Skip to content

Instantly share code, notes, and snippets.

@JElbourne
Created June 28, 2021 23:33
Show Gist options
  • Save JElbourne/96208c8f881bee7b5d2192df977f5f23 to your computer and use it in GitHub Desktop.
Save JElbourne/96208c8f881bee7b5d2192df977f5f23 to your computer and use it in GitHub Desktop.
YourGameInstance.cpp - For Video: https://youtu.be/jc0COamYRm4
#include "Game/CreteGameInstance.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/World.h"
UCreteGameInstance::UCreteGameInstance()
{
}
void UCreteGameInstance::Init()
{
Super::Init();
if(IOnlineSubsystem* Subsystem = IOnlineSubsystem::Get())
{
SessionInterface = Subsystem->GetSessionInterface();
if (SessionInterface.IsValid())
{
SessionInterface->OnCreateSessionCompleteDelegates.AddUObject(this, &UCreteGameInstance::OnCreateSessionComplete);
SessionInterface->OnFindSessionsCompleteDelegates.AddUObject(this, &UCreteGameInstance::OnFindSessionComplete);
SessionInterface->OnJoinSessionCompleteDelegates.AddUObject(this, &UCreteGameInstance::OnJoinSessionComplete);
}
}
}
void UCreteGameInstance::OnCreateSessionComplete(FName SessionName, bool Succeeded)
{
if (Succeeded)
{
GetWorld()->ServerTravel("/Game/ThirdPersonCPP/Maps/ThirdPersonExampleMap?listen");
}
}
void UCreteGameInstance::OnFindSessionComplete(bool Succeeded)
{
if (Succeeded)
{
TArray<FOnlineSessionSearchResult> SearchResults = SessionSearch->SearchResults;
if (SearchResults.Num())
{
SessionInterface->JoinSession(0, FName("Crete Session"), SearchResults[0]);
}
}
}
void UCreteGameInstance::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{
if(APlayerController* PController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{
FString JoinAddress = "";
SessionInterface->GetResolvedConnectString(SessionName, JoinAddress);
if(JoinAddress != "")
{
PController->ClientTravel(JoinAddress, ETravelType::TRAVEL_Absolute);
}
}
}
void UCreteGameInstance::CreateServer()
{
UE_LOG(LogTemp, Warning, TEXT("CreateServer"));
FOnlineSessionSettings SessionSettings;
SessionSettings.bAllowJoinInProgress = true;
SessionSettings.bIsDedicated = false;
SessionSettings.bIsLANMatch = (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL");
SessionSettings.bShouldAdvertise = true;
SessionSettings.bUsesPresence = true;
SessionSettings.NumPublicConnections = 5;
SessionInterface->CreateSession(0, FName("Crete Session"), SessionSettings);
}
void UCreteGameInstance::JoinServer()
{
SessionSearch = MakeShareable(new FOnlineSessionSearch());
SessionSearch->bIsLanQuery = (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL");
SessionSearch->MaxSearchResults = 10000;
SessionSearch->QuerySettings.Set("SEARCH_PRESENCE", true, EOnlineComparisonOp::Equals);
SessionInterface->FindSessions(0, SessionSearch.ToSharedRef());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment