Hello,
I’m new to this forum and been working with unreal engine for an year (as my first game engine).
I planning to create an online multiplayer game using the OSS Steam.
When I create a session (without an error), I can’t seem to find my session with another client (different PC, steam account, inside same network and outside). It worked fine locally with OSS Steam.
If anyone knows what I am doing wrong, I would appreciate that information.
Inside 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"
Inside .uproject
"Plugins": [
{
"Name": "ModelingToolsEditorMode",
"Enabled": true,
"TargetAllowList": [
"Editor"
]
},
{
"Name": "VisualStudioTools",
"Enabled": true,
"MarketplaceURL": "com.epicgames.launcher://ue/marketplace/product/362651520df94e4fa65492dbcba44ae2",
"SupportedTargetPlatforms": [
"Win64"
]
},
{
"Name": "OnlineSubsystemSteam",
"Enabled": true
}
MyGameInstance.h:
#pragma once
#include "CoreMinimal.h"
#include "OnlineSubsystem.h"
#include "OnlineSessionSettings.h"
#include "Interfaces/OnlineSessionInterface.h"
#include "Engine/GameInstance.h"
#include "MyGameInstance.generated.h"
/**
*
*/
UCLASS()
class GOPRO2_API UMyGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
UMyGameInstance();
protected:
TSharedPtr<FOnlineSessionSearch> SessionSearch;
IOnlineSessionPtr SessionInterface;
FName SessionName = FName("My Game");
void Init() override;
//Methods
UFUNCTION(BlueprintCallable)
void CreateServer();
UFUNCTION(BlueprintCallable)
void JoinServer();
//Delegates
void OnCreateSessionComplete(FName SessionName, bool wasSuccessful);
void OnFindSessionComplete(bool Succeeded);
void OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result);
void OnDestroySessionComplete(FName SessionName, bool bWasSuccessful);
};
MyGameInstance.cpp:
#include "MyGameInstance.h"
#include "Kismet/GameplayStatics.h"
UMyGameInstance::UMyGameInstance()
{
}
void UMyGameInstance::Init()
{
Super::Init();
if (IOnlineSubsystem* Subsystem = IOnlineSubsystem::Get()) {
SessionInterface = Subsystem->GetSessionInterface();
if (SessionInterface.IsValid()) {
SessionInterface->OnCreateSessionCompleteDelegates.AddUObject(this, &UMyGameInstance::OnCreateSessionComplete);
SessionInterface->OnFindSessionsCompleteDelegates.AddUObject(this, &UMyGameInstance::OnFindSessionComplete);
SessionInterface->OnJoinSessionCompleteDelegates.AddUObject(this, &UMyGameInstance::OnJoinSessionComplete);
SessionInterface->OnDestroySessionCompleteDelegates.AddUObject(this, &UMyGameInstance::OnDestroySessionComplete);
}
}
}
void UMyGameInstance::CreateServer()
{
UE_LOG(LogTemp, Warning, TEXT("Create Server"));
bool exists = SessionInterface->GetNamedSession(SessionName) != nullptr;
if (exists) {
SessionInterface->DestroySession(SessionName);
return;
}
FOnlineSessionSettings SessionSettings;
SessionSettings.bAllowJoinInProgress = true;
SessionSettings.bIsLANMatch = (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL");
SessionSettings.bShouldAdvertise = true;
SessionSettings.bUsesPresence = true;
SessionSettings.bUseLobbiesIfAvailable = true;
SessionSettings.NumPublicConnections = 5;
SessionInterface->CreateSession(0, SessionName, SessionSettings);
}
void UMyGameInstance::JoinServer()
{
UE_LOG(LogTemp, Warning, TEXT("Join Server"));
SessionSearch = MakeShareable(new FOnlineSessionSearch());
SessionSearch->bIsLanQuery = (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL");
SessionSearch->MaxSearchResults = 10000;
SessionSearch->QuerySettings.Set("SEARCH_PRESENCE", true, EOnlineComparisonOp::Equals);
SessionInterface->FindSessions(0, SessionSearch.ToSharedRef());
}
void UMyGameInstance::OnCreateSessionComplete(FName SessionName, bool Succeeded)
{
if (Succeeded) {
GetWorld()->ServerTravel("/Game/Game/Maps/TestingLobby?listen");
}
}
void UMyGameInstance::OnFindSessionComplete(bool Succeeded)
{
if (!Succeeded) {
return;
}
TArray<FOnlineSessionSearchResult> SearchResults = SessionSearch->SearchResults;
if (SearchResults.Num()) {
UE_LOG(LogTemp, Warning, TEXT("LISTING SESSIONS"));
UE_LOG(LogTemp, Warning, TEXT("-----------"));
for (FOnlineSessionSearchResult i : SearchResults) {
UE_LOG(LogTemp, Warning, TEXT("Owning User Name: %s"), *FString(i.Session.OwningUserName));
}
SessionInterface->JoinSession(0, SessionName, SearchResults[0]);
}
}
void UMyGameInstance::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{
if (APlayerController* PController = UGameplayStatics::GetPlayerController(GetWorld(), 0)) {
FString JoinAddress = "";
SessionInterface->GetResolvedConnectString(SessionName, JoinAddress);
if (JoinAddress != "") {
PController->ClientTravel(JoinAddress, ETravelType::TRAVEL_Absolute);
}
}
}
void UMyGameInstance::OnDestroySessionComplete(FName SessionName, bool bWasSuccessful)
{
if (bWasSuccessful) {
CreateServer();
}
}