Hi i managed to create a server and join online. but now when I have created new menus and have to enter the server name and enter the server name to join the server it does not find the server name.
now when i look at visual studio i see that MySessionName didn’t seem to be activated?
here are the codes
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “Subsystems/GameInstanceSubsystem.h”
#include “Interfaces/OnlineSessionInterface.h”
#include “OnlineSessionSettings.h”
#include “MultiplayerSessionsSubsystem.generated.h”
/**
*
*/
UCLASS()
class COOPADVENTURE_API UMultiplayerSessionsSubsystem : public UGameInstanceSubsystem
{
GENERATED_BODY()
public:
UMultiplayerSessionsSubsystem();
void Initialize(FSubsystemCollectionBase& Collection) override;
void Deinitialize() override;
IOnlineSessionPtr SessionInterface;
UFUNCTION(BlueprintCallable)
void CreateServer(FString ServerName);
UFUNCTION(BlueprintCallable)
void FindServer(FString ServerName);
void OnCreateSessionComplete(FName SessionName, bool WasSuccessful);
void OnDestroySessionComplete(FName SessionName, bool WasSuccessful);
void OnFindSessionsComplete(bool WasSuccessful);
void OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result);
bool CreateServerAfterDestroy;
FString DestroyServerName;
FString ServerNameToFind;
FName MySessionName;
TSharedPtr<FOnlineSessionSearch> SessionSearch;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include “MultiplayerSessionsSubsystem.h”
#include “OnlineSubsystem.h”
void PrintString(const FString& Str)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Cyan, Str);
}
}
UMultiplayerSessionsSubsystem::UMultiplayerSessionsSubsystem()
{
//PrintString(“MSS Constructor”);
CreateServerAfterDestroy = false;
DestroyServerName = "";
ServerNameToFind = "";
MySessionName = FName("Co-op Adventure Session Name");
}
void UMultiplayerSessionsSubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
//PrintString(“MSS Initialize”);
IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
if (OnlineSubsystem)
{
FString SubsystemName = OnlineSubsystem->GetSubsystemName().ToString();
PrintString(SubsystemName);
SessionInterface = OnlineSubsystem->GetSessionInterface();
if (SessionInterface.IsValid())
{
SessionInterface->OnCreateSessionCompleteDelegates.AddUObject(this, &UMultiplayerSessionsSubsystem::OnCreateSessionComplete);
SessionInterface->OnDestroySessionCompleteDelegates.AddUObject(this, &UMultiplayerSessionsSubsystem::OnDestroySessionComplete);
SessionInterface->OnFindSessionsCompleteDelegates.AddUObject(this, &UMultiplayerSessionsSubsystem::OnFindSessionsComplete);
SessionInterface->OnJoinSessionCompleteDelegates.AddUObject(this, &UMultiplayerSessionsSubsystem::OnJoinSessionComplete);
}
}
}
void UMultiplayerSessionsSubsystem::Deinitialize()
{
//UE_LOG(LogTemp, Warning, TEXT(“MSS Deinitialize”));
}
void UMultiplayerSessionsSubsystem::CreateServer(FString ServerName)
{
PrintString(“CreateServer”);
if (ServerName.IsEmpty())
{
PrintString("Server name connot be empty");
return;
}
FNamedOnlineSession *ExistingSession = SessionInterface->GetNamedSession(MySessionName);
if (ExistingSession)
{
FString Msg = FString::Printf(TEXT("Session with name %s already exits, destroying it."), *MySessionName.ToString());
PrintString(Msg);
CreateServerAfterDestroy = true;
DestroyServerName = ServerName;
SessionInterface->DestroySession(MySessionName);
return;
}
FOnlineSessionSettings SessionSettings;
SessionSettings.bAllowJoinInProgress = true;
SessionSettings.bIsDedicated = false;
SessionSettings.bShouldAdvertise = true;
SessionSettings.NumPublicConnections = 2;
SessionSettings.bUseLobbiesIfAvailable = true;
SessionSettings.bUsesPresence = true;
SessionSettings.bAllowJoinViaPresence = true;
bool IsLAN = false;
if (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL")
{
IsLAN = true;
}
SessionSettings.bIsLANMatch = IsLAN;
SessionSettings.Set(FName("SERVER_NAME"), ServerName, EOnlineDataAdvertisementType::ViaOnlineServiceAndPing);
SessionInterface->CreateSession(0, MySessionName, SessionSettings);
}
void UMultiplayerSessionsSubsystem::FindServer(FString ServerName)
{
PrintString(“FindServer”);
if (ServerName.IsEmpty())
{
PrintString("Server name cannot be empty");
return;
}
SessionSearch = MakeShareable(new FOnlineSessionSearch());
bool IsLAN = false;
if (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL")
{
IsLAN = true;
}
SessionSearch->bIsLanQuery = IsLAN;
SessionSearch->MaxSearchResults = 9999;
SessionSearch->QuerySettings.Set(SEARCH_PRESENCE, true, EOnlineComparisonOp::Equals);
SessionInterface->FindSessions(0, SessionSearch.ToSharedRef());
ServerNameToFind = ServerName;
}
void UMultiplayerSessionsSubsystem::OnCreateSessionComplete(FName SessionName, bool WasSuccessful)
{
PrintString(FString::Printf(TEXT(“OnCreateSessionComplete: %d”), WasSuccessful));
if (WasSuccessful)
{
GetWorld()->ServerTravel("/Game/ThirdPerson/Maps/ThirdPersonMap?listen");
}
}
void UMultiplayerSessionsSubsystem::OnDestroySessionComplete(FName SessionName, bool WasSuccessful)
{
FString Msg = FString::Printf(TEXT(“OnDestroySessionComplete, SessionName: %s, Success: %d”), *SessionName.ToString(), WasSuccessful);
PrintString(Msg);
if (CreateServerAfterDestroy)
{
CreateServerAfterDestroy = false;
CreateServer(DestroyServerName);
}
}
void UMultiplayerSessionsSubsystem::OnFindSessionsComplete(bool WasSuccessful)
{
if (!WasSuccessful) return;
if (ServerNameToFind.IsEmpty()) return;
TArray<FOnlineSessionSearchResult> Results = SessionSearch->SearchResults;
FOnlineSessionSearchResult* CorrectResult = 0;
if (Results.Num() > 0)
{
FString Msg = FString::Printf(TEXT(" % d session found."), Results.Num());
PrintString(Msg);
for(FOnlineSessionSearchResult Result : Results)
{
if (Result.IsValid())
{
FString ServerName = "No name";
Result.Session.SessionSettings.Get(FName("SERVER_NAME"), ServerName);
if (ServerName.Equals(ServerNameToFind))
{
CorrectResult = &Result;
FString Msg2 = FString::Printf(TEXT("Found server with name: %s"), *ServerName);
PrintString(Msg2);
break;
}
}
}
if (CorrectResult)
{
SessionInterface->JoinSession(0, MySessionName, *CorrectResult);
}
else
{
PrintString(FString::Printf(TEXT("Couldnt find server: %s"), *ServerNameToFind));
ServerNameToFind = "";
}
}
else
{
PrintString("Zero Sessions found.");
}
}
void UMultiplayerSessionsSubsystem::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{
if (Result == EOnJoinSessionCompleteResult::Success)
{
FString Msg = FString::Printf(TEXT(“Successfully joined session %s”), *SessionName.ToString());
PrintString(Msg);
FString Address = "";
bool Success = SessionInterface->GetResolvedConnectString(MySessionName, Address);
if (Success)
{
PrintString(FString::Printf(TEXT("Address: %s"), *Address));
APlayerController* PlayerController = GetGameInstance()->GetFirstLocalPlayerController();
if (PlayerController)
{
PlayerController->ClientTravel(Address, ETravelType::TRAVEL_Absolute);
}
}
else
{
PrintString("GetResolvedConnectString returned false");
}
}
else
{
PrintString("OnJoinSessionComplete failed");
}
}