フレンドリクエストに SDK を使用する - フレンドを追加する - (Unity モジュール)
What's in the Starter Pack
This tutorial shows you how to implement the handling friend requests using the AccelByte Gaming Services (AGS) Game SDK.
Already defined are event actions that will be used later by the user interface (UI) script handler:
public static event Action OnRejected;
public static event Action OnIncomingAdded;
public static event Action OnAccepted;
Implement getting received friend requests
Open
FriendsEssentialsWrapper_Starter.cs
and create a function calledLoadIncomingFriendRequests
. This function will retrieve all incoming friend requests.public void LoadIncomingFriendRequests(ResultCallback<Friends> resultCallback)
{
_lobby.ListIncomingFriends(result =>
{
if (!result.IsError)
{
Debug.Log($"Succes to load incoming friend request");
}
else
{
Debug.LogWarning($"Error ListIncomingFriends, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
resultCallback?.Invoke(result);
});
}To reject a friend request, you need to create a function called
DeclineFriend
that will be used later from the UI script handler.public void DeclineFriend(string userID, ResultCallback resultCallback)
{
_lobby.RejectFriend(userID, result =>
{
if (!result.IsError)
{
Debug.Log($"success to reject friend request {!result.IsError}");
}
else
{
Debug.LogWarning($"{result.Error.Message}");
}
resultCallback?.Invoke(result);
});
}Create a function to accept incoming friend requests called
AcceptFriend
.public void AcceptFriend(string userID, ResultCallback resultCallback)
{
_lobby.AcceptFriend(userID, result =>
{
if (!result.IsError)
{
Debug.Log($"Success to accept friend {userID}");
}
else
{
Debug.LogWarning($"{result.Error.Message}");
}
resultCallback?.Invoke(result);
});
}
Implement getting sent friend requests
Open
FriendsEssentialsWrapper_Starter.cs
and create a new function calledLoadOutgoingFriendRequests
to query all outgoing friend requests.public void LoadOutgoingFriendRequests(ResultCallback<Friends> resultCallback = null)
{
_lobby.ListOutgoingFriends(result => {
if (!result.IsError)
{
Debug.Log($"Success to load outgoing friend request");
}
else
{
Debug.LogWarning($"Error ListOutgoingFriends, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
resultCallback?.Invoke(result);
});
}Define a function to cancel outgoing friend requests.
public void CancelFriendRequests(string userID, ResultCallback resultCallback)
{
_lobby.CancelFriendRequest(userID, result =>
{
if (!result.IsError)
{
Debug.Log($"Success to accept friend {userID}");
}
else
{
Debug.LogWarning($"{result.Error.Message}");
}
resultCallback?.Invoke(result);
});
}
Implement getting friend details
Already defined are the LoadIncomingFriendRequests
and LoadOutgoingFriendRequests
functions that query the friend request list. The return value of these functions is a list of friend user IDs. However, to obtain user information, you need to call another SDK function by creating a function with the following code:
public void GetBulkUserInfo(string[] usersId, ResultCallback<ListBulkUserInfoResponse> resultCallback)
{
_user.BulkGetUserInfo(usersId, result =>
{
if (!result.IsError)
{
Debug.Log($"success to retrieve bulk user info {!result.IsError}");
resultCallback?.Invoke(result);
}
else
{
Debug.LogWarning($"{result.Error.Message}");
}
});
}
Listen for friend list updating
When players accept, reject, or cancel friend requests, you need to update the incoming or outgoing friend request lists. To achieve this, you will use the notification events provided by the AGS Game SDK. In this section, you will listen for the OnIncomingFriendRequest
, FriendRequestRejected
, and FriendRequestAccepted
event notifications. These events are made available by the AGS Game SDK for handling friend request interactions.
Open
FriendsEssentialsWrapper_Starter.cs
and create a function to listen for new incoming friend requests. This function will invoke theOnIncomingAdded
event action, which will be used later in the user interface (UI) script handler.public void ListenIncomingFriendRequest()
{
_lobby.OnIncomingFriendRequest += result =>
{
if (!result.IsError)
{
Debug.Log($"Successfully listened for incoming friend request");
OnIncomingAdded?.Invoke();
}
else
{
Debug.LogWarning($"{result.Error.Message}");
}
};
}Create a function called
ListenRejectedRequest()
that will be triggered whenever one of your sent friend requests is rejected by another player. This function will invoke theOnRejected
event action, which will be used later in the UI script handler.public void ListenRejectedRequest()
{
_lobby.FriendRequestRejected += result =>
{
if (!result.IsError)
{
Debug.Log($"Successfully listened for outgoing friend request rejection");
OnRejected?.Invoke();
}
else
{
Debug.LogWarning($"Error OnUnfriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
};
}Create a function to listen when the outgoing friend request is accepted. This function will invoke the
OnRejected
event action, which will be used later in the UI script handler.public void ListenAcceptedRequest()
{
_lobby.FriendRequestAccepted += result =>
{
if (!result.IsError)
{
Debug.Log($"Successfully listened for outgoing friend request acceptance");
OnAccepted?.Invoke();
}
else
{
Debug.LogWarning($"Error OnUnfriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
};
}You now have all the necessary functionalities to update the friend request list. To listen to these events, add the following highlighted code to the
Start()
function:void Start()
{
_user = MultiRegistry.GetApiClient().GetUser();
_lobby = MultiRegistry.GetApiClient().GetLobby();
LoginHandler.onLoginCompleted += tokenData =>
{
Debug.LogWarning(tokenData.user_id);
PlayerUserId = tokenData.user_id;
};
LoginHandler.onLoginCompleted += tokenData => LoginToLobby();
LoginHandler.onLoginCompleted += tokenData => ListenIncomingFriendRequest();
LoginHandler.onLoginCompleted += tokenData => ListenRejectedRequest();
LoginHandler.onLoginCompleted += tokenData => ListenAcceptedRequest();
}
Resources
- GitHub link to the file in the Unity Byte Wars repository: