Skip to main content

Use the SDK to manage friends - Manage friends - (Unity module)

Last updated on December 13, 2024

Unwrap the Wrapper

In this tutorial, you will learn how to manage friends using the AccelByte Gaming Services (AGS) Game SDK. In the Byte Wars project, there is an SDK wrapper called ManagingFriendsWrapper.cs. This wrapper contains functionalities related to friends management. In this tutorial, you will use the starter version of that wrapper so you can implement the functionalities to your liking.

What's in the Starter Pack

To follow this tutorial, find the starter version of the friends management wrapper class called ManagingFriendsWrapper_Starter.

  • FriendsEssentialsWrapper_Starter file: /Assets/Resources/Modules/ManagingFriends/Scripts/ManagingFriendsWrapper_Starter.cs

This class has several functionalities already implemented:

  • The AGS Game SDK class to get the user's friends is Lobby. You can find this line of code in ManagingFriendsWrapper_Starter.cs.

    private static ApiClient ApiClient => AccelByteSDK.GetClientRegistry().GetApi();
    private static Lobby lobby;
  • The Awake() function already contains a predefined variable for lobby, which is assigned to the ApiClient.GetLobby() function.

    void Awake()
    {
    lobby = ApiClient.GetLobby();
    }
  • A delegate events called OnPlayerBlocked and OnPlayerUnfriended.

    public static event Action<string> OnPlayerBlocked = delegate { };
    public static event Action<string> OnPlayerUnfriended = delegate { };

Implement Get Blocked Players

In this section, you will implement the functionality to get a list of blocked players. This function will return a list of blocked players by the user.

  1. Open ManagingFriendsWrapper_Starter.cs and create a variable definition for the FriendEssentialWrapper_Starter, called friendsEssentialsWrapper. You will need this reference to update the cached user IDs so that other script can function properly.

    private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;
  2. Update the Start() function to initialize the friendsEssentialsWrapper. Replace the existing implementation with the code below.

    private void Start()
    {
    friendsEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendsEssentialsWrapper_Starter>();
    }
  3. Create a new function called GetBlockedPlayers() to get the list of blocked players.

    public void GetBlockedPlayers(ResultCallback<BlockedList> resultCallback)
    {
    lobby.GetListOfBlockedUser(result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error to load blocked users, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Success to load blocked users, total blocked users {result.Value.data.Length}");

    IEnumerable<string> blockedUserIds = result.Value.data.Select(user => user.blockedUserId);
    friendsEssentialsWrapper.CachedFriendUserIds.Add(blockedUserIds.ToArray());
    }

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

Implement Block Player

In this section, you will implement the player blocking functionality. This function will block a player by their user ID. This function needs a reference to FriendsEssentialsWrapper_Starter. Refer to step 1 - 2 of Implement Get Blocked Players if you haven't done so.

  1. Open ManagingFriendsWrapper_Starter.cs and create a new function called BlockPlayer() to block a player by their user ID.

    public void BlockPlayer(string userId, ResultCallback<BlockPlayerResponse> resultCallback)
    {
    lobby.BlockPlayer(userId, result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error sending block player request, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Successfully blocked player with user Id: {userId}");
    }

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

Implement Unblock Player

In this section, you will implement the unblocking functionality. This function will unblock a player by their user ID. This function needs a reference to FriendsEssentialsWrapper_Starter. Refer to step 1 - 2 of Implement Get Blocked Players if you haven't done so.

  1. Open ManagingFriendsWrapper_Starter.cs and create a new function called UnblockPlayer() to unblock a player by their user ID.

    public void UnblockPlayer(string userId, ResultCallback<UnblockPlayerResponse> resultCallback)
    {
    lobby.UnblockPlayer(userId, result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error unblock a friend, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Successfully unblocked player with user Id: {userId}");

    friendsEssentialsWrapper.CachedFriendUserIds.Remove(userId);
    }

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

Implement Unfriend

In this section, you will implement the unfriend functionality. This function will remove a friend from the player's friend list. This function needs a reference to FriendsEssentialsWrapper_Starter. Refer to step 1 - 2 of Implement Get Blocked Players if you haven't done so.

  1. Open ManagingFriendsWrapper_Starter.cs and create a new function called Unfriend() to unfriend a player by their user ID.

    public void Unfriend(string userId, ResultCallback resultCallback)
    {
    lobby.Unfriend(userId, result =>
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error sending unfriend request, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    else
    {
    BytewarsLogger.Log($"Successfully unfriended player with user Id: {userId}");

    friendsEssentialsWrapper.CachedFriendUserIds.Remove(userId);
    }

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

Listen On Blocked Player and On Unfriend

When a player is blocked or unblocked, the OnPlayerBlocked event is triggered, and when a player is unfriended, the OnUnfriend event is triggered on the other player. You can listen to these events in your game to update the UI or data.

  1. Open the ManagingFriendsWrapper_Starter.cs file and add the function called OnPlayerBlockedNotif() to listen to the OnPlayerBlocked event.

    private static void OnPlayerBlockedNotif(Result<PlayerBlockedNotif> result)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning("Error retrieving player blocked notif, " +
    $"Error Code: {result.Error.Code} Error Message: {result.Error.Message}");

    return;
    }

    BytewarsLogger.Log($"Player with user Id: {result.Value.userId} has been blocked");

    OnPlayerBlocked?.Invoke(result.Value.userId);
    }
  2. Create a function called OnPlayerUnfriendNotif() to listen to the OnUnfriend event.

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

    BytewarsLogger.Log($"Unfriend from {result.Value.friendId}");

    OnPlayerUnfriended?.Invoke(result.Value.friendId);
    }
  3. Now, you need to bind those functions to the actual event on the AGS Game SDK. Update the Awake() function with the code below.

    private void Awake()
    {
    lobby = ApiClient.GetLobby();

    lobby.PlayerBlockedNotif += OnPlayerBlockedNotif;
    lobby.OnUnfriend += OnPlayerUnfriendNotif;
    }
  4. Update the OnDestroy() to unbind those functions to make sure a clean destroy. Replace the existing implementation with the code below.

    private void OnDestroy()
    {
    lobby.PlayerBlockedNotif -= OnPlayerBlockedNotif;
    lobby.OnUnfriend -= OnPlayerUnfriendNotif;
    }

Resources