RealDocs

AGameModeBase::GetNumPlayers

function Engine Blueprint Since 4.14
#include "GameFramework/GameModeBase.h"
Access: public Specifiers: virtualENGINE_APIUFUNCTIONBlueprintCallable

Description

Returns the number of active human players currently in the game, excluding spectators. Useful for enforcing player caps or displaying lobby headcounts.

Caveats & Gotchas

  • Only meaningful on the server or in standalone. Calling this on a client always returns 0 because GameMode does not exist on clients.
  • Spectators are explicitly excluded. If you need the total connection count, iterate GameState->PlayerArray instead.

Signature

UFUNCTION(BlueprintCallable, Category=Game)
virtual int32 GetNumPlayers()

Return Type

int32

Examples

Warn when the player count reaches the cap Blueprint
Event Tick Delta Seconds Delta Seconds Get Num Players Target is Game Mode Base Target Return Value Branch Condition Condition True False Print String In String Player cap reached! Player cap reached! >= 4
Edit Blueprint graph Warn when the player count reaches the cap
Drag node headers to move · Drag from an output pin to an input pin to connect · Scroll to zoom · Right-click for actions
Enforce a max-player cap at login C++
void AMyGameMode::PreLogin(const FString& Options, const FString& Address,
    const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage)
{
    Super::PreLogin(Options, Address, UniqueId, ErrorMessage);
    if (ErrorMessage.IsEmpty() && GetNumPlayers() >= MaxPlayers)
    {
        ErrorMessage = TEXT("Server is full.");
    }
}

Version History

Introduced in: 4.14

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.