Skip to main content

Put it all together - Display friend list - (Unity module)

Last updated on December 20, 2024

Connect the UI to get friend list

  1. Open FriendsMenuHandler_Starter.cs and declare the FriendsEssentialsWrapper_Starter class as a private variable.

    private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;
  2. Replace the existing LoadFriendList function with the code below to call the GetFriendList function from the FriendsEssentialsWrapper_Starter class. This will trigger the actual friend list retrieval from the backend.

    private void LoadFriendList()
    {
    CurrentView = FriendsView.Loading;
    ClearFriendList();

    friendsEssentialsWrapper.GetFriendList(OnLoadFriendListCompleted);
    }
  3. Replace the existing OnLoadFriendListCompleted callback function with the code below to handle the result.

    private void OnLoadFriendListCompleted(Result<Friends> result)
    {
    if (result.IsError)
    {
    CurrentView = FriendsView.LoadFailed;
    return;
    }

    if (result.Value.friendsId.Length <= 0)
    {
    CurrentView = FriendsView.Default;
    return;
    }

    GetBulkUserInfo(result.Value);
    }
  4. Create a function called GetBulkUserInfo() to get the user information from the list of friend IDs.

    private void GetBulkUserInfo(Friends friends)
    {
    friendsEssentialsWrapper.GetBulkUserInfo(friends.friendsId, OnGetBulkUserInfoCompleted);
    }
  5. Create a callback function called OnGetBulkUserInfoCompleted() to handle the result.

    private void OnGetBulkUserInfoCompleted(Result<ListBulkUserInfoResponse> result)
    {
    if (result.IsError)
    {
    return;
    }

    ClearFriendList();
    CurrentView = FriendsView.LoadSuccess;

    PopulateFriendList(result.Value.data);
    }
  6. Create a function called PopulateFriendRequestList() to populate the friend list.

    private void PopulateFriendList(params BaseUserInfo[] userInfo)
    {
    foreach (BaseUserInfo baseUserInfo in userInfo)
    {
    CreateFriendEntry(baseUserInfo.userId, baseUserInfo.displayName);
    }
    }
  7. Create a function called CreateFriendEntry() to create a friend entry for each friend. If the friend's display name is empty, the function will use the first five characters of the user ID as the display name.

    private void CreateFriendEntry(string userId, string displayName)
    {
    GameObject playerEntry = InstantiateToColumn(friendEntryPrefab);
    playerEntry.name = userId;

    if (string.IsNullOrEmpty(displayName))
    {
    string truncatedUserId = userId[..5];
    displayName = $"Player-{truncatedUserId}";
    }

    FriendsEntryComponentHandler playerEntryHandler = playerEntry.GetComponent<FriendsEntryComponentHandler>();
    playerEntryHandler.UserId = userId;
    playerEntryHandler.FriendName.text = displayName;

    friendEntries.Add(userId, playerEntry);
    }
  8. Replace the existing OnEnable() function with the code below to initialize the friendsEssentialsWrapper variable and call the LoadFriendList() function.

    private void OnEnable()
    {
    if (friendsEssentialsWrapper == null)
    {
    friendsEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendsEssentialsWrapper_Starter>();
    }

    if (friendsEssentialsWrapper == null)
    {
    BytewarsLogger.LogWarning("FriendsEssentialsWrapper is not enabled");
    return;
    }

    if (CurrentView == FriendsView.Loading)
    {
    BytewarsLogger.Log("Already loading friend list");
    return;
    }

    LoadFriendList();
    }

Connect the UI to display avatars of friends

  1. Open FriendsMenuHandler_Starter.cs and create a function called RetrieveUserAvatar() to get the user avatar.

    private void RetrieveUserAvatar(string userId)
    {
    friendsEssentialsWrapper.GetUserAvatar(userId, result => OnGetAvatarCompleted(result, userId));
    }
  2. Create a callback function called OnGetAvatarCompleted() to handle the result.

    private void OnGetAvatarCompleted(Result<Texture2D> result, string userId)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning($"Unable to get avatar for user Id: {userId}, " +
    $"Error Code: {result.Error.Code}, " +
    $"Error Message: {result.Error.Message}");
    return;
    }

    if (result.Value == null || !friendEntries.TryGetValue(userId, out GameObject friendEntry))
    {
    return;
    }

    Image friendImage = friendEntry.GetComponent<FriendsEntryComponentHandler>().FriendImage;
    Rect imageRect = new Rect(0f, 0f, result.Value.width, result.Value.height);
    friendImage.sprite = Sprite.Create(result.Value, imageRect, Vector2.zero);
    }
  3. Update the CreateFriendEntry() function to call the RetrieveUserAvatar() function. Add the code below:

    private void CreateFriendEntry(string userId, string displayName)
    {
    //...
    friendEntries.Add(userId, playerEntry);

    RetrieveUserAvatar(userId);
    }

