Integrating presence with the client SDK
Overview
The Presence service can be used to set and retrieve player presence status so you can keep your user base informed of what other players are currently doing in game. To manage these statuses, you will use the Lobby interface to call presence functions, which then will be sent through WebSocket to ensure statuses are updated in real-time.
Because each game is unique and may need to provide more than just online / offline states, we provide two parameters that you can set and retrieve:
Availability - Can be set to one of the following states: Offline, Available, Busy, Away, and Invisible. If a player disconnects from the Lobby, this will automatically be updated to offline.
Activity - This is customizable in order to allow the game to provide unique / specific details about a player’s current state such as Playing a Game, In Lobby, In a Match, or In Party Looking For Members.
Implementing Presence
When integrating the service into your game client, keep in mind this general flow for setting and retrieving player statuses:
- On game start, connect to the Lobby service and set the player’s status to Available.
- Retrieve the statuses of a player’s friends list so they can be displayed.
- Register to listen for changes to friend statuses so that they can be updated.
- As the player changes game play activities, be sure to set their status accordingly.
Set Player Status
To set a player’s presence from the game client, you will call the update function and provide both the availability and activity states you want to set for the player as parameters.
- Unreal Engine
- Unity
Availability Availability = Availability::Available;
FString Activity = FString("My New Activity");
FRegistry::Lobby.Connect();
FRegistry::Lobby.SendSetPresenceStatus(Availability, Activity);
UserStatus status = UserStatus.Availabe;
string activity = "My New Activity";
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().SetUserStatus(status, activity, result =>
{
if (result.IsError)
{
// Do something if SetUserStatus has an error
Debug.Log($"Error SetUserStatus, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if SetUserStatus has been successful
}
});
Listen for changes to Friend Statuses
In order to receive real time updates about a friend's status, you will need to register to listen for notifications. As a friend's status changes you can inform the player through the game client of the change, such as letting the player know a friend just came online.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetUserPresenceNotifDelegate(AccelByte::Api::Lobby::FFriendStatusNotif::CreateLambda([](const FAccelByteModelsUsersPresenceNotice& Result)
{
// Do something if UserPresenceNotifDelegate has been successful
}));
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().FriendsStatusChanged += result =>
{
if (result.IsError)
{
// Do something if FriendsStatusChanged has an error
Debug.Log($"Error FriendsStatusChanged, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if FriendsStatusChanged has been successful
}
};
Retrieve the Friends List Statuses
To provide presence statuses as part of displaying a friends list to players, you can call this function to get the status for all the player’s friends.
- Unreal Engine
- Unity
FRegistry::Lobby.Connect();
FRegistry::Lobby.SetGetOnlineFriendsPresenceResponseDelegate(AccelByte::Api::Lobby::FGetAllFriendsStatusResponse::CreateLambda([](const FAccelByteModelsGetOnlineUsersResponse& Result)
{
if (Result.Code == "0")
{
// Do something if GetOnlineFriendsPresenceResponseDelegate has been successful
}
else
{
// Do something if GetOnlineFriendsPresenceResponseDelegate has an error
}
}));
FRegistry::Lobby.SendGetOnlineFriendPresenceRequest();
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().ListFriendsStatus(result =>
{
if (result.IsError)
{
// Do something if ListFriendsStatus has an error
Debug.Log($"Error ListFriendsStatus, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if ListFriendsStatus has been successful
}
});
Retrieve Player Statuses in Bulk
In cases where you need to provide presence status between player’s who are not friends, you can use this function by passing in a list of User IDs. This is useful in situations such as players who are in a group or raid team who need to coordinate with each other even without being friends. This function can also be used to provide the count of players in a given status by setting the countOnly parameter to true when calling it.
- Unreal Engine
- Unity
TArray<FString> UserIds = {FString("12345abcd"), FString("abcd12345")};
bool CountOnly = true;
FRegistry::Lobby.Connect();
FRegistry::Lobby.BulkGetUserPresence(UserIds, THandler<FAccelByteModelsBulkUserStatusNotif>::CreateLambda([](const FAccelByteModelsBulkUserStatusNotif& Result)
{
// Do something if BulkGetUserPresence has been successful
}), FErrorHandler::CreateLambda([](int32 ErrorCode, const FString& ErrorMessage)
{
// Do something if BulkGetUserPresence has an error
UE_LOG(LogTemp, Log, TEXT("Error BulkGetUserPresence, Error Code: %d Error Message: %s"), ErrorCode, *ErrorMessage);
}), CountOnly);
string[] userIds = { "12345abcd", "abcd12345" };
bool countOnly = true;
AccelBytePlugin.GetLobby().Connect();
AccelBytePlugin.GetLobby().BulkGetUserPresence(userIds, result =>
{
if (result.IsError)
{
// Do something if BulkGetUserPresence has an error
Debug.Log($"Error BulkGetUserPresence, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
}
else
{
// Do something if BulkGetUserPresence has been successful
}
}, countOnly);