Skip to main content

Destroying a session

When your server has finished hosting a game, or when the game client disconnects from a server early, you need to destroy the local session.

Destroying a session

First, get the online session interface:

#include "OnlineSubsystem.h"
#include "OnlineSubsystemUtils.h"
#include "Interfaces/OnlineSessionInterface.h"

// ...

IOnlineSubsystem* Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineSessionPtr Session = Subsystem->GetSessionInterface();

Register the event handler so you know when you've successfully destroyed the session. DestroySessionDelegateHandle is declared as an FDelegateHandle.

this->DestroySessionDelegateHandle =
Session->AddOnDestroySessionCompleteDelegate_Handle(FOnDestroySessionComplete::FDelegate::CreateUObject(
this,
&UMyClass::HandleDestroySessionComplete));

Using the search result you previously got from a search, call DestroySession:

// "MyLocalSessionName" is the local name of the session for the client or server. It should be the value you specified when creating or joining the session (depending on which you called).
if (!Session->DestroySession(FName(TEXT("MyLocalSessionName"))))
{
// Call didn't start, return error.
}

When your callback fires, check whether the destroy operation was successful, and deregister the event handler:

void UMyClass::HandleDestroySessionComplete(
FName SessionName,
bool bWasSuccessful)
{
// TODO: Check bWasSuccessful to see if the session was created.

// Deregister the event handler.
IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineSessionPtr Session = Subsystem->GetSessionInterface();
Session->ClearOnDestroySessionCompleteDelegate_Handle(this->DestroySessionDelegateHandle);
this->DestroySessionDelegateHandle.Reset();
}