Connect the entry to the friend details menu

  1. Open FriendsMenuHandler_Starter.cs and create a function called OnFriendEntryClicked() to handle the friend entry click event. This function will instantiate the friend details menu canvas, pass the user information, and change the menu to the friend details menu.

    private void OnFriendEntryClicked(string userId, string displayName,
    FriendsEntryComponentHandler playerEntryHandler)
    {
    AssetEnum friendDetailCanvas = friendDetailMenuCanvas;

    if (friendDetailCanvas is AssetEnum.FriendDetailsMenuCanvas)
    {
    MenuManager.Instance.InstantiateCanvas(friendDetailCanvas);
    }

    if (!MenuManager.Instance.AllMenu.TryGetValue(friendDetailCanvas, out MenuCanvas menuCanvas))
    {
    BytewarsLogger.LogWarning($"Unable to find {friendDetailCanvas} in menu manager");
    return;
    }

    if (menuCanvas.gameObject.TryGetComponent(out FriendDetailsMenuHandler friendDetailMenu))
    {
    friendDetailMenu.UserId = userId;
    friendDetailMenu.FriendImage.sprite = playerEntryHandler.FriendImage.sprite;
    friendDetailMenu.FriendDisplayName.text = displayName;
    }

    MenuManager.Instance.ChangeToMenu(friendDetailCanvas);
    }
  2. Update the CreateFriendEntry() function to add an event listener to the friend entry to call the OnFriendEntryClicked() function. Add the code below.

    private void CreateFriendEntry(string userId, string displayName)
    {
    //...
    playerEntryHandler.FriendName.text = displayName;

    Button friendButton = playerEntry.GetComponent<Button>();
    friendButton.onClick.AddListener(() => OnFriendEntryClicked(userId, displayName, playerEntryHandler));

    friendEntries.Add(userId, playerEntry);
    //...
    }

Connect the UI to Friends Action Events

In this section, you will use the static action events that you have used in the Add friends and Manage friends modules. If you have not completed or do not intend to complete those modules, you will only forfeit the real-time friend list update feature.

  1. Open FriendsMenuHandler_Starter.cs and create a function called OnFriendListUpdate(). It will trigger the LoadFriendList function, updating the current displayed list.

    private void OnFriendListUpdate(string userId)
    {
    if (!gameObject.activeSelf)
    {
    return;
    }

    LoadFriendList();
    }
  2. Update the Awake function to bind the action events in the wrapper to the OnFriendListUpdate function. Add the code below:

    private void Awake()
    {
    //...
    backButton.onClick.AddListener(MenuManager.Instance.OnBackPressed);

    // Bind to both starter and non starter to allow mixed usage of starter and non
    FriendsEssentialsWrapper.OnRequestAccepted += OnFriendListUpdate;
    ManagingFriendsWrapper.OnPlayerUnfriended += OnFriendListUpdate;
    ManagingFriendsWrapper.OnPlayerBlocked += OnFriendListUpdate;
    FriendsEssentialsWrapper_Starter.OnRequestAccepted += OnFriendListUpdate;
    ManagingFriendsWrapper_Starter.OnPlayerUnfriended += OnFriendListUpdate;
    ManagingFriendsWrapper_Starter.OnPlayerBlocked += OnFriendListUpdate;
    //...
    }
  3. Replace the OnDestroy function with the code below to unbind those events when the UI is destroyed.

    private void OnDestroy()
    {
    FriendsEssentialsWrapper.OnRequestAccepted -= OnFriendListUpdate;
    ManagingFriendsWrapper.OnPlayerUnfriended -= OnFriendListUpdate;
    ManagingFriendsWrapper.OnPlayerBlocked -= OnFriendListUpdate;
    FriendsEssentialsWrapper_Starter.OnRequestAccepted -= OnFriendListUpdate;
    ManagingFriendsWrapper_Starter.OnPlayerUnfriended -= OnFriendListUpdate;
    ManagingFriendsWrapper_Starter.OnPlayerBlocked -= OnFriendListUpdate;
    }

Resources