C++ Create session not working like Blueprint node

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!

Bumpi bump.

I guess we, or at least i, need more information. Most of us haven’t worked with C++ Server yet, and the Docs aren’t that good. The ShooterGame is too packed with additional things to really understand how it works, so, you will need to debug it more.

Does creating the session work? Is the client able to find the session? Is the client really joining the session (can you see that on the server)? Is only the MapLoad failing?

Maybe also check the LOG. There should be some errors!

Because we need to know which part is failing to try to help you (:

Hello, Creating the session seems to work as the CreateSessionCompleteDelegate is fired and bSuccessfull is true. However after opening a new map as a listen server I get the following warnings in the output log:

LogOnline:Warning: NULL: No game present to join for session (Game)
LogOnline:Warning: NULL: Can't start an online game for session (Game) that hasn't been created

So apparently after loading the map the session doesn’t exist anylonger. Using the blueprint nodes to do pretty much the exact same thing works without those errors.

Any ideas what I did wrong here?

Hello, I got a notification via e-mail about the last two comments on this topic and also on my profile page it says that somebody replied, but those last two comments are not being displayed right here which must be a bug of some sort.

However I know what you wrote from the e-mail. joeGraf mentioned that I forgot to enable join in progress, which he was right about but does not solve the problem unfortunatelly. alkor.isharioth commented that it could be due to the player controller being recreated at some point, but firstly it has to be destroyed and recreated as I use two different player controller classes in my main menu and the actual game. And secondly the create session will not return you a reference to a session which you have to store, I believe it is stored somewhere automatically. Calling the create session function really only initiates this process so it should be irrelevant from which class you call it.

Thank you for trying to help!