Use the SDK to manage friends - Manage friends - (Unity module)
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 forlobby
, which is assigned to theApiClient.GetLobby()
function.void Awake()
{
lobby = ApiClient.GetLobby();
}A delegate events called
OnPlayerBlocked
andOnPlayerUnfriended
.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.
Open
ManagingFriendsWrapper_Starter.cs
and create a variable definition for theFriendEssentialWrapper_Starter
, calledfriendsEssentialsWrapper
. You will need this reference to update the cached user IDs so that other script can function properly.private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;
Update the
Start()
function to initialize thefriendsEssentialsWrapper
. Replace the existing implementation with the code below.private void Start()
{
friendsEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendsEssentialsWrapper_Starter>();
}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.
Open
ManagingFriendsWrapper_Starter.cs
and create a new function calledBlockPlayer()
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.
Open
ManagingFriendsWrapper_Starter.cs
and create a new function calledUnblockPlayer()
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.
Open
ManagingFriendsWrapper_Starter.cs
and create a new function calledUnfriend()
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.
Open the
ManagingFriendsWrapper_Starter.cs
file and add the function calledOnPlayerBlockedNotif()
to listen to theOnPlayerBlocked
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);
}Create a function called
OnPlayerUnfriendNotif()
to listen to theOnUnfriend
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);
}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;
}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
- The file used in this tutorial is available in the Unity Byte Wars GitHub repository.