I followed this tutorial to make a multiplayer game via Steam. Everything works fine and I was able to create/join session on two different accounts/pc.
However, sometimes when I try to join a session, it failed. I tried to close firewall or use VPN, but it still fails like 50% of the time. (Create session always work with bUseLobbiesIfAvailable = true
.)
Here is my DefaultEngine.ini:
[/Script/Engine.GameEngine]
+NetDriverDefinitions=(DefName="GameNetDriver",DriverClassName="OnlineSubsystemSteam.SteamNetDriver",DriverClassNameFallback="OnlineSubsystemUtils.IpNetDriver")
[OnlineSubsystem]
DefaultPlatformService=Steam
[OnlineSubsystemSteam]
bEnabled=true
SteamDevAppId=480
GameServerQueryPort=27015
[/Script/OnlineSubsystemSteam.SteamNetDriver]
NetConnectionClassName="OnlineSubsystemSteam.SteamNetConnection"
And my C++ function to create session:
void UCPP_GameInstance::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;
SessionSettings.bUseLobbiesIfAvailable = true;
SessionInterface->CreateSession(0, FName("My Session"), SessionSettings);
}
Find session (ignore the funciton name):
void UCPP_GameInstance::JoinServer()
{
UE_LOG(LogTemp, Warning, TEXT("JoinServer"));
SessionSearch = MakeShareable(new FOnlineSessionSearch());
SessionSearch->bIsLanQuery = IOnlineSubsystem::Get()->GetSubsystemName() == "NULL";
SessionSearch->MaxSearchResults = 150000;
SessionSearch->QuerySettings.Set(SEARCH_PRESENCE, true, EOnlineComparisonOp::Equals);
SessionInterface->FindSessions(0, SessionSearch.ToSharedRef());
}
Join session after session found:
void UCPP_GameInstance::OnFindSessionComplete(bool Succeeded)
{
UE_LOG(LogTemp, Warning, TEXT("OnFindSessionComplete, Succeeded: %d"), Succeeded);
if (Succeeded)
{
TArray<FOnlineSessionSearchResult> SearchResults = SessionSearch->SearchResults;
UE_LOG(LogTemp, Warning, TEXT("SearchResults, Server Count: %d"), SearchResults.Num());
if (SearchResults.Num())
{
UE_LOG(LogTemp, Warning, TEXT("Joining Server"));
SessionInterface->JoinSession(0, "My Session", SearchResults[0]);
}
}
}
Client travel after join session:
void UCPP_GameInstance::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{
UE_LOG(LogTemp, Warning, TEXT("OnJoinSessionComplete, SessionName: %s"), *SessionName.ToString());
if (APlayerController* PController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{
FString JoinAddress = "";
SessionInterface->GetResolvedConnectString(SessionName, JoinAddress);
if (JoinAddress != "")
{
PController->ClientTravel(JoinAddress, ETravelType::TRAVEL_Absolute);
}
}
}
I know there is already a lot of posts about “Steam online subsystem not working”. But I guess none of them only fail 50% of the time? I want a clear explanation of why this happened. Maybe because of the instability of the internet? Or can I adjust the handshake times? Use TCP instead?
I am using Unreal Engine 5.3.2 on Windows 11. Running packaged build (Development) on both machine. Will be very thankful if someone can help me with this.