UGameplayStatics::GetNumPlayerControllers
#include "Kismet/GameplayStatics.h"
Access: public
Specifiers: staticBlueprintPure
Description
Returns the total number of player controllers available in the world, including remote player controllers when called on a server.
Caveats & Gotchas
- • On a network client, only local player controllers are available — remote controllers do not exist on clients, so this returns only the local player count there.
- • The returned count can be used safely as the upper bound for GetPlayerController's PlayerIndex, but only on the machine that called it; the same index is not meaningful on a different machine.
- • UnsafeDuringActorConstruction: GameState may not be initialized when called from constructors.
Signature
static ENGINE_API int32 GetNumPlayerControllers(const UObject* WorldContextObject); Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| WorldContextObject | const UObject* | Object used to retrieve the world context. | — |
Return Type
int32 Example
Iterate all controllers on the server C++
// On the server, this includes remote player controllers
int32 Num = UGameplayStatics::GetNumPlayerControllers(this);
for (int32 i = 0; i < Num; ++i)
{
APlayerController* PC = UGameplayStatics::GetPlayerController(this, i);
if (PC && PC->HasAuthority()) { /* server-side logic */ }
} Tags
Version History
Introduced in: 5.1
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?