APawn::GetPlayerState
#include "GameFramework/Pawn.h"
Access: public
Specifiers: const
Description
Returns the PlayerState associated with this pawn, or null if the pawn is not possessed by a player. PlayerState is replicated to all clients, making this the reliable way to access per-player persistent data in multiplayer.
Caveats & Gotchas
- • PlayerState is null for AI-controlled pawns — always null-check before use, or use the templated overload Cast<T> form. Use GetPlayerStateChecked<T>() only when you are certain the cast must succeed, as it will assert on failure.
- • During brief possession transitions on clients, PlayerState may temporarily be null even on a player-controlled pawn; do not assume non-null just because the pawn appears controlled.
Signature
APlayerState* GetPlayerState() const Return Type
APlayerState* Example
Read score from a custom PlayerState C++
if (AMyPlayerState* PS = GetPlayerState<AMyPlayerState>())
{
UE_LOG(LogTemp, Log, TEXT("Score: %d"), PS->GetScore());
} Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?