Skip to main content

Friend Interactions

Last updated on

These are all features for how players can manage their friends.

Retrieve a Player's Public Code

You can retrieve a player's public code using the PublicId parameter.

const FString& InUserIds = "userId1, userId2";
Api::UserProfile UserProfileApi(InCredentials, FRegistry::Settings, FRegistry::HttpRetryScheduler);
UserProfileApi.BatchGetPublicUserProfileInfos(InUserIds, THandler<TArray<FAccelByteModelsPublicUserProfileInfo>>::CreateLambda([&](const TArray<FAccelByteModelsPublicUserProfileInfo>& Result)
{
UE_LOG(LogTemp, Log, TEXT("Public Id - User 1 = %s"), *Result[0].PublicId);
UE_LOG(LogTemp, Log, TEXT("Public Id - User 2 = %s"), *Result[1].PublicId);
}),
FErrorHandler::CreateLambda([&](int32 ErrorCode, const FString& ErrorMessage)
{
UE_LOG(LogTemp, Warning, TEXT("Error %d: %s"), ErrorCode, *ErrorMessage);
}));

Retrieve the Friend List

Use the following function to retrieve a friends list.

FRegistry::Lobby.Connect();
FRegistry::Lobby.SetLoadFriendListResponseDelegate(AccelByte::Api::Lobby::FLoadFriendListResponse::CreateLambda([](const FAccelByteModelsLoadFriendListResponse& Result)
{
// Do something if LoadFriendListResponseDelegate has been successful
}), FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something if LoadFriendListResponseDelegate has an error
UE_LOG(LogTemp, Log, TEXT("Error LoadFriendListResponseDelegate, Error Code: %d Error Message: %s"), ErrorCode, *ErrorMessage);
}));

FRegistry::Lobby.LoadFriendsList();


Send a Friend Request using a User ID

The first step in making a friend is sending a friend request to another player. Use this code to send a friend request using a User ID.

FRegistry::Lobby.Connect();
FRegistry::Lobby.SetRequestFriendsResponseDelegate(AccelByte::Api::Lobby::FRequestFriendsResponse::CreateLambda([](const FAccelByteModelsRequestFriendsResponse& Result)
{
// Do something if RequestFriendsResponseDelegate has been successful
}), FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something if RequestFriendsResponseDelegate has an error
UE_LOG(LogTemp, Log, TEXT("Error RequestFriendsResponseDelegate, Error Code: %d Error Message: %s"), ErrorCode, *ErrorMessage);
}));

FString UserId = FString("SomeUserId");
FRegistry::Lobby.RequestFriend(UserId);

Send a Friend Request using a Public Code

The first step in making a friend is sending a friend request to another player. Use this code to send a friend request using a Public Code.

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();

const auto OnRequestFriendByPublicIdDone = Api::Lobby::FRequestFriendsResponse::CreateLambda([](const FAccelByteModelsRequestFriendsResponse& Result)
{
if(Result.Code == "0")
{
// do something when request success
}
else
{
// do something when request failed
}
});

ApiClient->Lobby.SetRequestFriendsByPublicIdResponseDelegate(OnRequestFriendByPublicIdDone);

ApiClient->Lobby.RequestFriendByPublicId(PublicId);

Retrieve a List of Incoming Friend Requests

Use this function to retrieve all the information about incoming friend requests. This function retrieves user ID which you can use to accept or reject each request.

FRegistry::Lobby.Connect();
FRegistry::Lobby.SetListIncomingFriendsResponseDelegate(AccelByte::Api::Lobby::FListIncomingFriendsResponse::CreateLambda([](const FAccelByteModelsListIncomingFriendsResponse& Result)
{
if (Result.Code == "0")
{
// Do something if ListIncomingFriendsResponseDelegate has been successful
}
else
{
// Do something if ListIncomingFriendsResponseDelegate has an error
}
}));

FRegistry::Lobby.ListIncomingFriends();

Accept Incoming Friend Requests

After a friend request has been sent, the player who received the request can either accept or reject it. Use the following function to accept a friend request.

note

To see a list of incoming friend requests, you can retrieve a list of incoming friend requests.

FRegistry::Lobby.Connect();
FRegistry::Lobby.SetAcceptFriendsResponseDelegate(AccelByte::Api::Lobby::FAcceptFriendsResponse::CreateLambda([](const FAccelByteModelsAcceptFriendsResponse& Result)
{
if (Result.Code == "0")
{
// Do something if AcceptFriendsResponseDelegate has been successful
}
else
{
// Do something if AcceptFriendsResponseDelegate has an error
}
}));

FString UserId = FString("SomeUserId");
FRegistry::Lobby.AcceptFriend(UserId);

Reject Incoming Friend Requests

You can reject the incoming friend request by their User ID. To get the user ID, you need to retrieve a list of incoming friend requests, copy the User ID, and store it somewhere safe for use in the following function.

FRegistry::Lobby.Connect();
FRegistry::Lobby.SetRejectFriendsResponseDelegate(AccelByte::Api::Lobby::FRejectFriendsResponse::CreateLambda([](const FAccelByteModelsRejectFriendsResponse& Result)
{
if (Result.Code == "0")
{
// Do something if RejectFriendsResponseDelegate has been successful
}
else
{
// Do something if RejectFriendsResponseDelegate has an error
}
}));

FString UserId = FString("SomeTargetRejectFriendUserId");
FRegistry::Lobby.RejectFriend(UserId);

Retrieve a List of Outgoing Friend Requests

Use the following function to retrieve a list of outgoing friend requests.

FRegistry::Lobby.Connect();
FRegistry::Lobby.SetListOutgoingFriendsResponseDelegate(AccelByte::Api::Lobby::FListOutgoingFriendsResponse::CreateLambda([](const FAccelByteModelsListOutgoingFriendsResponse& Result)
{
if (Result.Code == "0")
{
// Do something if ListOutgoingFriendsResponseDelegate has an error
}
else
{
// Do something if ListOutgoingFriendsResponseDelegate has been successful
}
}));

FRegistry::Lobby.ListOutgoingFriends();

Cancel Outgoing Friend Requests

You can cancel outgoing friend requests using User ID. To get the user ID, you need to retrieve a list of outgoing friend requests, copy the User ID, and store it somewhere safe for the following function.

FRegistry::Lobby.Connect();
FRegistry::Lobby.SetCancelFriendsResponseDelegate(AccelByte::Api::Lobby::FCancelFriendsResponse::CreateLambda([](const FAccelByteModelsCancelFriendsResponse& Result)
{
if (Result.Code == "0")
{
// Do something if CancelFriendsResponseDelegate has been successful
}
else
{
// Do something if CancelFriendsResponseDelegate has an error
}
}));

FString UserId = FString("SomeTargetCancelFriendUserId");
FRegistry::Lobby.CancelFriendRequest(UserId);

Unfriend

Use the following function to unfriend another player.

FRegistry::Lobby.Connect(); 
FRegistry::Lobby.SetUnfriendResponseDelegate(AccelByte::Api::Lobby::FUnfriendResponse::CreateLambda([](const FAccelByteModelsUnfriendResponse& Result)
{
if (Result.Code == "0")
{
// Do something if UnfriendResponseDelegate has been successful
}
else
{
// Do something if UnfriendResponseDelegate has an error
}
}));

FString UserId = FString("SomeTargetFriendUserId");
FRegistry::Lobby.Unfriend(UserId);