Hey, so I’m trying to learn how to use OnlineSubsystems and tried Steam ones. Everything works and the travel is happening but somehow the Net Mode of the client is still Standalone and not Client. Do I miss something in the GameInstance or in Engine Settings?
// TPGameInstance.cpp
#include "TPGameInstance.h"
#include "OnlineSessionSettings.h"
#include "GameFramework/GameSession.h"
#include "GameFramework/OnlineSession.h"
#include "Kismet/GameplayStatics.h"
void UTPGameInstance::Init()
{
Super::Init();
if (IOnlineSubsystem* Subsystem = IOnlineSubsystem::Get())
{
if (IOnlineSessionPtr CurrentSession = Subsystem->GetSessionInterface())
{
CurrentSession->OnSessionUserInviteAcceptedDelegates.AddUObject(this, &UTPGameInstance::OnSessionUserInviteAccepted);
}
}
}
void UTPGameInstance::OnCreateSessionComplete(FName SessionName, bool bWasSuccessful)
{
if (bWasSuccessful)
{
GetWorld()->ServerTravel("/Game/ThirdPerson/Lvl_ThirdPerson?listen");
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Successfully created Online Session!"));
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Couldn't create Online Session!"));
}
}
void UTPGameInstance::OnSessionUserInviteAccepted(const bool bWasSuccessful, const int32 ControllerId,
FUniqueNetIdPtr UserId, const FOnlineSessionSearchResult& InviteResult)
{
if (bWasSuccessful)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Accepted Invite successfully!"));
if (IOnlineSubsystem* Subsystem = IOnlineSubsystem::Get())
{
if (IOnlineSessionPtr CurrentSession = Subsystem->GetSessionInterface())
{
CurrentSession->OnJoinSessionCompleteDelegates.AddUObject(this, &UTPGameInstance::OnJoinSessionComplete);
CurrentSession->JoinSession(0, NAME_GameSession, InviteResult);
}
}
}
}
void UTPGameInstance::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type JoinResult)
{
if (JoinResult == EOnJoinSessionCompleteResult::Type::Success)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Joined the Session successfully!"));
if (IOnlineSubsystem* Subsystem = IOnlineSubsystem::Get())
{
if (IOnlineSessionPtr CurrentSession = Subsystem->GetSessionInterface())
{
FString ConnectInfo;
if (CurrentSession->GetResolvedConnectString(SessionName, ConnectInfo))
{
GetFirstLocalPlayerController()->ClientTravel(ConnectInfo, TRAVEL_Absolute, false);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Traveling to Server"));
}
}
}
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Couldn't join session!"));
}
}
void UTPGameInstance::CreateSeassion()
{
if (IOnlineSubsystem* OnlineSub = IOnlineSubsystem::Get())
{
FOnlineSessionSettings SessionSettings;
SessionSettings.bAllowInvites = true;
SessionSettings.bAllowJoinInProgress = true;
SessionSettings.bAllowJoinViaPresence = true;
SessionSettings.bIsDedicated = false;
SessionSettings.bAntiCheatProtected = false;
SessionSettings.bIsLANMatch = false;
SessionSettings.bShouldAdvertise = true;
SessionSettings.bUseLobbiesIfAvailable = true;
SessionSettings.bUsesPresence = true;
SessionSettings.bUsesStats = true;
SessionSettings.NumPublicConnections = 4;
if (IOnlineSubsystem* Subsystem = IOnlineSubsystem::Get())
{
if (IOnlineSessionPtr CurrentSession = Subsystem->GetSessionInterface())
{
CurrentSession->OnCreateSessionCompleteDelegates.AddUObject(this, &UTPGameInstance::OnCreateSessionComplete);
CurrentSession->CreateSession(0, NAME_GameSession, SessionSettings);
}
}
}
}
// TPGameInstance.h
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "Interfaces/OnlineSessionInterface.h"
#include "OnlineSubsystem.h"
#include "OnlineSessionSettings.h"
#include "TPGameInstance.generated.h"
/**
*
*/
UCLASS()
class TPMULTIPLAYER_API UTPGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
virtual void Init() override;
void OnCreateSessionComplete(FName SessionName, bool bWasSuccessful);
void OnSessionUserInviteAccepted(const bool bWasSuccessful, const int32 ControllerId, FUniqueNetIdPtr UserId, const FOnlineSessionSearchResult& InviteResult);
void OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type JoinResult);
void OnDestroySessionComplete(FName SessionName, bool bWasSuccessful);
void CreateSeassion();
private:
FOnlineSessionSearchResult PendingInviteResult;
};
DefaultEngine.ini
[/Script/Engine.GameEngine]
+NetDriverDefinitions=(DefName="GameNetDriver",DriverClassName="OnlineSubsystemSteam.SteamNetDriver",DriverClassNameFallback="OnlineSubsystemUtils.IpNetDriver")
[OnlineSubsystem]
DefaultPlatformService=Steam
[OnlineSubsystemSteam]
bEnabled=true
SteamDevAppId=480
bUseSteamNetworking=true
[/Script/OnlineSubsystemSteam.SteamNetDriver]
NetConnectionClassName="OnlineSubsystemSteam.SteamNetConnection"
bUsesPresence=true
bUseLobbiesIfAvailable=true
AllowPeerConnections=True
AllowPeerVoice=True