Skip to main content

Read the recent players list

EOS Online Subsystem provides the "recent players" functionality of the friends interface. It does this by storing a list of recently seen players in Player Data Storage.

You don't need to manually add recently seen players; the plugin will check what other players are seen in multiplayer matches and automatically add them to the recent players list.

Reading the recent players list

Before you can get the list of recent players, you need to call QueryRecentPlayers. Before you call QueryRecentPlayers for the first time, the list of recent players will be empty. This is similar to how the friends list (ReadFriendsList) operation works.

First, get the online friends and identity interfaces:

#include "OnlineSubsystem.h"
#include "OnlineSubsystemUtils.h"
#include "Interfaces/OnlineFriendsInterface.h"
#include "Interfaces/OnlineIdentityInterface.h"

// ...

IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineFriendsPtr Friends = Subsystem->GetFriendsInterface();
IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();

Then, call QueryRecentPlayers:

// You need to store the handle so you can deregister the event listener
// in the callback.
this->QueryRecentPlayersHandle =
Friends->AddOnQueryRecentPlayersCompleteDelegate_Handle(FOnQueryRecentPlayersCompleteDelegate::CreateUObject(
this,
&UMyClass::OnQueryRecentPlayersComplete));

if (!Friends->QueryRecentPlayers(*Identity->GetUniquePlayerId(0), TEXT("")))
{
// Call failed to start.
}

// ...

void UMyClass::OnQueryRecentPlayersComplete(
const FUniqueNetId &UserId,
const FString &Namespace,
bool bWasSuccessful,
const FString &Error)
{
IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineFriendsPtr Friends = Subsystem->GetFriendsInterface();
IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();

Friends->ClearOnQueryRecentPlayersCompleteDelegate_Handle(this->QueryRecentPlayersHandle);

// Check bWasSuccessful; if it's true, you can now call GetRecentPlayers.
}

Getting the cached recent players list

Once QueryRecentPlayers has completed successfully, you can then call GetRecentPlayers:

TArray<TSharedRef<FOnlineRecentPlayer>> RecentPlayers;
if (Friends->GetRecentPlayers(*Identity->GetUniquePlayerId(0), TEXT(""), RecentPlayers))
{
for (const auto &RecentPlayer : RecentPlayers)
{
// Access the recent player via `RecentPlayer`.
}
}
else
{
// Recent players could not be read.
}

Receive notifications when recent players change

The plugin automatically keeps track of players that a local user has recently seen on multiplayer servers. Therefore, the recent players list can update by itself, and you'll need to listen for the OnRecentPlayersAdded event to know when it changes:

FDelegateHandle Handle = Friends->AddOnRecentPlayersAddedDelegate_Handle(
FOnRecentPlayersAddedDelegate::CreateUObject(this, &UMyClass::OnRecentPlayersChanged));
// You can use `Handle` later to call `Friends->ClearOnRecentPlayersAddedDelegate_Handle(Handle)`
// if you want to unregister the event handler.

// ...

void UMyClass::OnRecentPlayersChanged(
const FUniqueNetId& UserId,
const TArray<TSharedRef<FOnlineRecentPlayer>>& AddedPlayers)
{
// The recent players list has changed. AddedPlayers contains a list
// of the players that were added.
}