[BLOG] C++ Session Create/Find/Join/Destroy

Good start. I feel that it would be easier if we talked about it in a more bare bones way so that it’s clearly understood what exactly is required vs optional.

Dependencies (in [Project].build.cs):
Public: “OnlineSubsystem”, “OnlineSubsystemUtils”, “OnlineSubsystemNull”.
Dynamic: “OnlineSubsystemNull”.
Can substitute null with Steam if that’s what you want.

In DefaultEngine.ini:


[OnlineSubsystem]
DefaultPlatformService=Null

Can substitute null with Steam if that’s what you want.

Header include:
#include “OnlineSubsystemUtils.h”
Put this wherever you deal with the Online Subsystem directly.

For creating a game this is what I did for creating sessions:

.h



FOnCreateSessionCompleteDelegate CreateSessionCompleteD;
FDelegateHandle CreateSessionCompleteDH;
void CreateSessionComplete(FName SessionName, bool bWasSuccessful);
FOnlineSessionSettings SessionSettings;


.cpp



void ANetworkTest_GameSession::BeginPlay()
{
	IOnlineSubsystem* OSInst = IOnlineSubsystem::Get();
	IOnlineSessionPtr SessionInst = OSInst->GetSessionInterface();

	CreateSessionCompleteD = FOnCreateSessionCompleteDelegate::CreateUObject(this, &ANetworkTest_GameSession::CreateSessionComplete);
	CreateSessionCompleteDH = SessionInst->AddOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteD);
}

void ANetworkTest_GameSession::Host()
{
	SessionSettings.bIsLANMatch = true;
	SessionSettings.bAllowJoinInProgress = true;
	SessionSettings.bUsesPresence = true;   // What is presence?
	SessionSettings.bShouldAdvertise = true;

	IOnlineSubsystem* OSInst = IOnlineSubsystem::Get();
	IOnlineSessionPtr SessionInst = OSInst->GetSessionInterface();

	// If session isn't created then one might already exist so try to destroy it.	
	if (SessionInst->CreateSession(0, "cheese", SessionSettings))
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, "Yes: Create");
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, "No: Create");

		if (SessionInst->DestroySession("cheese"))
			GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, "Yes: Delete");
		else
			GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, "No: Delete");
	}
}

void ANetworkTest_GameSession::CreateSessionComplete(FName SessionName, bool bWasSuccessful)
{
	if (bWasSuccessful)
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, "Pass: Create");
	else
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, "Fail: Create");

	UGameplayStatics::OpenLevel(GetWorld(), "Sandbox", true, "listen");
}


I’ve tried to keep it as simple as possible so I know what’s required. My Host function for example takes no parameters and this makes it clear that it’s not directly an important function.

Edit:

I’ve asked before, but hopefully someone who knows will see it here:

I’m hoping someone could tell me why setting a delegate wasn’t made as simple as MySessionPtr->CreateSession(0, “SessionName”, this, &MyClass::CallThisWhenCreated, &MyClass::CallThisWhenFailed). The advantage here is that I don’t need to remember 4 or so extra steps for Unreal’s custom delegate system.

Also the way we set the game session is quite odd. Why isn’t it as simple as setting a variable for like DefaultPawnClass?

1 Like