I was running into an issue where when I would join a locally hosted server, exit, then try to join a different server my game would always join the server I joined first again regardless of what was selected in my join list. I also had an issue where if I went from hosting a server to trying to join one I wouldn’t be able to join as a session already existed.
This was because, in my case, the Session still existed even after exiting back to the main menu. I did my GameInstance management a bit different than Sam, so I’m not sure if this applies to you if you followed his examples exactly. However, I resolved my problem by calling DestroySession
on my ExitServer callback. Then, in the OnDestroySessionComplete
callback I had to add a new check if we were either exiting a game or trying to host a new game. I added a private class variable: bool IsHostRequest
to check for this case. If true, the code behaved as before and created a new session. If false, it just returned to the main menu.
This seems to have resolved my issue of allowing you to quit a current game and either join or host a new one. Below is the code snippet modification inside my OnDestroySessionComplete
callback.
if (IsHostRequest)
{
CreateSession();
IsHostRequest = false;
}
else
{
// We can assume here the user just exited the game, return to main menu
this->ReturnToMainMenu();
}
And here is my ExitServer
code:
void UPuzzlePlatformsGameInstance::ExitServer()
{
UE_LOG(LogTemp, Display, TEXT("Exiting server"));
if (!ensure(GameMenu != nullptr)) return;
GameMenu->RemoveFromViewport();
IsHostRequest = false;
SessionInterface->DestroySession(SESSION_NAME);
I feel like I should probably save the RemoveFromViewport
call here for the DestroySession callback, but as of right now I haven’t noticed any strange behavior. If you attempt a similar fix, let me know if this causes issues for you.