Skip to main content

Use the SDK to find players - Search for players - (Unity module)

Last updated on March 9, 2024

Unwrap the wrapper

In this tutorial, you will learn how to find players by their display name using the AccelByte Gaming Services (AGS) Game SDK. In Byte Wars, there is already an SDK wrapper created named FriendsEssentialsWrapper.cs. This wrapper contains friends-related functionality, including finding potential friends. You will use a starter version of that wrapper so you can implement friends functionalities from scratch.

What's in the Starter Pack

The FriendsEssentialsWrapper_Starter class has been provided for you as the starter code. This file is available in the Resources section.

  • FriendsEssentialsWrapper_Starter file: /Assets/Resources/Modules/FriendEssentials/Scripts/FriendEssentialsWrapper_Starter.cs

The FriendsEssentialsWrapper_Starter class has several functionalities already:

  • The AGS Game SDK classes to get user details and user friends are User and Lobby, respectively. You can find these lines of code in the FriendsEssentialsWrapper_Starter:

    private User _user;
    private Lobby _lobby;
  • A property has been created to get the player User ID. It will be used to remove the user itself from the search results.

    public string PlayerUserId { get; private set; }
  • Start() gets the User and Lobby classes and stores them to the variables _user and _lobby, respectively.

    void Start()
    {
    _user = MultiRegistry.GetApiClient().GetUser();
    _lobby = MultiRegistry.GetApiClient().GetLobby();
    LoginHandler.onLoginCompleted += tokenData =>
    {
    PlayerUserId = tokenData.user_id;
    };
    }
  • A function for logging into the lobby has been declared.

    private void LoginToLobby()
    {
    if (!_lobby.IsConnected)
    {
    _lobby.Connect();
    }
    }
  • Later, you need to listen to the event delegate from LoginHandler and attach LoginToLobby() to ensure that you have successfully logged into the lobby.

    void Start()
    {
    _user = MultiRegistry.GetApiClient().GetUser();
    _lobby = MultiRegistry.GetApiClient().GetLobby();
    LoginHandler.onLoginCompleted += tokenData =>
    {
    PlayerUserId = tokenData.user_id;
    };
    LoginHandler.onLoginCompleted += tokenData => LoginToLobby();
    }

In this section, you will implement functionality to find players by their display name.

  1. Create a function to get the player list by display name first. Open FriendsEssentialsWrapper_Starter.cs and create a new function called GetUserByDisplayName.

    public void GetUserByDisplayName(string displayName, ResultCallback<PagedPublicUsersInfo> resultCallback)
    {
    var searcRules = SearchType.DISPLAYNAME;
    _user.SearchUsers(displayName, searcRules, result =>
    {
    if (!result.IsError) {
    Debug.Log($"Success to search users with displayname {displayName}");
    resultCallback?.Invoke(result);
    }
    else
    {
    Debug.LogWarning($"Error SearchUsers, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
    }
    });
    }
  2. Create a new function called GetFriendshipStatus to get friendship status.

    public void GetFriendshipStatus(string userId, ResultCallback<FriendshipStatus> resultCallback)
    {
    _lobby.GetFriendshipStatus(userId, result =>
    {
    if (!result.IsError)
    {
    Debug.Log($"success to get friendship status for {userId}");
    }
    else
    {
    Debug.LogWarning($"{result.Error.Message}");
    }

    resultCallback.Invoke(result);
    });
    }
  3. Create a function to retrieve the avatar from a player.

    public void GetUserAvatar(string userId, ResultCallback<Texture2D> resultCallback)
    {
    _user.GetUserAvatar(userId, result =>
    {
    if (!result.IsError)
    {
    Debug.Log($"Success to retrieve Avatar for User {userId}");
    }
    else
    {
    Debug.LogWarning($"Unable to retrieve Avatar for User {userId} : {result.Error}");
    }
    resultCallback?.Invoke(result);
    });
    }

Implement send friend request

In this section, you will implement functionality to send a friend invitation request.

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

    public void SendFriendRequest(string userId, ResultCallback resultCallback)
    {
    _lobby.RequestFriend(userId, result =>
    {
    if (!result.IsError)
    {
    Debug.Log("Success to send a friends request");
    resultCallback?.Invoke(result);
    }
    else
    {
    Debug.LogWarning($"Failed to send a friends request: error code: {result.Error.Code} message: {result.Error.Message}");
    }
    });
    }

Resources