Integrating friends with the client SDK
Overview
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.
Implementing 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 has been successful
}), FErrorHandler::CreateLambda([](int32 ErrorCode,
const FString & ErrorMessage) {
// Do something if SearchUsers has an error
UE_LOG(LogTemp, Log, TEXT("Error SearchUsers, Error Code: %d Error Message: %s"), ErrorCode, * ErrorMessage);
}));
string query = "UsersDisplayName";
SearchType by = SearchType.DISPLAYNAME;
AccelBytePlugin.GetUser().SearchUsers(query, by, result => {
if (result.IsError) {
// Do something if SearchUsers has an error
Debug.Log($"Error SearchUsers, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if SearchUsers has been successful
}
});
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 has been successful
}), FErrorHandler::CreateLambda([](int32 ErrorCode,
const FString & ErrorMessage) {
// Do something if LoadFriendListResponseDelegate has an error
UE_LOG(LogTemp, Log, TEXT("Error LoadFriendListResponseDelegate, Error Code: %d Error Message: %s"), ErrorCode, * ErrorMessage);
}));
FRegistry::Lobby.LoadFriendsList();
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().LoadFriendsList(result => {
if (result.IsError) {
// Do something if LoadFriendsList has an error
Debug.Log($"Error LoadFriendsList, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if LoadFriendsList has been successful
}
})
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 has been successful
}), FErrorHandler::CreateLambda([](int32 ErrorCode,
const FString & ErrorMessage) {
// Do something if RequestFriendsResponseDelegate has an error
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";
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().RequestFriend(userId, result => {
if (result.IsError) {
// Do something if RequestFriend has an error
Debug.Log($"Error RequestFriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if RequestFriend has been successful
}
});
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 has been successful
} else {
// Do something if ListIncomingFriendsResponseDelegate has an error
}
}));
FRegistry::Lobby.ListIncomingFriends();
FRegistry::Lobby.LoadFriendsList();
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().ListIncomingFriends(result => {
if (result.IsError) {
// Do something if ListIncomingFriends has an error
Debug.Log($"Error ListIncomingFriends, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if ListIncomingFriends has been successful
}
});
Accept Incoming Friend Requests
After a friend request has been sent, the player who received the request can either accept or reject it. Use the following function to accept a friend request.
- 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 has been successful
} else {
// Do something if AcceptFriendsResponseDelegate has an error
}
}));
FString UserId = FString("SomeUserId");
FRegistry::Lobby.AcceptFriend(UserId);
string userId = "SomeTargetedAcceptedFriendUserId";
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().AcceptFriend(userId, result => {
if (result.IsError) {
// Do something if AcceptFriend has an error
Debug.Log($"Error AcceptFriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if AcceptFriend has been successful
}
});
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 has been successful
} else {
// Do something if RejectFriendsResponseDelegate has an error
}
}));
FString UserId = FString("SomeTargetRejectFriendUserId");
FRegistry::Lobby.RejectFriend(UserId);
string userId = "SomeTargetRejectedFriendUserId";
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().RejectFriend(userId, result => {
if (result.IsError) {
// Do something if RejectFriend has an error
Debug.Log($"Error RejectFriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if RejectFriend has been successful
}
});
Unfriend Players
Use the following function to unfriend another player.
- 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 has been successful
} else {
// Do something if UnfriendResponseDelegate has an error
}
}));
FString UserId = FString("SomeTargetFriendUserId");
FRegistry::Lobby.Unfriend(UserId);
string userId = "SomeTargetFriendUserId";
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().Unfriend(userId, result => {
if (result.IsError) {
// Do something if Unfriend has an error
Debug.Log($"Error Unfriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if Unfriend has been successful
}
});
Retrieve the List of Outgoing Friend Requests
Use the following function to retrieve a list of outgoing friend requests.
- 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 has an error
} else {
// Do something if ListOutgoingFriendsResponseDelegate has been successful
}
}));
FRegistry::Lobby.ListOutgoingFriends();
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().ListOutgoingFriends(result => {
if (result.IsError) {
// Do something if ListOutgoingFriends has an error
Debug.Log($"Error ListOutgoingFriends, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if ListOutgoingFriends has been successful
}
});
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 has been successful
} else {
// Do something if CancelFriendsResponseDelegate has an error
}
}));
FString UserId = FString("SomeTargetCancelFriendUserId");
FRegistry::Lobby.CancelFriendRequest(UserId);
string userId = "SomeTargetOutgoingFriendUserId";
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().CancelFriendRequest(userId, result => {
if (result.IsError) {
// Do something if CancelFriendRequest has an error
Debug.Log($"Error CancelFriendRequest, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if CancelFriendRequest has been successful
}
});
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 has been successful
}));
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().OnIncomingFriendRequest += result => {
if (result.IsError) {
// Do something if OnIncomingFriendRequest has an error
Debug.Log($"Error OnIncomingFriendRequest, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if OnIncomingFriendRequest has been successful
}
};
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 has been successful
}));
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().FriendRequestAccepted += result => {
if (result.IsError) {
// Do something if FriendRequestAccepted has an error
Debug.Log($"Error FriendRequestAccepted, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if FriendRequestAccepted has been successful
}
};
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 has been successful
}));
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().FriendRequestRejected += result => {
if (result.IsError) {
// Do something if FriendRequestRejected has an error
Debug.Log($"Error FriendRequestRejected, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if FriendRequestRejected has been successful
}
};
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 has been successful
}));
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().FriendRequestCanceled += result => {
if (result.IsError) {
// Do something if FriendRequestCanceled has an error
Debug.Log($"Error FriendRequestCanceled, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if FriendRequestCanceled has been successful
}
};
Unfriended 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 has been successful
}));
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().OnUnfriend += result => {
if (result.IsError) {
// Do something if OnUnfriend has an error
Debug.Log($"Error OnUnfriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if OnUnfriend has been successful
}
};
Synchronize friends from third-party platforms in bulk
This process is used to connect players' friends from third-party platforms IAM service. This process works in the background and is triggered automatically every time your game starts, or manually by interaction between a player and the game client.
To allow your players to sync their third-party platform friends lists with their AccelByte Cloud friends list, you must call user.BulkGetUserByOtherPlatformUserIds() and lobby.BulkRequestFriend() consecutively.
- Unreal Engine
- Unity
TArray < FString > ThirdPartyPlatformFriendId = {
"12345abcd",
"abcd12345"
};
EAccelBytePlatformType PlatformType = EAccelBytePlatformType::Steam;
FRegistry::User.BulkGetUserByOtherPlatformUserIds(PlatformType, ThirdPartyPlatformFriendId, THandler < FBulkPlatformUserIdResponse > ::CreateLambda([](const FBulkPlatformUserIdResponse & Result) {
TArray < FString > UserIds;
for (auto UserData: Result.UserIdPlatforms) {
UserIds.Add(UserData.UserId);
}
FAccelByteModelsBulkFriendsRequest BulkFriendsRequest;
BulkFriendsRequest.FriendIds = UserIds;
FRegistry::Lobby.BulkFriendRequest(BulkFriendsRequest, FVoidHandler::CreateLambda([]() {
// Do something if BulkFriendRequest has been successful
}), FErrorHandler::CreateLambda([](int32 ErrorCode,
const FString & ErrorMessage) {
// Do something if BulkFriendRequest has an error
UE_LOG(LogTemp, Log, TEXT("Error BulkFriendRequest, Error Code: %d Error Message: %s"), ErrorCode, * ErrorMessage);
}));
}), FErrorHandler::CreateLambda([](int32 ErrorCode,
const FString ErrorMessage) {
// Do something if BulkGetUserByOtherPlatformUserIds has an error
UE_LOG(LogTemp, Log, TEXT("Error BulkGetUserByOtherPlatformUserIds, Error Code: %d Error Message: %s"), ErrorCode, * ErrorMessage);
}));
string[] thirdPartyPlatfromFriendId = {
"12345abcd",
"abcd12345"
};
PlatformType platformType = PlatformType.Steam;
AccelBytePlugin.GetUser().BulkGetUserByOtherPlatformUserIds(platformType, thirdPartyPlatfromFriendId, result => {
if (result.IsError) {
// Do something if BulkGetUserByOtherPlatformUserIds has an error
Debug.Log($"Error BulkGetUserByOtherPlatformUserIds, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
List < string > userIds = new List < string > ();
foreach(var userData in result.Value.userIdPlatforms) {
userIds.Add(userData.userId);
}
AccelBytePlugin.GetLobby().BulkRequestFriend(userIds.ToArray(), result => {
if (result.IsError) {
// Do something if BulkRequestFriend has an error
Debug.Log($"Error BulkRequestFriend, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
} else {
// Do something if BulRequestFriend has been successful
}
});
}
});