How to switch to another player controller with C++?

Hi all, I’ve posted a similar question to blueprint section and seems that it’s not feasible to change player controller through BP, so I’m now trying to find a solution under C++ coding.

The situation is that I’m using a 3rd party player controller class and it works well, but I wanna switch it back to those default player controller class after trigger certain event when play.

I’ve found function like AGameMode::SwapPlayerControllers(APlayerController* OldPC, APlayerController* NewPC), which is used when switching between levels. I’m fresh to UE4 and got no idea on how to apply that into code (like how to get current game mode / player controller, and how to create a new player controller).

Would anyone suggest if this function can be used and how to apply it for changing player controller purpose? It’ll also help if you got any other solutions for this. Thanks in advance.

There’s a static function in Kismet framework, which allows to get GameMode.

You just have to include it’s header file where you want to use it.

Back to your problem. I’d recommend this setup. Here’s code for GameMode:

#pragma once

#include "GameFramework/GameMode.h"
#include "MyProjectGameMode.generated.h"

/**
 * 
 */
UCLASS()
class MYPROJECT_API AMyProjectGameMode : public AGameMode
{
	GENERATED_BODY()
	
public:
	
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="Controllers")
	TSubclassOf<APlayerController> MyControllerOne;

	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="Controllers")
	TSubclassOf<APlayerController> MyControllerTwo;
	
};

Now you can easily create GameMode BP in Editor and assign controllers you need so you can have access to your controllers and switch between them.

It is not recommended to switch the PlayerController while you are in the same map, it has a lot of data a information about the current game. I have some plug-able functionality that I can just add or remove to a PlayerControl which I then delegate through a chain of responsibility, it’s not the same as switching the PC but you can achieve what you want.

Thanks for your clear and detailed response. But I got some further concern: while I write a game mode as you suggested (the controller class is set as EditAnywhere & BlueprintReadWrite), the setting neither shows up in the GameMode Default panel, nor get accessed in graph panel.

Did I miss anything?

#pragma once

#include "RollercoasterPlayerController.h"
#include "RollercoasterGameMode.generated.h"

/**
*
*/
UCLASS()
class ARollercoasterGameMode : public AGameMode
{
	GENERATED_UCLASS_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = SwitchController)
	TSubclassOf<APlayerController> MyDefaultController;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = SwitchController)
	TSubclassOf<ARollercoasterPlayerController> RCController;
};

26460-gamemode+default+panel.png

That’s really strange. What engine version do you use?

As I can see you’ve got another uproperty (Default Player Controller) working fine.

I created a new project then those game mode setting things go smoothly (using UE4.6). I drafted game mode blueprint like below and nothing happened.

26716-set+certain+playercontroller+class.png

Only when I set the trigger event as “Event Begin Play”, the playercontroller will be set to which I want. Maybe it’s set as that to avoid us changing game mode during play?

BTW I’ve worked out a solution with SwapPlayerControllers at last and I’ll post it to my question :stuck_out_tongue:

Got a solution with the function SwapPlayerControllers in GameMode class: https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/Engine/Private/GameMode.cpp

  1. Create a new game mode class
  2. Override SwapPlayerControllers and make it blueprint callable
  3. Spawn new controller → swap → spawn new pawn → let the new controller possess the new pawn (in blueprint)

Many say it’s not recommended to change player controller like that during play in the same level, but it’s still a solution in case anyone need it. The question will stay unsolved and wait for a better answer :slight_smile:

You shouldn’t switch player controller class during runtime just by setting it. you really should use swap function instead (implementation you’ve already found).

I just gave you the way you get references to necessary classes for that function.

I don’t know what exact critical information will you lose while swapping your controllers. Maybe others will tell us. I keep all the critical data I don’t want to lose in some persistent object. Anyway, there’s just one rule for this case: if it works for your project — then use it. =)