AGameModeBase::GetNumPlayers
#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
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.");
}
} See Also
Version History
Introduced in: 4.14
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?