To round out the Epic Online Services Getting Started articles on Epic Account Services—after having covered the authentication and presence interfaces—we’ll cover how to query for Epic Friends and their status using the Friends Interface. In this article, we’ll go over:
Before we dive into the query API, it’s important to understand how these APIs are scoped. As briefly explained in the previous article, by default the friends APIs will not return the full list of friends of a player that they would see in the Epic Games Launcher, for example. As we’ve seen, players will be prompted to consent to our EAS application accessing the specific information we specify (basic profile, presence, friends). Only after a player’s friend has provided this consent will the Friends Interface return that particular friend when querying. If players purchase the game through the Epic Games Store, consent is implicitly provided as part of the purchase flow. To illustrate:
Player A is friends with Player B and Player C, and purchases the game through Epic Games Store
Player B does not own the game
Player C purchases the game through Steam
In this scenario, Player A will not see any friends in the game when we call the query API until Player C launches the game and provides consent as part of the authentication flow. Player B will never be returned by the query API as they do not own the game.
Note that per the documentation, there are currently no APIs to manage friend requests or adding/deleting friends for the player. The Social Overlay will be expanded later this year to assist with that functionality.
We then add the friend, including friend status, to the ViewModel to be displayed in the UI.
Add a FriendsQueryCommand.cs class to the Commands folder:
Now open FriendsViewModel.cs again and instantiate FriendsQueryCommand:
Add the following line in the ViewModelLocator RaiseAuthCanExecuteChanged() method to ensure that the Query presence button is only enabled when a user is logged in:
Finally, add the PresenceView to our MainWindow TabControl:
Now we can run the app, authenticate, and consent to the updated permissions in the browser, after which we’ll be able to use the Query friends button to get the player’s friends and status. As expected, however, we won’t get any friends back from the API, as none of them have consented to our application. Let’s fix that.
To validate our code, we have to get a friend to consent to our application. Note that only friends who have accepted your friend request will be returned by the QueryFriends API we implemented. If this were an actual game we were developing, we could complete a Brand Review submission for our application and share our game with a friend to get consent.
However, as we’re only developing this sample, we’ll have to add a friend to our Developer Portal Organization, as otherwise they will not be able to access our application. This is done to protect Epic Games ecosystem users, you (the developer), other game developers, and Epic Games from brand spoofing, phishing, and more.
Click on the Organization tab on the left and use the blue Invite button to invite a friend, selecting an appropriate Role.
Adding someone to the Organization
Now our friend has to run our sample application, connecting to our product, client, sandbox, and deployment. Package and share the following files from the /bin/ directory of the project:
The first time they run the application, they will get prompted for consent, and should see our account show up when Querying friends. Subsequently, when we do the same after the friend has provided consent, their account will show up when Querying friends.
Queried friends for the logged in user
Note that we only see friends’ Epic Account ID and not their display name, but you can use the same QueryUserInfo call we used in AuthService.Login() to retrieve more information for each friend. The status column will always display “Offline” at this point, as that’s the 0 value of the Epic.OnlineServices.Presence.Status enum, but we’ll tackle that further in this article.
Once you’ve queried for friends, you should subscribe to notifications when the friend status changes (rather than query again). The Friends Interface will return one of these four statuses for a friend, per the Epic.OnlineServices.Friends.FriendsStatus enum: NotFriends, InviteSent, InviteReceived, Friends. While there are no APIs to manage friends directly, as mentioned earlier, we still need to subscribe to updates so we know when a friend request is accepted or someone is unfriended. The Friends Interface provides APIs to subscribe and unsubscribe to these updates, so let’s implement them.
Open FriendsService.cs and add the following methods:
We call AddNotifyFriendsUpdate, passing in empty options, to get a Notification ID back, which both confirms that the notification has been set up and we’ll need to use to unsubscribe from updates (as seen in UnsubscribeUpdates()).
We then define a callback method with the functionality we want when a notification is sent. In this case we simply add another record to the Friends list with the new status.
We’ll also add a trigger to these commands’ CanExecuteChanged behaviors, so they’re only available after we do an initial query for friends:
Below the following lines in FriendsService.QueryFriends():
var friendStatus = App.Settings.PlatformInterface.GetFriendsInterface().GetStatus(getStatusOptions);
Add a FriendsSubscribeUpdatesCommand.cs class to the Commands folder:
Add a FriendsUnsubscribeUpdatesCommand.cs class to the Commands folder:
Declare and initialize our new command in FriendsViewModel.cs:
Now when we run the app, we can query for friends and subscribe/unsubscribe updates. Once subscribed for updates, you can remove a listed friend through the Epic Games Launcher and you’ll see a new entry appear for that friend in the ListView with a status of NotFriends.
The last thing for us to do is implement the functionality to query for friends’ presence status, building on what we implemented in the last article. Now we can make use of QueryPresence to retrieve presence for each friend.
Open PresenceQueryCommand from the Commands folder and replace both methods with the following code:
We modify the CanExecute code to also check for a parameter (the selected Friend we’ll pass from our FriendsView ListView) and add the “else” statement to Execute to call PresenceService.Query() when a Friend is passed.
Open FriendsViewModel and instantiate PresenceQueryCommand:
Finally, add the following to the FriendsListView_SelectionChanged event handler in FriendsView.xaml.cs to ensure the query status button is only enabled when a friend is selected:
Now when we run the application, hit Query friends, select a friend that’s online, and hit Query status, we’ll see their status update in the UI.
Queried friends status
As mentioned in the previous article, you should implement AddNotifyOnPresenceChanged in a production game to get notified when there’s a change in presence status for friends.
In the next article, we’ll start using Game Services, which are the platform-agnostic back-end services in Epic Online Services. We’ll start with explaining the Connect Interface, which is used to authenticate users to these services using one of many identity providers.
You can find the full list of articles in this series in the series reference and if you have questions or feedback about this article, use the button below to head to the Community forum and let us know.