RealDocs

APawn::GetPlayerStateChecked

function Engine Since 4.0
#include "GameFramework/Pawn.h"
Access: public Specifiers: const

Description

Returns the PlayerState cast to the specified type, using CastChecked which asserts if the cast fails. Use this only when you are certain the PlayerState is of the expected type and want a crash instead of a null pointer on mismatch.

Caveats & Gotchas

  • CastChecked triggers a check() assertion in Debug and Development builds and a fatal crash in Shipping — calling this when PlayerState is null or a different type will crash the game. Only use in code paths guaranteed to have a typed PlayerState.
  • In multiplayer this is risky during server-client transitions: the replicated PlayerState may not have arrived yet, leaving the pointer null when you expect a valid typed pointer.

Signature

template<class T> T* GetPlayerStateChecked() const

Return Type

T*

Example

Checked cast in a server-authority-only function C++
void AMyGameMode::OnPlayerScored(APawn* ScoringPawn)
{
	// Safe: this function is only called server-side after possession is confirmed
	AMyPlayerState* PS = ScoringPawn->GetPlayerStateChecked<AMyPlayerState>();
	PS->AddScore(10);
}

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.