Deleting a friend from the friends list
A user might want to delete a friend that they previously added to their friends list. You can delete friends from the friends list, where those friends were added through SendInvite
or AcceptInvite
.
caution
You can't delete friends if they're friends from a delegated subsystem (such as Steam). To know if you can delete a friend from the friends list, check the deletable
user attribute. It will be true
if the friend can be deleted.
Deleting a friend from the friends list
- C++
- Blueprints
To delete a friend from the friends list, use DeleteFriend
. You'll first need to get the friends interface:
#include "OnlineSubsystem.h"
#include "OnlineSubsystemUtils.h"
#include "Interfaces/OnlineFriendsInterface.h"
// ...
IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineFriendsPtr Friends = Subsystem->GetFriendsInterface();
Then, call DeleteFriend
with the user ID of the target player:
// Store the delegate handle, as you'll need to unregister it
// in the OnDeleteFriendComplete callback.
this->DeleteFriendCompleteHandle = Friends->AddOnDeleteFriendCompleteDelegate_Handle(
0 /* LocalUserNum */,
FOnDeleteFriendCompleteDelegate::CreateUObject(
this,
&UMyClass::OnDeleteFriendComplete));
if (!Friends->DeleteFriend(
0 /* LocalUserNum */,
TargetUserId /* The friend to delete */,
TEXT("") /* ListName, unused by EOS */))
{
// Call failed to start
}
// ...
void UMyClass::OnDeleteFriendComplete(
int32 LocalUserNum,
bool bWasSuccessful,
const FUniqueNetId &FriendId,
const FString &ListName,
const FString &ErrorStr)
{
IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineFriendsPtr Friends = Subsystem->GetFriendsInterface();
// NOTE: If you're sending multiple deletions in parallel, you'll need to
// capture the original FriendId for the DeleteFriend call as a user parameter
// to this callback, and then compare the FriendId the event is for with the
// FriendId you called DeleteFriend for. You'll also need to store the delegate
// handle (DeleteFriendCompleteHandle) per friend you are deleting.
// Remove the event handler.
Friends->ClearOnDeleteFriendCompleteDelegate_Handle(LocalUserNum, this->DeleteFriendCompleteHandle);
if (bWasSuccessful)
{
// Friend was successfully deleted. The "delete" notification to tell
// the friend to remove us from their friends list might be
// stored for later sending if the friend is currently offline.
}
else
{
// The friend could not be deleted.
}
}