Hello all!
I am currently working on implementing the sessions system into my game. With the blueprint nodes it worked as expected except that it lacks some functionality. So I decided to go for a C++ solution, however I can’t quite get it to work. When I create a session with the BP node and start a listen server, my other client can find and join the session. But when I create and start a session and the server with C++ the client will not find it or won’t load the map when joining.
Here is how the important code part looks like:
AMenuPlayerController::AMenuPlayerController()
{
SessionSettings.bAllowJoinInProgress = true;
SessionSettings.bUsesPresence = true;
SessionSettings.bAllowJoinViaPresence = true;
}
void AMenuPlayerController::StartSession(bool bIsLANMatch, bool bShouldAdvertise, int32 NumPublicConnections)
{
if (bStartedSession) return;
SessionSettings.bIsLANMatch = bIsLANMatch;
SessionSettings.bShouldAdvertise = bShouldAdvertise;
SessionSettings.NumPublicConnections = NumPublicConnections;
if (PlayerState)
{
IOnlineSubsystem * OnlineSubsystem = IOnlineSubsystem::Get();
if (OnlineSubsystem)
{
IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
if (SessionInterface.IsValid())
{
DelegateHandle_CreateSessionComplete = SessionInterface->AddOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegate);
SessionInterface->CreateSession(*PlayerState->UniqueId.GetUniqueNetId(), GameSessionName, SessionSettings);
}
}
}
}
void AMenuPlayerController::StartServer(const FName & MapName)
{
if (!bStartedSession) return;
NextMapName = MapName;
IOnlineSubsystem * OnlineSubsystem = IOnlineSubsystem::Get();
if (OnlineSubsystem)
{
IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
if (SessionInterface.IsValid())
{
DelegateHandle_StartSessionComplete = SessionInterface->AddOnStartSessionCompleteDelegate_Handle(OnStartSessionCompleteDelegate);
SessionInterface->StartSession(GameSessionName);
}
}
}
void AMenuPlayerController::OnCreateSessionComplete(FName SessionName, bool bSuccessfull)
{
IOnlineSubsystem * OnlineSubsystem = IOnlineSubsystem::Get();
if (OnlineSubsystem)
{
IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
if (SessionInterface.IsValid())
{
SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(DelegateHandle_CreateSessionComplete);
if (bSuccessfull)
{
bStartedSession = true;
}
}
}
}
void AMenuPlayerController::OnStartSessionComplete(FName SessionName, bool bSuccessfull)
{
IOnlineSubsystem * OnlineSubsystem = IOnlineSubsystem::Get();
if (OnlineSubsystem)
{
IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
if (SessionInterface.IsValid())
{
SessionInterface->ClearOnStartSessionCompleteDelegate_Handle(DelegateHandle_StartSessionComplete);
if (bSuccessfull)
{
UGameplayStatics::OpenLevel(this, NextMapName, true, TEXT("listen"));
}
}
}
}
Thanks in advance for you help!