フレンドサービスを Client SDK に統合する
Introduction
The Friends service can be used to connect players with each other so they can perform social activities in the game such as chatting, seeing presence statuses and inviting each other to join a party. To manage friends, you will use the Lobby interface which uses WebSocket to ensure real-time updates.
Integration with this service will allow you to manage friend requests, retrieve a player's list of friends, and synchronize a player's friends from third-party platforms automatically so that they don't have to send a new friend request. The game client will need to register to listen for friend notifications with the Lobby service in order to know when another player is requesting an interaction.
Implement friend interactions
Search for players
You can search for a player's account information using their Display Name or Username as the query. You can configure the type of search by setting the enum of SearchType for Unity and EAccelByteSearchType for Unreal to DisplayName or Username.
- Unreal Engine
- Unity
FString Query = FString("UsersDisplayName");
EAccelByteSearchType By = EAccelByteSearchType::DISPLAYNAME;
FRegistry::User.SearchUsers(Query, By, THandler < FPagedPublicUsersInfo > ::CreateLambda([](const FPagedPublicUsersInfo & Result) {
// Do something if SearchUsers succeeds
}), FErrorHandler::CreateLambda([](int32 ErrorCode,
const FString & ErrorMessage) {
// Do something if SearchUsers fails
UE_LOG(LogTemp, Log, TEXT("Error SearchUsers, Error Code: %d Error Message: %s"), ErrorCode, * ErrorMessage);
}));
string query = "UsersDisplayName";
SearchType by = SearchType.DISPLAYNAME;
AccelByteSDK.GetClientRegistry().GetApi().GetUser().SearchUsers(query, by, result => {
if (result.IsError) {
// Do something if SearchUsers fails
Debug.Log($"Error SearchUsers, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if SearchUsers succeeds
}
});
Retrieve the Friends List
Use the following function to retrieve a friends list.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetLoadFriendListResponseDelegate(AccelByte::Api::Lobby::FLoadFriendListResponse::CreateLambda([](const FAccelByteModelsLoadFriendListResponse & Result) {
// Do something if LoadFriendListResponseDelegate succeeds
}), FErrorHandler::CreateLambda([](int32 ErrorCode,
const FString & ErrorMessage) {
// Do something if LoadFriendListResponseDelegate fails
UE_LOG(LogTemp, Log, TEXT("Error LoadFriendListResponseDelegate, Error Code: %d Error Message: %s"), ErrorCode, * ErrorMessage);
}));
FRegistry::Lobby.LoadFriendsList();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().LoadFriendsList(result => {
if (result.IsError) {
// Do something if LoadFriendsList fails
Debug.Log($"Error LoadFriendsList, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if LoadFriendsList succeeds
}
})
Send a Friend request using a user ID
The first step in making a friend is sending a friend request to another player. Use this code to send a friend request using a User ID.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetRequestFriendsResponseDelegate(AccelByte::Api::Lobby::FRequestFriendsResponse::CreateLambda([](const FAccelByteModelsRequestFriendsResponse & Result) {
// Do something if RequestFriendsResponseDelegate succeeds
}), FErrorHandler::CreateLambda([](int32 ErrorCode,
const FString & ErrorMessage) {
// Do something if RequestFriendsResponseDelegate fails
UE_LOG(LogTemp, Log, TEXT("Error RequestFriendsResponseDelegate, Error Code: %d Error Message: %s"), ErrorCode, * ErrorMessage);
}));
FString UserId = FString("SomeUserId");
FRegistry::Lobby.RequestFriend(UserId);
string userId = "SomeTargetFriendUserId";
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().RequestFriend(userId, result => {
if (result.IsError) {
// Do something if RequestFriend fails
Debug.Log($"Error RequestFriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if RequestFriend succeeds
}
});
Retrieve the list of incoming Friend requests
Use this function to retrieve all the information about incoming friend requests. This function retrieves user ID which you can use to accept or reject each request.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetListIncomingFriendsResponseDelegate(AccelByte::Api::Lobby::FListIncomingFriendsResponse::CreateLambda([](const FAccelByteModelsListIncomingFriendsResponse & Result) {
if (Result.Code == "0") {
// Do something if ListIncomingFriendsResponseDelegate succeeds
} else {
// Do something if ListIncomingFriendsResponseDelegate fails
}
}));
FRegistry::Lobby.ListIncomingFriends();
FRegistry::Lobby.LoadFriendsList();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().ListIncomingFriends(result => {
if (result.IsError) {
// Do something if ListIncomingFriends fails
Debug.Log($"Error ListIncomingFriends, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if ListIncomingFriends succeeds
}
});
Accept incoming friend requests
After a friend request has been sent, the player who received the request can either accept or reject it. To accept a friend request, use this function:
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetAcceptFriendsResponseDelegate(AccelByte::Api::Lobby::FAcceptFriendsResponse::CreateLambda([](const FAccelByteModelsAcceptFriendsResponse & Result) {
if (Result.Code == "0") {
// Do something if AcceptFriendsResponseDelegate succeeds
} else {
// Do something if AcceptFriendsResponseDelegate fails
}
}));
FString UserId = FString("SomeUserId");
FRegistry::Lobby.AcceptFriend(UserId);
string userId = "SomeTargetedAcceptedFriendUserId";
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().AcceptFriend(userId, result => {
if (result.IsError) {
// Do something if AcceptFriend fails
Debug.Log($"Error AcceptFriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if AcceptFriend succeeds
}
});
Reject incoming Friend requests
You can reject the incoming friend request by their User ID. To get the user ID, you need to retrieve a list of incoming friend requests, copy the User ID, and store it somewhere safe for use in the following function.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetRejectFriendsResponseDelegate(AccelByte::Api::Lobby::FRejectFriendsResponse::CreateLambda([](const FAccelByteModelsRejectFriendsResponse & Result) {
if (Result.Code == "0") {
// Do something if RejectFriendsResponseDelegate succeeds
} else {
// Do something if RejectFriendsResponseDelegate fails
}
}));
FString UserId = FString("SomeTargetRejectFriendUserId");
FRegistry::Lobby.RejectFriend(UserId);
string userId = "SomeTargetRejectedFriendUserId";
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().RejectFriend(userId, result => {
if (result.IsError) {
// Do something if RejectFriend fails
Debug.Log($"Error RejectFriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if RejectFriend succeeds
}
});
Unfriend players
Ro unfriend another player, use this function:
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetUnfriendResponseDelegate(AccelByte::Api::Lobby::FUnfriendResponse::CreateLambda([](const FAccelByteModelsUnfriendResponse & Result) {
if (Result.Code == "0") {
// Do something if UnfriendResponseDelegate succeeds
} else {
// Do something if UnfriendResponseDelegate fails
}
}));
FString UserId = FString("SomeTargetFriendUserId");
FRegistry::Lobby.Unfriend(UserId);
string userId = "SomeTargetFriendUserId";
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Unfriend(userId, result => {
if (result.IsError) {
// Do something if Unfriend fails
Debug.Log($"Error Unfriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if Unfriend succeeds
}
});
Retrieve the list of outgoing friend requests
To retrieve a list of outgoing friend requests, use this function:
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetListOutgoingFriendsResponseDelegate(AccelByte::Api::Lobby::FListOutgoingFriendsResponse::CreateLambda([](const FAccelByteModelsListOutgoingFriendsResponse & Result) {
if (Result.Code == "0") {
// Do something if ListOutgoingFriendsResponseDelegate fails
} else {
// Do something if ListOutgoingFriendsResponseDelegate succeeds
}
}));
FRegistry::Lobby.ListOutgoingFriends();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().ListOutgoingFriends(result => {
if (result.IsError) {
// Do something if ListOutgoingFriends fails
Debug.Log($"Error ListOutgoingFriends, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if ListOutgoingFriends succeeds
}
});
Cancel outgoing Friend requests
You can cancel outgoing friend requests using User ID. To get the user ID, you need to retrieve a list of outgoing friend requests, copy the User ID, and store it somewhere safe for the following function.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetCancelFriendsResponseDelegate(AccelByte::Api::Lobby::FCancelFriendsResponse::CreateLambda([](const FAccelByteModelsCancelFriendsResponse & Result) {
if (Result.Code == "0") {
// Do something if CancelFriendsResponseDelegate succeeds
} else {
// Do something if CancelFriendsResponseDelegate fails
}
}));
FString UserId = FString("SomeTargetCancelFriendUserId");
FRegistry::Lobby.CancelFriendRequest(UserId);
string userId = "SomeTargetOutgoingFriendUserId";
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().CancelFriendRequest(userId, result => {
if (result.IsError) {
// Do something if CancelFriendRequest fails
Debug.Log($"Error CancelFriendRequest, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if CancelFriendRequest succeeds
}
});
Listening for Friend notifications
Based on the different friend interactions players take, they will receive notifications from the Lobby service to inform them of a pending action. You will need to register delegates to listen for each of these notifications:
- You have received friend request from another player
- A player has accepted your friend request
- A player has rejected your friend request
- A player has canceled their pending friend request they sent you
- A friend has unfriended you
Incoming Friend notifications
Register to listen for a notification when you receive an incoming friend request.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetOnIncomingRequestFriendsNotifDelegate(AccelByte::Api::Lobby::FRequestFriendsNotif::CreateLambda([](const FAccelByteModelsRequestFriendsNotif & Result) {
// Do something if OnIncomingRequestFriendsNotifDelegate succeeds
}));
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().OnIncomingFriendRequest += result => {
if (result.IsError) {
// Do something if OnIncomingFriendRequest fails
Debug.Log($"Error OnIncomingFriendRequest, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if OnIncomingFriendRequest succeeds
}
};
Accepted Friend request notification
Register to listen for a notification when your friend accepts your friend request.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetOnFriendRequestAcceptedNotifDelegate(AccelByte::Api::Lobby::FAcceptFriendsNotif::CreateLambda([](const FAccelByteModelsAcceptFriendsNotif & Result) {
// Do something if OnFriendRequestAcceptedNotifDelegate succeeds
}));
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().FriendRequestAccepted += result => {
if (result.IsError) {
// Do something if FriendRequestAccepted fails
Debug.Log($"Error FriendRequestAccepted, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if FriendRequestAccepted succeeds
}
};
Rejected Friend request notification
Register to listen for a notification when a player rejects your friend request.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetOnRejectFriendsNotifDelegate(AccelByte::Api::Lobby::FRejectFriendsNotif::CreateLambda([](const FAccelByteModelsRejectFriendsNotif & Result) {
// Do something if OnRejectFriendsNotifDelegate succeeds
}));
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().FriendRequestRejected += result => {
if (result.IsError) {
// Do something if FriendRequestRejected fails
Debug.Log($"Error FriendRequestRejected, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if FriendRequestRejected succeeds
}
};
Canceled outgoing Friend request notification
Register to listen for a notification when another player cancels their friend request.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetOnCancelFriendsNotifDelegate(AccelByte::Api::Lobby::FCancelFriendsNotif::CreateLambda([](const FAccelByteModelsCancelFriendsNotif & Result) {
// Do something if OnCancelFriendsNotifDelegate succeeds
}));
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().FriendRequestCanceled += result => {
if (result.IsError) {
// Do something if FriendRequestCanceled fails
Debug.Log($"Error FriendRequestCanceled, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if FriendRequestCanceled succeeds
}
};
Unfriend notification
Register to listen for a notification when a player unfriends another player.
- Unreal Engine
- Unity
FRegistry::Lobby.SetOnUnfriendNotifDelegate(AccelByte::Api::Lobby::FUnfriendNotif::CreateLambda([](const FAccelByteModelsUnfriendNotif & Result) {
// Do something if OnUnfriendNotifDelegate succeeds
}));
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().Connect();
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().OnUnfriend += result => {
if (result.IsError) {
// Do something if OnUnfriend fails
Debug.Log($"Error OnUnfriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if OnUnfriend succeeds
}
};