Start/Switch to Spectator via Blueprints

How do you start a player as spectator, or switch him to spectator via Blueprint? All of the solutions I have found are C++ specific (use ChangeState and bIsSpectator, etc). ChangeState does not seem to be exposed to BP and bIsSpectator cannot be changed in BP either.

I imagine I can spawn a Spectator pawn and possess it, but I doubt that’s the solution since I don’t think that changes the player’s actual state to spectator.

Thanks!

For anyone else who finds this, I came across the same problem. Changing a player to spectator mode seems like it really ought to be exposed to blueprints.

If you are making a c++ project and want to expose ChangeState to blueprints, the answer is to create a c++ class derived from PlayerController.

MyPlayerController.h:

UCLASS()
class YOURPROJECT_API AMyPlayerController : public APlayerController
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable, Category = State)
	void BP_ChangeState_Spectator();

	UFUNCTION(BlueprintCallable, Category = State)
	void BP_ChangeState_Player();
	
	
};

MyPlayerController.cpp:

#include "YourProject.h"
#include "MyPlayerController.h"
#include "GameFramework/PlayerState.h"


void AMyPlayerController::BP_ChangeState_Spectator()
{
	ChangeState(NAME_Spectating);
	if (Role == ROLE_Authority && PlayerState != NULL)
	{
		PlayerState->bIsSpectator = true;
	}
}

void AMyPlayerController::BP_ChangeState_Player()
{
	ChangeState(NAME_Playing);
	if (Role == ROLE_Authority && PlayerState != NULL)
	{
		PlayerState->bIsSpectator = false;
	}
}

Then your PlayerController blueprint is derived from MyPlayerController. If you already have a playercontroller blueprint, you can easily change the parent class in ‘blueprint settings.’

The ChangeState function will automatically spawn a SpectatorPawn and possess it.

If your game is multiplayer I think you have to call your new blueprint functions on the server and the client.

thanks a lot :slight_smile:

This code works almost properly, but after changing state this way GameMode function GetNumPlayers() and GetNumSpectators() will return wrong value.
This code fixes it.

#include "YourProject.h"
#include "CustomPlayerController.h"
#include "GameFramework/PlayerState.h"


void ACustomPlayerController::BP_ChangeState_Spectator()
{
	ChangeState(NAME_Spectating);
	if (Role == ROLE_Authority && PlayerState != NULL)
	{
		PlayerState->bIsSpectator = true;
		PlayerState->bOnlySpectator = true;
	}
}

void ACustomPlayerController::BP_ChangeState_Player()
{
	ChangeState(NAME_Playing);
	if (Role == ROLE_Authority && PlayerState != NULL)
	{
		PlayerState->bIsSpectator = false;
		PlayerState->bOnlySpectator = false;
	}
}

Thanks for help!

Thanks

any reason why this isnt exposed to blueprint ? When i choose to make a blueprint project only, i expect to be blueprint only but if EPIC dont expose everything for blueprint its impossible to do it only blueprint… Can we make a game with blueprint only or we always have to expose things ourself in c++ …