Send and respond to friend invites #

Players can become friends by sending and accepting friend invites.

The FriendApi in the Player SDK supports this by providing APIs to initiate and respond to invites, processing of notifications, and triggering events.

Send a friend invite #

There are two apis for sending invites, one uses a player’s display name and the other uses the player’s social id.

This guide is based on an example Unreal project using the Third Person Unreal C++ project template. The example project is named ‘Narwhal’, update references accordingly.

Source\Narwhal\NarwhalPlayerController.h


// ...
class NARWHAL_API ANarwhalPlayerController : public APlayerController
{
    GENERATED_BODY()

public:
  // ...

	UFUNCTION(Exec)
	void SendFriendInviteBySocialId(const FString& SocialId);

	UFUNCTION(Exec)
	void SendFriendInviteByName(const FString& DisplayName, const FString& Discriminator);

  // ...
};

Source\Narwhal\NarwhalPlayerController.cpp

void ANarwhalPlayerController::SendFriendInviteBySocialId(
	const FString &SocialId) {
	Player->FriendApi().SendFriendInviteBySocialId(
		SocialId,
		FOnCompleteDelegate::CreateUObject(
			this, &ANarwhalPlayerController::OnFriendUpdate));
}

void ANarwhalPlayerController::SendFriendInviteByName(
    const FString &DisplayName, const FString &Discriminator) {
  Player->FriendApi().SendFriendInviteByDisplayName(
      {DisplayName, Discriminator},
      FOnCompleteDelegate::CreateUObject(
          this, &ANarwhalPlayerController::OnFriendUpdate));
}

Accept or decline a friend invite #

To accept a friend invite, use the Friend API’s AcceptFriendInvite. When accepted, the inviter and invitee are added to each other’s friend list. Use the DeclineFriendInvite API to allow a player to decline an invite. Players can only have a max of 20 received invites at any time.

Source\Narwhal\NarwhalPlayerController.h


// ...
class NARWHAL_API ANarwhalPlayerController : public APlayerController
{
    GENERATED_BODY()

public:
  // ...

  	UFUNCTION(Exec)
	void ViewAndRespondToFriendInvites();

	UFUNCTION(Exec)
	void AcceptFriendInvite(const FString& SocialId);

	UFUNCTION(Exec)
	void DeclineFriendInvite(const FString& SocialId);
  // ...
};

Source\Narwhal\NarwhalPlayerController.cpp

void ANarwhalPlayerController::ViewAndRespondToFriendInvites() {
  // access pending invites 
  if (const auto InvitesPtr = Player->FriendApi().GetReceivedInvites();
      InvitesPtr.IsValid()) {

    for (TPair<FString, FPragmaFriendOverview> Invite :
         InvitesPtr.Get()->Array()) {
      UE_LOG(LogTemp, Log, TEXT("Player %s wants to be friends."),
             *Invite.Value.DisplayName().DisplayName);
      // use FPragmaFriendOverview.SocialId() to call other friend apis
      AcceptFriendInvite(Invite.Value.SocialId());
    }
  }
}

void ANarwhalPlayerController::AcceptFriendInvite(const FString &SocialId) {
	Player->FriendApi().AcceptFriendInvite(
		SocialId,
		FOnCompleteDelegate::CreateUObject(
			this, &ANarwhalPlayerController::OnFriendUpdate));
}

void ANarwhalPlayerController::DeclineFriendInvite(const FString &SocialId) {
  Player->FriendApi().DeclineFriendInvite(
      SocialId, FOnCompleteDelegate::CreateUObject(
                    this, &ANarwhalPlayerController::OnFriendUpdate));
}

Cancel a friend invite #

Players are limited to 20 pending invites. You can allow players to cancel previously sent friend invites using the FriendApi.CancelFriendInvite.

Source\Narwhal\NarwhalPlayerController.h


// ...
class NARWHAL_API ANarwhalPlayerController : public APlayerController
{
    GENERATED_BODY()

public:
  // ...

	UFUNCTION(Exec)
	void CancelFriendInvite(const FString& SocialId);

  // ...
};

Source\Narwhal\NarwhalPlayerController.cpp

void ANarwhalPlayerController::CancelFriendInvite(const FString &SocialId) {
  Player->FriendApi().CancelFriendInvite(
      SocialId, FOnCompleteDelegate::CreateUObject(
                    this, &ANarwhalPlayerController::OnFriendUpdate));
}}

Remove friend #

Players can remove friends using the RemoveFriend api.

Source\Narwhal\NarwhalPlayerController.h


// ...
class NARWHAL_API ANarwhalPlayerController : public APlayerController
{
    GENERATED_BODY()

public:
  // ...

	UFUNCTION(Exec)
	void RemoveFriend(const FString& SocialId);

  // ...
};

Source\Narwhal\NarwhalPlayerController.cpp

void ANarwhalPlayerController::RemoveFriend(const FString &SocialId) {
  Player->FriendApi().RemoveFriend(
      SocialId, FOnCompleteDelegate::CreateUObject(
                    this, &ANarwhalPlayerController::OnFriendUpdate));
}