Skip to main content

Use the SDK for friend requests - Add friends - (Unity module)

Last updated on December 13, 2024

Unwrap the wrapper

In this tutorial, you will learn how to display friend request list, sent friend request list, accepting friend request, rejecting friend request, and canceling sent friend request using the AccelByte Gaming Services (AGS) Game SDK. In Byte Wars, there is already an SDK wrapper created named FriendsEssentialsWrapper.cs. You will use a starter version of that wrapper so you can implement friends functionalities from scratch.

What's in the Starter Pack

To start this tutorial, find the starter version of the friends essentials wrapper called FriendsEssentialsWrapper_Starter. This class contains several functionalities already implemented. You will add more functionalities to this class to complete the player search feature.

  • FriendsEssentialsWrapper_Starter file: /Assets/Resources/Modules/FriendsEssentials/Scripts/FriendsEssentialsWrapper_Starter.cs

The FriendsEssentialsWrapper_Starter class has several functionalities:

  • Delegates how incoming friend requests are handled, i.e., accepted or rejected.

    public static event Action<string> OnIncomingRequest = delegate { };
    public static event Action<string> OnRequestCanceled = delegate { };
    public static event Action<string> OnRequestRejected = delegate { };
    public static event Action<string> OnRequestAccepted = delegate { };
  • An ObservableList property to let other object knows when the player's friend list has changed and let them do something in response.

    public ObservableList<string> CachedFriendUserIds { get; private set; } = new ObservableList<string>();
  • The AGS Game SDK classes to get the details of players are User, Lobby, and UserProfiles. You can find these lines of code in FriendsEssentialsWrapper_Starter:

    private static ApiClient ApiClient => AccelByteSDK.GetClientRegistry().GetApi();
    private User user;
    private UserProfiles userProfiles;
    private Lobby lobby;

Implement Get Received Friend Requests

To implement the functionalities to retrieve incoming friend requests, open FriendsEssentialsWrapper_Starter.cs and create a function called LoadIncomingFriendRequests. This function will retrieve all incoming friend requests from the Lobby service and store them in the CachedFriendUserIds list.

public void LoadIncomingFriendRequests(ResultCallback<Friends> resultCallback)
{
lobby.ListIncomingFriends(result =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning("Error loading incoming friend requests, " +
$"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
BytewarsLogger.Log("Successfully loaded incoming friend requests");
CachedFriendUserIds.Add(result.Value.friendsId);
}

resultCallback?.Invoke(result);
});
}

Implement Get Sent Friend Requests

To implement the functionality to retrieve outgoing friend requests, open FriendsEssentialsWrapper_Starter.cs and create a new function called LoadOutgoingFriendRequests to query all outgoing friend requests.

public void LoadOutgoingFriendRequests(ResultCallback<Friends> resultCallback = null)
{
lobby.ListOutgoingFriends(result =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning("Error loading outgoing friend requests, " +
$" Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
BytewarsLogger.Log("Successfully loaded outgoing friend requests");
CachedFriendUserIds.Add(result.Value.friendsId);
}

resultCallback?.Invoke(result);
});
}

Implement getting friend details

To obtain user information, you need to call another SDK function by creating the GetBulkUserInfo function. Open FriendsEssentialsWrapper_Starter.cs and create the GetBulkUserInfo function to query multiple user information at once.

public void GetBulkUserInfo(string[] userIds, ResultCallback<ListBulkUserInfoResponse> resultCallback)
{
user.BulkGetUserInfo(userIds, result =>
{
if (result.IsError)
{
BytewarsLogger.LogWarning("Error getting bulk user info, " +
$"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
BytewarsLogger.Log($"Successfully retrieved bulk user info");
CachedFriendUserIds.Add(userIds);
}

resultCallback?.Invoke(result);
});
}

Implement Cancel, Reject, and Accept Friend Request

In this section, you will learn how to enable senders to cancel friend requests and receivers to reject or accept friend requests. Follow these steps:

  1. Create a function called CancelFriendRequests(). This function calls CancelFriendRequest from the AGS Game SDK Lobby, which cancels any friend requests from the local user to the target user.

    public void CancelFriendRequests(string userId, ResultCallback resultCallback)
    {
    lobby.CancelFriendRequest(userId, result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error canceling friend request, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Successfully canceled outgoing friend request with User Id: {userId}");
    CachedFriendUserIds.Remove(userId);
    }

    resultCallback?.Invoke(result);
    });
    }
  2. Create a function called DeclineFriend(). This function calls RejectFriend from the AGS Game SDK Lobby, which rejects any friend requests the target user has received.

    public void DeclineFriend(string userId, ResultCallback resultCallback)
    {
    lobby.RejectFriend(userId, result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error declining friend request, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Successfully rejected friend request with User Id: {userId}");
    CachedFriendUserIds.Remove(userId);
    }

    resultCallback?.Invoke(result);
    });
    }
  3. Open FriendsEssentialsWrapper_Starter.cs and create a function called AcceptFriend(). This function calls AcceptFriend from the AGS Game SDK Lobby, which accepts friend requests the target user has received.

    public void AcceptFriend(string userId, ResultCallback resultCallback)
    {
    lobby.AcceptFriend(userId, result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error accepting friend request, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Successfully accepted friend request with User Id: {userId}");
    CachedFriendUserIds.Add(userId);
    }

    resultCallback?.Invoke(result);
    });
    }

