サブシステムの実装 - セッション入門 - (Unreal Engine モジュール)
Session creation and leave flow
Before you begin, take a look at how the session creation and leave flow works:
About the subsystem
You are now ready to implement the Session Essentials using the AccelByte Gaming Services (AGS) Online Subsystem (OSS). In the Byte Wars module project, there is an Online Session class called SessionEssentialsOnlineSession_Starter
to handle the session implementation. This Online Session class provides necessary declarations and definitions, so you can begin using them to implement Session features.
Before we discuss the Online Session, make sure your /Config/DefaultEngine.ini
has bEnableV2Sessions
enabled as shown below. This is necessary so your game can use matchmaking features provided by the AGS OSS.
[OnlineSubsystemAccelByte]
bEnabled=true
bMultipleLocalUsersEnabled=false
bAutoLobbyConnectAfterLoginSuccess=true
...
[OnlineSubsystemAccelByte]
bEnableV2Sessions=true
You can find the SessionEssentialsOnlineSession_Starter
class files at the following locations:
- Header file:
/Source/AccelByteWars/TutorialModules/Play/SessionEssentials/SessionEssentialsOnlineSession_Starter.h
- CPP file:
/Source/AccelByteWars/TutorialModules/Play/SessionEssentials/SessionEssentialsOnlineSession_Starter.cpp
Take a look at what is provided in the class:
In the
SessionEssentialsOnlineSession_Starter
class CPP file, you will seeRegisterOnlineDelegates
andClearOnlineDelegates
functions. These functions are provided by Unreal Engine's Online Session,UOnlineSession
, the parent of this class.RegisterOnlineDelegates
will be called on Game Instance's initialization, just before Game Instance Subsystem initialization.ClearOnlineDelegates
will be called just before the game closes.The parent of this class,
UOnlineSessionClient
, is provided by Unreal Engine and comes with a function to call the Session Interface gateway to use the actual session functionality.IOnlineSessionPtr UOnlineSessionClient::GetSessionInt()
{
UWorld* World = GetWorld();
if (World == nullptr)
{
UE_LOG_ONLINE(Warning, TEXT("UOnlineSessionClient::GetSessionInt: Called with NULL world."));
return nullptr;
}
return Online::GetSessionInterface(World);
}AGS's Session Interface has some functions that is not abstracted by Unreal Engine's Session Interface. A function to get the AGS's Session Interface has been provided in the parent class.
FOnlineSessionV2AccelBytePtr UAccelByteWarsOnlineSessionBase::GetABSessionInt()
{
return StaticCastSharedPtr<FOnlineSessionV2AccelByte>(GetSessionInt());
}Still in the CPP file, you will see some helper functions:
FNamedOnlineSession* USessionEssentialsOnlineSession_Starter::GetSession(const FName SessionName)
{
return GetSessionInt()->GetNamedSession(SessionName);
}
EAccelByteV2SessionType USessionEssentialsOnlineSession_Starter::GetSessionType(const FName SessionName)
{
const FNamedOnlineSession* OnlineSession = GetSession(SessionName);
if (!OnlineSession)
{
return EAccelByteV2SessionType::Unknown;
}
const FOnlineSessionSettings& OnlineSessionSettings = OnlineSession->SessionSettings;
return GetABSessionInt()->GetSessionTypeFromSettings(OnlineSessionSettings);
}
FName USessionEssentialsOnlineSession_Starter::GetPredefinedSessionNameFromType(const EAccelByteV2SessionType SessionType)
{
FName SessionName = FName();
switch (SessionType)
{
case EAccelByteV2SessionType::GameSession:
SessionName = GameSessionName;
break;
case EAccelByteV2SessionType::PartySession:
SessionName = PartySessionName;
break;
default: ;
}
return SessionName;
}GetSession
: as the name implies, it will get the session based on the given name.GetSessionType
: an function specific to AGS Session Interface that indicates whether a session is a game session or a party session.GetPredefinedSessionNameFromType
: returns a predefinedNAME_GameSession
for a game session andNAME_PartySession
for a party session. These names were provided by Unreal Engine to differentiate which session is a game session or a party session.
Take a looking at
SessionEssentialsOnlineSession_Starter
class Header file. You will see seven delegates and their getter functions. These delegates will be your way to connect the UI to the response call when making a request.public:
virtual FOnCreateSessionComplete* GetOnCreateSessionCompleteDelegates() override
{
return &OnCreateSessionCompleteDelegates;
}
virtual FOnJoinSessionComplete* GetOnJoinSessionCompleteDelegates() override
{
return &OnJoinSessionCompleteDelegates;
}
virtual FOnSendSessionInviteComplete* GetOnSendSessionInviteCompleteDelegates() override
{
return &OnSendSessionInviteCompleteDelegates;
}
virtual FOnRejectSessionInviteCompleteMulticast* GetOnRejectSessionInviteCompleteDelegate() override
{
return &OnRejectSessionInviteCompleteDelegates;
}
virtual FOnDestroySessionComplete* GetOnLeaveSessionCompleteDelegates() override
{
return &OnLeaveSessionCompleteDelegates;
}
virtual FOnV2SessionInviteReceived* GetOnSessionInviteReceivedDelegates() override
{
return &OnSessionInviteReceivedDelegates;
}
virtual FOnSessionParticipantsChange* GetOnSessionParticipantsChange() override
{
return &OnSessionParticipantsChangeDelegates;
}
private:
FOnCreateSessionComplete OnCreateSessionCompleteDelegates;
FOnJoinSessionComplete OnJoinSessionCompleteDelegates;
FOnSendSessionInviteComplete OnSendSessionInviteCompleteDelegates;
FOnRejectSessionInviteCompleteMulticast OnRejectSessionInviteCompleteDelegates;
FOnDestroySessionComplete OnLeaveSessionCompleteDelegates;
FOnV2SessionInviteReceived OnSessionInviteReceivedDelegates;
FOnSessionParticipantsChange OnSessionParticipantsChangeDelegates;Still in the Header file, you will also see a
boolean
variablebLeavingSession
. This variable will be used for implementation in later modules.protected:
bool bLeavingSession = false;
Leave session
If you look back at the flow diagram, you will notice that the create function will leave the session in some cases, so you will start with leave session.
Start with declaring functions to request session leave. Open the
SessionEssentialsOnlineSession_Starter
class Header file and add the following function declarations:public:
virtual void LeaveSession(FName SessionName) override;Still in the Header file, add one more function declaration for the response callback:
protected:
virtual void OnLeaveSessionComplete(FName SessionName, bool bSucceeded) override;Open the
SessionEssentialsOnlineSession_Starter
class CPP file and add the code below. Before calling the request function, verify whether the player is a part of a session or not by using the utility functionGetSession()
. We will only trigger the leave session request if the player is in a session.void USessionEssentialsOnlineSession_Starter::LeaveSession(FName SessionName)
{
// safety
if (!GetSessionInt())
{
UE_LOG_ONLINESESSION(Warning, TEXT("Session interface null"))
ExecuteNextTick(FSimpleDelegate::CreateWeakLambda(this, [this, SessionName]()
{
OnLeaveSessionComplete(SessionName, false);
}));
return;
}
if (GetSession(SessionName))
{
if (!GetABSessionInt()->DestroySession(SessionName))
{
UE_LOG_ONLINESESSION(Warning, TEXT("Failed to execute"))
ExecuteNextTick(FSimpleDelegate::CreateWeakLambda(this, [this, SessionName] ()
{
OnLeaveSessionComplete(SessionName, false);
}));
}
else
{
bLeavingSession = true;
}
}
else
{
UE_LOG_ONLINESESSION(Log, TEXT("Not in session"))
ExecuteNextTick(FSimpleDelegate::CreateWeakLambda(this, [this, SessionName]()
{
OnLeaveSessionComplete(SessionName, true);
}));
}
}注記The request function to leave a session is called
DestroySession
, which leaves that session and destroys the local instance of it. The session destruction itself will be handled by the backend, so you useLeaveSession
as the name to make it clearer.Implement the response callback. Still in the CPP file, add the code below. The callback will call the delegate that will be used for other classes to execute something when the response callback is received.
void USessionEssentialsOnlineSession_Starter::OnLeaveSessionComplete(FName SessionName, bool bSucceeded)
{
UE_LOG_ONLINESESSION(Log, TEXT("succeeded: %s"), *FString(bSucceeded ? "TRUE": "FALSE"))
bLeavingSession = false;
OnLeaveSessionCompleteDelegates.Broadcast(SessionName, bSucceeded);
}Notice that the response function
OnLeaveSessionComplete
has not been called anywhere yet. Currently, the response function is not bound to the response of the leave session request. Still in the class CPP file, navigate toUSessionEssentialsOnlineSession_Starter::RegisterOnlineDelegates
and add the highlighted lines from the following code:void USessionEssentialsOnlineSession_Starter::RegisterOnlineDelegates()
{
Super::RegisterOnlineDelegates();
GetSessionInt()->AddOnDestroySessionCompleteDelegate_Handle (FOnDestroySessionCompleteDelegate::CreateUObject(this, &ThisClass::OnLeaveSessionComplete));
}Unbind the bound functions. Still in the class CPP file, navigate to
USessionEssentialsOnlineSession_Starter::ClearOnlineDelegates
and add the highlighted lines from the following code:void USessionEssentialsOnlineSession_Starter::ClearOnlineDelegates()
{
Super::ClearOnlineDelegates();
GetSessionInt()->ClearOnDestroySessionCompleteDelegates(this);
}
Create session
Declare a function to request session creation. Open the
SessionEssentialsOnlineSession_Starter
class Header file and add the following function declarations:public:
virtual void CreateSession(
const int32 LocalUserNum,
FName SessionName,
FOnlineSessionSettings SessionSettings,
const EAccelByteV2SessionType SessionType,
const FString& SessionTemplateName) override;Add another function declaration for the response callback:
protected:
virtual void OnCreateSessionComplete(FName SessionName, bool bSucceeded) override;Still in the Header file, add one more function declaration and a delegate handle variable. This function and delegate handle will be used to call
LeaveSession
when the player calls create session but is a part of another session.private:
void OnLeaveSessionForCreateSessionComplete(
FName SessionName,
bool bSucceeded,
const int32 LocalUserNum,
const FOnlineSessionSettings SessionSettings);
FDelegateHandle OnLeaveSessionForCreateSessionCompleteDelegateHandle;Create the implementation for the request function. Open the
SessionEssentialsOnlineSession_Starter
class CPP file and add the code below. Before session creation can be requested, session settings need to be set up first: Set theSessionTemplateName
that will be used to create the session with the session template name you have set up in the Set up game session, set theSessionType
as a game or a party session, and set the server name as preparation for the next module.void USessionEssentialsOnlineSession_Starter::CreateSession(
const int32 LocalUserNum,
FName SessionName,
FOnlineSessionSettings SessionSettings,
const EAccelByteV2SessionType SessionType,
const FString& SessionTemplateName)
{
// safety
if (!GetSessionInt())
{
UE_LOG_ONLINESESSION(Warning, TEXT("Session interface is null"))
ExecuteNextTick(FSimpleDelegate::CreateWeakLambda(this, [this, SessionName]()
{
OnCreateSessionComplete(SessionName, false);
}));
return;
}
if (SessionTemplateName.IsEmpty())
{
UE_LOG_ONLINESESSION(Warning, TEXT("Session Template Name can't be empty"))
ExecuteNextTick(FSimpleDelegate::CreateWeakLambda(this, [this, SessionName]()
{
OnCreateSessionComplete(SessionName, false);
}));
return;
}
#pragma region "Setup Session Settings"
// Session Template Name
SessionSettings.Set(SETTING_SESSION_TEMPLATE_NAME, SessionTemplateName);
// Session type
if (SessionType != EAccelByteV2SessionType::Unknown)
{
SessionSettings.Set(SETTING_SESSION_TYPE, GetPredefinedSessionNameFromType (SessionType).ToString());
}
// Set local server name for matchmaking request if any.
// This is useful if you want to try matchmaking using local dedicated server.
if (SessionType == EAccelByteV2SessionType::GameSession)
{
FString ServerName;
FParse::Value(FCommandLine::Get(), TEXT("-ServerName="), ServerName);
if (!ServerName.IsEmpty())
{
UE_LOG_ONLINESESSION(Log, TEXT("Requesting to use server with name: %s"), *ServerName)
SessionSettings.Set(SETTING_GAMESESSION_SERVERNAME, ServerName);
}
}
#pragma endregion
// if session exist locally -> destroy session first
if (GetSession(SessionName))
{
UE_LOG_ONLINESESSION(Log, TEXT("Session exist locally, leaving session first"))
// remove from delegate if exist
if (OnLeaveSessionForCreateSessionCompleteDelegateHandle.IsValid())
{
OnLeaveSessionCompleteDelegates.Remove (OnLeaveSessionForCreateSessionCompleteDelegateHandle);
OnLeaveSessionForCreateSessionCompleteDelegateHandle.Reset();
}
OnLeaveSessionForCreateSessionCompleteDelegateHandle = OnLeaveSessionCompleteDelegates.AddUObject(this, & ThisClass::OnLeaveSessionForCreateSessionComplete, LocalUserNum, SessionSettings);
LeaveSession(SessionName);
}
else
{
if (!GetSessionInt()->CreateSession(LocalUserNum, SessionName, SessionSettings))
{
UE_LOG_ONLINESESSION(Warning, TEXT("Failed to execute"))
ExecuteNextTick(FSimpleDelegate::CreateWeakLambda(this, [this, SessionName] ()
{
OnCreateSessionComplete(SessionName, false);
}));
}
}
}Implement the response callback function. This function will call the delegate that tells the caller's class that the request has finished executing.
void USessionEssentialsOnlineSession_Starter::OnCreateSessionComplete(FName SessionName, bool bSucceeded)
{
UE_LOG_ONLINESESSION(Log, TEXT("succeeded: %s"), *FString(bSucceeded ? "TRUE": "FALSE"))
OnCreateSessionCompleteDelegates.Broadcast(SessionName, bSucceeded);
}As seen in the
CreateSession
implementation and the diagram flow, callLeaveSession
first if the player is a part of a session as in the following code:void USessionEssentialsOnlineSession_Starter::OnLeaveSessionForCreateSessionComplete(
FName SessionName,
bool bSucceeded,
const int32 LocalUserNum,
const FOnlineSessionSettings SessionSettings)
{
UE_LOG_ONLINESESSION(Log, TEXT("succeeded: %s"), *FString(bSucceeded ? "TRUE": "FALSE"))
OnLeaveSessionCompleteDelegates.Remove(OnLeaveSessionForCreateSessionCompleteDelegateHandle);
if (bSucceeded)
{
if (!GetSessionInt()->CreateSession(LocalUserNum, SessionName, SessionSettings))
{
UE_LOG_ONLINESESSION(Warning, TEXT("Failed to execute"))
OnCreateSessionComplete(SessionName, false);
}
}
else
{
// Leave Session failed, execute complete delegate as failed
OnCreateSessionComplete(SessionName, false);
}
}Bind the
OnCreateSessionComplete
function to the response callback. Navigate toRegisterOnlineDelegates
in the CPP file and add the highlighted line in the following code:void USessionEssentialsOnlineSession_Starter::RegisterOnlineDelegates()
{
Super::RegisterOnlineDelegates();
GetSessionInt()->AddOnDestroySessionCompleteDelegate_Handle (FOnDestroySessionCompleteDelegate::CreateUObject(this, &ThisClass::OnLeaveSessionComplete));
GetSessionInt()->AddOnCreateSessionCompleteDelegate_Handle(FOnCreateSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnCreateSessionComplete));
}Unbind the callback when the player exits the game. Add the highlighted code into the
ClearOnlineDelegates
function.void USessionEssentialsOnlineSession_Starter::ClearOnlineDelegates()
{
Super::ClearOnlineDelegates();
GetSessionInt()->ClearOnDestroySessionCompleteDelegates(this);
GetSessionInt()->ClearOnCreateSessionCompleteDelegates(this);
}
Join session
In the
SessionEssentialsOnlineSession_Starter
class Header file, add a function declaration for the request function:public:
virtual void JoinSession(
const int32 LocalUserNum,
FName SessionName,
const FOnlineSessionSearchResult& SearchResult) override;Add another declaration for the response callback function:
protected:
virtual void OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result) override;Still in the Header file, add one more function declaration and a delegate to handle the leave session first if the player calling join session is already part of a session:
private:
void OnLeaveSessionForJoinSessionComplete(
FName SessionName,
bool bSucceeded,
const int32 LocalUserNum,
const FOnlineSessionSearchResult SearchResult);
FDelegateHandle OnLeaveSessionForJoinSessionCompleteDelegateHandle;Open the
SessionEssentialsOnlineSession_Starter
class CPP file and add the code below. For join session implementation, this function will callJoinSession
without any setup, but if the player is part of a session, it will callLeaveSession
first.void USessionEssentialsOnlineSession_Starter::JoinSession(
const int32 LocalUserNum,
FName SessionName,
const FOnlineSessionSearchResult& SearchResult)
{
// safety
if (!GetSessionInt())
{
UE_LOG_ONLINESESSION(Warning, TEXT("Session interface null"))
ExecuteNextTick(FSimpleDelegate::CreateWeakLambda(this, [this, SessionName]()
{
OnJoinSessionComplete(SessionName, EOnJoinSessionCompleteResult::UnknownError);
}));
return;
}
// If session exist -> destroy first then join
if (GetSession(SessionName))
{
// remove from delegate if exist
if (OnLeaveSessionForJoinSessionCompleteDelegateHandle.IsValid())
{
OnLeaveSessionCompleteDelegates.Remove(OnLeaveSessionForJoinSessionCompleteDelegateHandle);
OnLeaveSessionForJoinSessionCompleteDelegateHandle.Reset();
}
OnLeaveSessionForJoinSessionCompleteDelegateHandle = OnLeaveSessionCompleteDelegates.AddUObject(this, &ThisClass::OnLeaveSessionForJoinSessionComplete, LocalUserNum, SearchResult);
LeaveSession(SessionName);
}
else
{
if (!GetSessionInt()->JoinSession(LocalUserNum, SessionName, SearchResult))
{
UE_LOG_ONLINESESSION(Warning, TEXT("Failed to execute"))
ExecuteNextTick(FSimpleDelegate::CreateWeakLambda(this, [this, SessionName]()
{
OnJoinSessionComplete(SessionName, EOnJoinSessionCompleteResult::UnknownError);
}));
}
}
}Implement the callback function. Just like the before, this function will simply call the corresponding delegate.
void USessionEssentialsOnlineSession_Starter::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result)
{
UE_LOG_ONLINESESSION(Log, TEXT("succeeded: %s"), *FString(Result == EOnJoinSessionCompleteResult::Success ? "TRUE" : "FALSE"))
OnJoinSessionCompleteDelegates.Broadcast(SessionName, Result);
}Add the following code to the CPP file to implement joining a session after the leave session:
void USessionEssentialsOnlineSession_Starter::OnLeaveSessionForJoinSessionComplete(
FName SessionName,
bool bSucceeded,
const int32 LocalUserNum,
const FOnlineSessionSearchResult SearchResult)
{
UE_LOG_ONLINESESSION(Log, TEXT("succeeded: %s"), *FString(bSucceeded ? "TRUE": "FALSE"))
OnLeaveSessionCompleteDelegates.Remove(OnLeaveSessionForJoinSessionCompleteDelegateHandle);
if (bSucceeded)
{
if (!GetSessionInt()->JoinSession(LocalUserNum, SessionName, SearchResult))
{
UE_LOG_ONLINESESSION(Warning, TEXT("failed to execute"))
OnJoinSessionComplete(SessionName, EOnJoinSessionCompleteResult::UnknownError);
}
}
else
{
// Leave Session failed, execute complete delegate as failed
OnJoinSessionComplete(SessionName, EOnJoinSessionCompleteResult::UnknownError);
}
}Still in the CPP file, navigate to
RegisterOnlineDelegates
function and bind the response callback to the corresponding OSS delegate by adding the highlighted code below:void USessionEssentialsOnlineSession_Starter::RegisterOnlineDelegates()
{
Super::RegisterOnlineDelegates();
GetSessionInt()->AddOnDestroySessionCompleteDelegate_Handle (FOnDestroySessionCompleteDelegate::CreateUObject(this, &ThisClass::OnLeaveSessionComplete));
GetSessionInt()->AddOnCreateSessionCompleteDelegate_Handle(FOnCreateSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnCreateSessionComplete));
GetSessionInt()->AddOnJoinSessionCompleteDelegate_Handle(FOnJoinSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnJoinSessionComplete));
}Navigate to
ClearOnlineDelegates
and unbind the response callback.void USessionEssentialsOnlineSession_Starter::ClearOnlineDelegates()
{
Super::ClearOnlineDelegates();
GetSessionInt()->ClearOnDestroySessionCompleteDelegates(this);
GetSessionInt()->ClearOnCreateSessionCompleteDelegates(this);
GetSessionInt()->ClearOnJoinSessionCompleteDelegates(this);
}
Send and receive session invitations
Open the
SessionEssentialsOnlineSession_Starter
Header file. Add this function declaration for the request function:public:
virtual void SendSessionInvite(const int32 LocalUserNum, FName SessionName, const FUniqueNetIdPtr Invitee) override;Add this function declaration for the response callback.
protected:
virtual void OnSendSessionInviteComplete(
const FUniqueNetId& LocalSenderId,
FName SessionName,
bool bSucceeded,
const FUniqueNetId& InviteeId) override;Still in the Header file, add a function declaration that will be called upon receiving an invitation.
protected:
virtual void OnSessionInviteReceived(
const FUniqueNetId& UserId,
const FUniqueNetId& FromId,
const FOnlineSessionInviteAccelByte& Invite) override;Open the
SessionEssentialsOnlineSession_Starter
CPP file and add the code below for theSendSessionInvite
function. This function will call the corresponding OSS function.void USessionEssentialsOnlineSession_Starter::SendSessionInvite(
const int32 LocalUserNum,
FName SessionName,
const FUniqueNetIdPtr Invitee)
{
if (!Invitee.IsValid())
{
UE_LOG_ONLINESESSION(Log, TEXT("Invitee net id is invalid. Cancelling operation"));
return;
}
GetABSessionInt()->SendSessionInviteToFriend(LocalUserNum, SessionName, *Invitee.Get());
}Still in the CPP file, add the code below for the
OnSendSessionInviteComplete
function. This function will also call the corresponding delegate to let other objects know when the response was received.void USessionEssentialsOnlineSession_Starter::OnSendSessionInviteComplete(
const FUniqueNetId& LocalSenderId,
FName SessionName,
bool bSucceeded,
const FUniqueNetId& InviteeId)
{
UE_LOG_ONLINESESSION(Log, TEXT("succeeded: %s"), *FString(bSucceeded ? "TRUE" : "FALSE"))
OnSendSessionInviteCompleteDelegates.Broadcast(LocalSenderId, SessionName, bSucceeded, InviteeId);
}Implement the invitation received function. Just like the
OnSendSessionInviteComplete
, this function will call the corresponding delegate.void USessionEssentialsOnlineSession_Starter::OnSessionInviteReceived(
const FUniqueNetId& UserId,
const FUniqueNetId& FromId,
const FOnlineSessionInviteAccelByte& Invite)
{
UE_LOG_ONLINESESSION(Log, TEXT("from: %s"), *FromId.ToDebugString())
OnSessionInviteReceivedDelegates.Broadcast(UserId, FromId, Invite);
}Bind the response callback and the invitation received to the corresponding OSS delegate. Still on the CPP file, navigate to the
RegisterOnlineDelegates
function and add the highlighted code below:void USessionEssentialsOnlineSession_Starter::RegisterOnlineDelegates()
{
Super::RegisterOnlineDelegates();
GetSessionInt()->AddOnDestroySessionCompleteDelegate_Handle (FOnDestroySessionCompleteDelegate::CreateUObject(this, &ThisClass::OnLeaveSessionComplete));
GetSessionInt()->AddOnCreateSessionCompleteDelegate_Handle(FOnCreateSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnCreateSessionComplete));
GetSessionInt()->AddOnJoinSessionCompleteDelegate_Handle(FOnJoinSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnJoinSessionComplete));
GetABSessionInt()->AddOnSendSessionInviteCompleteDelegate_Handle(FOnSendSessionInviteCompleteDelegate::CreateUObject(this, &ThisClass::OnSendSessionInviteComplete));
GetABSessionInt()->AddOnV2SessionInviteReceivedDelegate_Handle(FOnV2SessionInviteReceivedDelegate::CreateUObject(this, &ThisClass::OnSessionInviteReceived));
}Unbind those callbacks. Navigate to the
ClearOnlineDelegates
function and add the highlighted code below:void USessionEssentialsOnlineSession_Starter::ClearOnlineDelegates()
{
Super::ClearOnlineDelegates();
GetSessionInt()->ClearOnDestroySessionCompleteDelegates(this);
GetSessionInt()->ClearOnCreateSessionCompleteDelegates(this);
GetSessionInt()->ClearOnJoinSessionCompleteDelegates(this);
GetABSessionInt()->ClearOnSendSessionInviteCompleteDelegates(this);
GetABSessionInt()->ClearOnV2SessionInviteReceivedDelegates(this);
}
Reject session invitation
Open the
SessionEssentialsOnlineSession_Starter
Header file and add the following function declaration. This function is responsible for requesting the reject functionality.public:
virtual void RejectSessionInvite(const int32 LocalUserNum, const FOnlineSessionInviteAccelByte& Invite) override;Still in the
SessionEssentialsOnlineSession_Starter
Header file, add the declaration for the response callback.protected:
virtual void OnRejectSessionInviteComplete(bool bSucceeded) override;Open the
SessionEssentialsOnlineSession_Starter
CPP file and add the implementation below for theRejectSessionInvite
function. The logic retrieves the local user's unique ID before calling the corresponding OSS function. Notice that, instead of binding the response callback to a delegate, theRejectInvite
OSS function takes the response callback as the parameter, removing the need to separately bind and unbind the response delegate.void USessionEssentialsOnlineSession_Starter::RejectSessionInvite(
const int32 LocalUserNum,
const FOnlineSessionInviteAccelByte& Invite)
{
const FUniqueNetIdPtr LocalUserNetId = GetLocalPlayerUniqueNetId(GetPlayerControllerByLocalUserNum(LocalUserNum));
if (!LocalUserNetId.IsValid())
{
UE_LOG_ONLINESESSION(Log, TEXT("Local User net id is invalid. Cancelling operation"));
return;
}
GetABSessionInt()->RejectInvite(
*LocalUserNetId.Get(),
Invite,
FOnRejectSessionInviteComplete::CreateUObject(this, &ThisClass::OnRejectSessionInviteComplete));
}Still in the CPP file, add this implementation for the response callback:
void USessionEssentialsOnlineSession_Starter::OnRejectSessionInviteComplete(bool bSucceeded)
{
UE_LOG_ONLINESESSION(Log, TEXT("succeeded: %s"), *FString(bSucceeded ? "TRUE" : "FALSE"))
OnRejectSessionInviteCompleteDelegates.Broadcast(bSucceeded);
}
Session participant changed callback
This callback will be called when other players join and leave the session.
Open the
SessionEssentialsOnlineSession_Starter
Header file and add this declaration:protected:
virtual void OnSessionParticipantsChange(FName SessionName, const FUniqueNetId& Member, bool bJoined) override;Open the
SessionEssentialsOnlineSession_Starter
CPP file and add the code below. This function will call the corresponding delegate.void USessionEssentialsOnlineSession_Starter::OnSessionParticipantsChange(
FName SessionName,
const FUniqueNetId& Member,
bool bJoined)
{
UE_LOG_ONLINESESSION(
Log,
TEXT("session name: %s | Member: %s [%s]"),
*SessionName.ToString(),
*Member.ToDebugString(),
*FString(bJoined ? "Joined" : "Left"))
OnSessionParticipantsChangeDelegates.Broadcast(SessionName, Member, bJoined);
}Bind the callback to the corresponding OSS delegate. Navigate to
RegisterOnlineDelegates
and add the highlighted code:void USessionEssentialsOnlineSession_Starter::RegisterOnlineDelegates()
{
Super::RegisterOnlineDelegates();
GetSessionInt()->AddOnDestroySessionCompleteDelegate_Handle (FOnDestroySessionCompleteDelegate::CreateUObject(this, &ThisClass::OnLeaveSessionComplete));
GetSessionInt()->AddOnCreateSessionCompleteDelegate_Handle(FOnCreateSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnCreateSessionComplete));
GetSessionInt()->AddOnJoinSessionCompleteDelegate_Handle(FOnJoinSessionCompleteDelegate::CreateUObject(this, &ThisClass::OnJoinSessionComplete));
GetABSessionInt()->AddOnSendSessionInviteCompleteDelegate_Handle(FOnSendSessionInviteCompleteDelegate::CreateUObject(this, &ThisClass::OnSendSessionInviteComplete));
GetABSessionInt()->AddOnV2SessionInviteReceivedDelegate_Handle(FOnV2SessionInviteReceivedDelegate::CreateUObject(this, &ThisClass::OnSessionInviteReceived));
GetABSessionInt()->AddOnSessionParticipantsChangeDelegate_Handle(FOnSessionParticipantsChangeDelegate::CreateUObject(this, &ThisClass::OnSessionParticipantsChange));
}Navigate to
ClearOnlineDelegates
and unbind the callback by adding this highlighted code:void USessionEssentialsOnlineSession_Starter::ClearOnlineDelegates()
{
Super::ClearOnlineDelegates();
GetSessionInt()->ClearOnDestroySessionCompleteDelegates(this);
GetSessionInt()->ClearOnCreateSessionCompleteDelegates(this);
GetSessionInt()->ClearOnJoinSessionCompleteDelegates(this);
GetABSessionInt()->ClearOnSendSessionInviteCompleteDelegates(this);
GetABSessionInt()->ClearOnV2SessionInviteReceivedDelegates(this);
GetABSessionInt()->ClearOnSessionParticipantsChangeDelegates(this);
}
Compile your project again and make sure it has no compile errors.
Resources
- The files used in this tutorial section are available in the Byte Wars Unreal GitHub repository.