Download

Create a Friend System (multiplatform)

Hello! I’m doing a multiplayer game (Client-Server). We want to be able to play on Steam and PS4. I have done session management but now I want to be able to invite friends to the session. And also go to your sessions. There aren’t tutorials for this. And there are two interfaces that allow you to invite friends. What are the differences between FOnlineFriendsInterface::SendInvite and FOnlineSessionInterface::SendSessionInviteToFriend? How do I manage when a request arrives? What if he accepts or cancels it?
The person who invites, does he need to have a created session or is it automatic? The friend who accepts, does he have to join (with the function)?

I couldn’t find the answer to this question: What are the differences between FOnlineFriendsInterface::SendInvite and FOnlineSessionInterface::SendSessionInviteToFriend?

But the others I could.

How do I manage when a request arrives? I used the GameInstance to handle it.
The person who invites, does he need to have a created session or is it automatic? YOU NEED IT. The session is necessary before sending the request.
The friend who accepts, does he have to join (with the function)? YES.

.h

class SESSIONPROJECTCPP_API USPGameInstance :
public UGameInstance
{
GENERATED_BODY()
public:
USPGameInstance(const FObjectInitializer& ObjectInitializer);
virtual void Init() override;
virtual void Shutdown() override;
FDelegateHandle SessionInviteReceivedDelegateHandle;
FDelegateHandle SessionInviteAcceptedDelegateHandle;
UFUNCTION(BlueprintCallable, Category = “Online|Friends”)
bool SendSessionInviteToFriend(APlayerController *PlayerController, const FBlueprintUniqueNetId &FriendUniqueNetId);

private:
FOnSessionInviteReceivedDelegate SessionInviteReceivedDelegate;
FOnSessionUserInviteAcceptedDelegate SessionInviteAcceptedDelegate;
void OnSessionInviteReceivedMaster(const FUniqueNetId & PersonInvited, const FUniqueNetId & PersonInviting, const FString & AppId, const FOnlineSessionSearchResult& SessionToJoin);
void OnSessionInviteAcceptedMaster(const bool bWasSuccessful, int32 LocalPlayer, TSharedPtr<const FUniqueNetId> PersonInviting, const FOnlineSessionSearchResult& SessionToJoin);
}

.cpp

USPGameInstance::USPGameInstance(const FObjectInitializer& ObjectInitializer) :
Super(ObjectInitializer) ,
SessionInviteReceivedDelegate(FOnSessionInviteReceivedDelegate::CreateUObject(this, &ThisClass::OnSessionInviteReceivedMaster)) ,
SessionInviteAcceptedDelegate(FOnSessionUserInviteAcceptedDelegate::CreateUObject(this, &ThisClass::OnSessionInviteAcceptedMaster))
{ }

void USPGameInstance::Init()
{
IOnlineSessionPtr SessionInterface = Online::GetSessionInterface();
if (SessionInterface.IsValid())
{
SessionInviteAcceptedDelegateHandle = SessionInterface->AddOnSessionUserInviteAcceptedDelegate_Handle(SessionInviteAcceptedDelegate);
SessionInviteReceivedDelegateHandle = SessionInterface->AddOnSessionInviteReceivedDelegate_Handle(SessionInviteReceivedDelegate);
}
Super::Init();
}

void USPGameInstance::Shutdown()
{
IOnlineSessionPtr SessionInterface = Online::GetSessionInterface();
if (SessionInterface.IsValid())
{
SessionInterface->ClearOnSessionUserInviteAcceptedDelegate_Handle(SessionInviteAcceptedDelegateHandle);
SessionInterface->ClearOnSessionInviteReceivedDelegate_Handle(SessionInviteReceivedDelegateHandle);
}
Super::Shutdown();
}

bool USPGameInstance::SendSessionInviteToFriend(APlayerController PlayerController, const FBlueprintUniqueNetId &FriendUniqueNetId)
{
bool bResult = false;
if (!PlayerController)
{
return bResult;
}
if (!FriendUniqueNetId.IsValid())
{
return bResult;
}
IOnlineSessionPtr SessionInterface = Online::GetSessionInterface();
if (!SessionInterface.IsValid())
{
return bResult;
}
ULocalPlayer
Player = Cast<ULocalPlayer>(PlayerController->Player);
if (!Player)
{
return bResult;
}
/** You can skip this function because SendSessionInviteToFriend will return false if there isnt a session /
FNamedOnlineSession
Session = SessionInterface->GetNamedSession(GameSessionName);
if (Session != nullptr)
{
bResult = SessionInterface->SendSessionInviteToFriend(Player->GetControllerId(), GameSessionName, FriendUniqueNetId.GetUniqueNetId());
return bResult;
}
else
{
/
SKIP this part if you dont want to create the session “automatic” /
TArray<FBPSessionPropertyKeyPair> EmptyExtraSettings;
/
my extra settings /
bResult = CreateParty(EmptyExtraSettings,true); /
my function /
/
I need these variables to handle the creation of the session. In CreateComplete function, before the OpenLevel, I use SendSessionInviteToFriend **/
bCreatingPrivateSession = bResult;
UniqueNetIdCreatingPrivateSession = FriendUniqueNetId;
PlayerCreatingPrivateSession = Player;
}
return bResult;
}

void USPGameInstance::OnSessionInviteReceivedMaster(const FUniqueNetId & PersonInvited, const FUniqueNetId & PersonInviting, const FString& AppId, const FOnlineSessionSearchResult& SessionToJoin)
{
// USELESS
}

void USPGameInstance::OnSessionInviteAcceptedMaster(const bool bWasSuccessful, int32 LocalPlayer, TSharedPtr<const FUniqueNetId> PersonInvited, const FOnlineSessionSearchResult& SessionToJoin)
{
if (PersonInvited.IsValid() && SessionToJoin.IsValid())
{
FBlueprintSessionResult SessionSearch; SessionSearch.OnlineResult = SessionToJoin; JoinParty(SessionSearch);/** my function /
}
}
/
******************************************************************/

Y’all trash