Listen for friend request events

When players accept, reject, or cancel friend requests, it is important to update the incoming or outgoing friend request lists immediately. To do this, use the notification events provided by the AGS Game SDK.

In this section, you will learn how to listen for the OnIncomingFriendRequest, FriendRequestRejected, and FriendRequestAccepted event notifications. These events are available through the AGS Game SDK and can be used to handle various friend request interactions.

To listen for friend request events, follow these steps:

  1. Open FriendsEssentialsWrapper_Starter.cs and create a function called OnIncomingFriendRequest to listen for incoming friend requests. This function invokes the OnIncomingRequest event, which will be used later in the UI script handler.

    private void OnIncomingFriendRequest(Result<Friend> result)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error receiving incoming friend request notification, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    return;
    }

    BytewarsLogger.Log($"Incoming friend request from {result.Value.friendId}");
    CachedFriendUserIds.Add(result.Value.friendId);

    OnIncomingRequest?.Invoke(result.Value.friendId);
    }
  2. Create a function called OnFriendRequestCanceled to listen for canceled friend requests. This function invokes the OnRequestCanceled event action, which will be used later in the UI script handler.

    private void OnFriendRequestCanceled(Result<Acquaintance> result)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error receiving friend request canceled notification, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    return;
    }

    BytewarsLogger.Log($"Friend request from {result.Value.userId} has been canceled");
    CachedFriendUserIds.Remove(result.Value.userId);

    OnRequestCanceled?.Invoke(result.Value.userId);
    }
  3. Create a function called OnFriendRequestRejected to listen for rejected friend requests. This function will invoke the OnRequestRejected event action, which will be used later in the UI script handler.

    private void OnFriendRequestRejected(Result<Acquaintance> result)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error receiving friend request rejected notification, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    return;
    }

    BytewarsLogger.Log($"Friend request from {result.Value.userId} has been rejected");
    CachedFriendUserIds.Remove(result.Value.userId);

    OnRequestRejected?.Invoke(result.Value.userId);
    }
  4. Create a function called OnFriendRequestAccepted to listen for accepted friend requests. This function will invoke the OnRequestAccepted event action, which will be used later in the UI script handler.

    private void OnFriendRequestAccepted(Result<Friend> result)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error receiving friend request accepted notification, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    return;
    }

    BytewarsLogger.Log($"Friend request from {result.Value.friendId} has been accepted");
    CachedFriendUserIds.Add(result.Value.friendId);

    OnRequestAccepted?.Invoke(result.Value.friendId);
    }
  5. Now that you have created the functions to listen for friend request events, you need to register these functions with the AGS Game SDK. To do this, add the highlighted code below to the Awake() function in the FriendsEssentialsWrapper_Starter.cs script.

    private void Awake()
    {
    //...
    SinglePlatformAuthWrapper_Starter.OnUserProfileReceived += SetPlayerInfo;

    lobby.OnIncomingFriendRequest += OnIncomingFriendRequest;
    lobby.FriendRequestCanceled += OnFriendRequestCanceled;
    lobby.FriendRequestRejected += OnFriendRequestRejected;
    lobby.FriendRequestAccepted += OnFriendRequestAccepted;
    }
  6. After registering the functions to listen for friend request events, you need to unregister these functions when the script is destroyed. To do this, add the highlighted code below to the OnDestroy() function in the FriendsEssentialsWrapper_Starter.cs script.

    private void OnDestroy()
    {
    AuthEssentialsWrapper.OnUserProfileReceived -= SetPlayerInfo;
    SinglePlatformAuthWrapper.OnUserProfileReceived -= SetPlayerInfo;
    AuthEssentialsWrapper_Starter.OnUserProfileReceived -= SetPlayerInfo;
    SinglePlatformAuthWrapper_Starter.OnUserProfileReceived -= SetPlayerInfo;

    lobby.OnIncomingFriendRequest -= OnIncomingFriendRequest;
    lobby.FriendRequestCanceled -= OnFriendRequestCanceled;
    lobby.FriendRequestRejected -= OnFriendRequestRejected;
    lobby.FriendRequestAccepted -= OnFriendRequestAccepted;
    }

Resources