Skip to main content

Find user by product user ID

You can fetch user account information based on their player ID. Although interfaces such as the friends interface return user accounts by default, you might need to lookup the accounts of other players in a multiplayer game, so you can show the display names of those users in-game or in match results.

Query users based on IDs

To load one or more accounts from player IDs, first get the user interface, like so:

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

// ...

IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineUserPtr User = Subsystem->GetUserInterface();

Register the event handler so you know when the users have been looked up. QueryUserInfoDelegateHandle is declared as an FDelegateHandle.

this->QueryUserInfoDelegateHandle = User->AddOnQueryUserInfoCompleteDelegate_Handle(
0, // Should match the LocalUserNum passed to `QueryUserInfo` below.
FOnQueryUserInfoComplete::FDelegate::CreateUObject(
this,
&UMyClass::HandleQueryUserInfoComplete));

Then add the player IDs (TSharedRef<const FUniqueNetId>) to an array, and call QueryUserInfo:

TArray<TSharedRef<const FUniqueNetId>> UserIds;
UserIds.Add(/* first player ID to lookup */);
// ... more user IDs here ...

User->QueryUserInfo(0 /* LocalUserNum */, UserIds);

When your callback fires, you'll want to handle any errors (check bWasSuccessful), and then deregister the event handler:

void UMyClass::HandleQueryUserInfoComplete(
int32 LocalUserNum,
bool bWasSuccessful,
const TArray<TSharedRef<const FUniqueNetId>> &UserIds,
const FString &ErrorStr)
{
// TODO: Check bWasSuccessful, if it wasn't successful the error will be in ErrorStr.

IOnlineSubsystem *Subsystem = Online::GetSubsystem(WorldContextObject->GetWorld());
IOnlineUserPtr User = Subsystem->GetUserInterface();

if (bWasSuccessful)
{
for (auto UserId : UserIds)
{
// You can now call GetUserInfo to get the account for the returned IDs.
TSharedPtr<FOnlineUser> UserAcc = User->GetUserInfo(LocalUserNum, *UserId);

// ...
}
}

User->ClearOnQueryUserInfoCompleteDelegate_Handle(LocalUserNum, this->QueryUserInfoDelegateHandle);
this->QueryUserInfoDelegateHandle.Reset();
}