Skip to main content

Retrieving statistics

You can retrieve arbitrary statistic values for any EOS user. Although stats are used to drive achievements and leaderboards, you can use them for any kind of integer-based data storage you want.

Retrieving stats from EOS

To retrieve statistics for a user, use the QueryStats function on the stats interface:

#include "OnlineSubsystem.h"
#include "OnlineSubsystemUtils.h"
#include "Interfaces/OnlineStatsInterface.h"

// ...

IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();
IOnlineStatsPtr StatsInterface = Subsystem->GetStatsInterface();

// The list of users to fetch stats for.
TArray<TSharedRef<const FUniqueNetId>> Users;
Users.Add(Identity->GetUniquePlayerId(0).ToSharedRef());

// The stats to retrieve.
TArray<FString> StatNames;
StatNames.Add(TEXT("StatName1"));
StatNames.Add(TEXT("StatName2"));

// The first parameter is the user performing the querying.
Stats->QueryStats(
Identity->GetUniquePlayerId(0).ToSharedRef(),
Users,
StatNames,
FOnlineStatsQueryUsersStatsComplete::CreateLambda([](
const FOnlineError &ResultState,
const TArray<TSharedRef<const FOnlineStatsUserStats>> &UsersStatsResult)
{
if (!ResultState.bSucceeded)
{
return;
}

// Each entry in UsersStatsResult is for a user.
for (auto QueriedStats : UsersStatsResult)
{
// Each entry in Stats is for a stat for that user.
for (auto KV : QueriedStats->Stats)
{
// KV.Key is the stat name.
int32 Value;
KV.Value.GetValue(Value);
}
}
}));