Skip to main content

Retrieving the user ID of a player controller or player state actor

When you're in a multiplayer match, you might need to get a user ID from a player controller or player state actor in the match.

On servers, you'll be able to see all of the player controllers in the match and can retrieve the user ID the player controller.

On both clients and servers, you'll be able to see all of the player state actors in the match and can retrieve the user ID from player state.

Get the user ID from a player controller

Player controllers store their user ID differently based on whether they're a local player controller on the current machine or a remote player controller connected via a network connection. You can use the following function to retrieve the user ID regardless of the player controller type:

TSharedPtr<const FUniqueNetId> GetControllerUniqueNetId(APlayerController *InPlayerController)
{
if (!IsValid(InPlayerController))
{
return nullptr;
}

if (InPlayerController->IsLocalPlayerController())
{
ULocalPlayer *LocalPlayer = InPlayerController->GetLocalPlayer();
if (IsValid(LocalPlayer))
{
return LocalPlayer->GetPreferredUniqueNetId().GetUniqueNetId();
}
}

UNetConnection *RemoteNetConnection = Cast<UNetConnection>(InPlayerController->Player);
if (IsValid(RemoteNetConnection))
{
return RemoteNetConnection->PlayerId.GetUniqueNetId();
}

UE_LOG(LogTemp, Error, TEXT("Player controller does not have a valid remote network connection"));
return nullptr;
}

Get the user ID from a player state

Player states provide the user's ID through the GetUniqueId function. You can safely access the user ID on a player state with the following function:

TSharedPtr<const FUniqueNetId> GetPlayerStateUniqueNetId(APlayerState *InPlayerState)
{
if (!IsValid(InPlayerState))
{
return nullptr;
}

return InPlayerState->GetUniqueId().GetUniqueNetId();
}