Skip to main content

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

Last updated on October 24, 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 already an SDK wrapper created named ManagingFriendsWrapper.cs. This wrapper contains managing friends-related functionality. In this tutorial, you will use a starter version of that wrapper so you can implement the functionalities from scratch.

What's in the Starter Pack

To follow this tutorial, there is a prepared for you a starter wrapper class named ManagingFriendsWrapper_Starter. The starter 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 Lobby _lobby;
  • Start() already contains a predefined variable for Lobby called _lobby.

    void Start()
    {
    _lobby = MultiRegistry.GetApiClient().GetLobby();
    }

Implement player blocking

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

    public void BlockPlayer(string userId, ResultCallback<BlockPlayerResponse> resultCallback)
    {
    _lobby.BlockPlayer(userId, result =>
    {
    if (!result.IsError) {
    Debug.Log($"Success to block player");
    } else {
    Debug.LogWarning($"Error unfriend a friend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    resultCallback?.Invoke(result);
    } );
    }
  2. Create a new function called UnblockPlayer to unblock a player by user ID.

    public void UnblockPlayer(string userId, ResultCallback<UnblockPlayerResponse> resultCallback)
    {
    _lobby.UnblockPlayer(userId, result =>
    {
    if (!result.IsError) {
    Debug.Log($"Success to unblock a player");
    } else {
    Debug.LogWarning($"Error unblock a friend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    resultCallback?.Invoke(result);
    } );
    }
  3. Create a function to query a list from blocked players.

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

Implement unfriending

  1. Open ManagingFriendsWrapper_Starter.cs and declare the following functions:

    public void Unfriend(string userId, ResultCallback resultCallback)
    {
    _lobby.Unfriend(userId, result =>
    {
    if (!result.IsError) {
    Debug.Log($"Success to unfriend");
    } else {
    Debug.LogWarning($"Error unfriend a friend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    resultCallback?.Invoke(result);
    } );
    }

Resources