AGameModeBase::CanSpectate
#include "GameFramework/GameModeBase.h"
Access: public
Specifiers: virtualUFUNCTIONBlueprintNativeEvent
Description
Determines whether a player is allowed to spectate another specific player. Override this to restrict or grant spectating permissions based on game rules, teams, or player status.
Caveats & Gotchas
- • Called server-side only; the result is not automatically communicated back to the client — you must handle any rejection UI yourself.
- • The default implementation always returns true, so if you want to restrict spectating (e.g., to same-team only), you must override this method.
- • ViewTarget can be null if the player being spectated has no PlayerState yet — always null-check before accessing it.
Signature
ENGINE_API bool CanSpectate(APlayerController* Viewer, APlayerState* ViewTarget); Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| Viewer | APlayerController* | The controller of the player requesting to spectate. | — |
| ViewTarget | APlayerState* | The PlayerState of the player to be spectated. | — |
Return Type
bool Example
Restrict spectating to same team C++
bool AMyGameMode::CanSpectate_Implementation(APlayerController* Viewer, APlayerState* ViewTarget)
{
if (!ViewTarget || !Viewer->PlayerState)
{
return false;
}
AMyPlayerState* ViewerState = Cast<AMyPlayerState>(Viewer->PlayerState);
AMyPlayerState* TargetState = Cast<AMyPlayerState>(ViewTarget);
// Only allow spectating players on the same team
return ViewerState && TargetState && ViewerState->TeamId == TargetState->TeamId;
} Version History
Introduced in: 4.14
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?