RealDocs

UGameplayStatics::GetNumPlayerStates

function Engine Blueprint Since 5.1
#include "Kismet/GameplayStatics.h"
Access: public Specifiers: staticBlueprintPure

Description

Returns the number of entries in the game state's PlayerArray, which equals the number of connected players — local and remote — on both client and server.

Caveats & Gotchas

  • PlayerStates replicate from server to clients; on a client, the count and array contents may lag slightly behind the server during join/leave transitions.
  • This count includes remote players whose player controllers are not available on this machine. Use GetNumPlayerControllers if you only want players with active controllers.
  • UnsafeDuringActorConstruction: do not call during construction scripts or constructors — GameState may not yet be initialized.

Signature

static ENGINE_API int32 GetNumPlayerStates(const UObject* WorldContextObject);

Parameters

Name Type Description Default
WorldContextObject const UObject* Object used to retrieve the world context.

Return Type

int32

Example

Log all connected player names on the server C++
int32 NumPlayers = UGameplayStatics::GetNumPlayerStates(this);
for (int32 i = 0; i < NumPlayers; ++i)
{
    APlayerState* PS = UGameplayStatics::GetPlayerState(this, i);
    if (PS) UE_LOG(LogGame, Log, TEXT("Player %d: %s"), i, *PS->GetPlayerName());
}

Version History

Introduced in: 5.1

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.