How to change player controller at runtime?

Hi there,

I searched for several hours a working method to switch my active player controller at runtime with blueprint. I basically just wanto support different control schemes (default and VR specific controls).

Possessing doesn’t work as I am not able to create an instance of my VRPlayerController and and assign it to my Game Mode (there is only a getter for ‘player controller’ defined).

So I tried to manually unpossess my pawn and possess it again with my VRPlayerController, but as have only a class of it and no instance it doesn’t work either…

So, how do set a new player controller as active with Blueprint at runtime?

Can’t you use SpawnActorFromClass to spawn the new player controller and then have it possess the pawn?

That won’t work as player controller is strictly assigned to player, spawning playercontroller will spawn dead playercontroller

Yes, that seems to be the problem. So how to do it the right way? Couldn’t find any working way.

Are you sure you need to be able to change it at runtime?

There is a function in C++ which does this however most of the time you’re advised against it. I’m not sure if it exists in blueprint as well but it shouldn’t be much of a challenge to expose it yourself.

EDIT: Sorry. I can only say that UnrealScript had this function… not entierly sure if UE4 still allows this.

However if you “simply” want to change between two input methods (e.g. VR and normal) and need the possibility to hotswap them (during gameplay) it might be worth doing both in the same class and simply splitting the behavior via a branch / variable.

If you don’t need the “hotswap” you can load a level with a different gamemode depending on the input you want.

Hello Erasio,

thanks for your answer. I’m not sure if I understood that correctly. Does that mean, that I have to branch every input action node right after the event node? Is there really no simple way to change the input scheme / activate another event graph?

Well it depends.

It shouldn’t be too much of a difference.

If you have like a strategy game and use lets say 20 buttons and want to change all of them this obviously is quite a bad solution.

But what is the difference between VR and “normal”?

Mainly the camera and maybe the movement. Actions should be the same and different buttons can be defined in the input settings. We’re purely talking about behavior here.

So you’d be with something around 4 - 8 points inside of your input where you actually have differences (at least as far as I imagine it) which should be fine if the majority is the same and you need that hotswap.

I mean changing will always affect quite a lot. Every variable you have will have to be transfered to the new controller and so on.

I use a complet different control scheme in VR, so switching the event graph would be a great help. (In fact i want to use the default control scheme if VR isn’t active with some extra buttons).

So it looks like I have to implement some layer of indirection on my own. Thanks anyway.

PS: Do you remember the name of the C++ function?

That would be swap player controllers inside of the game mode class.

I tried that, but that didn’t work. Controls were dead afterwards. I tried also to set my controller class with “Set default controller” and then “spawn default controller” but that didn’t work either.

Have you find a method with blueprint ?

+1 any method? thanks

No. At the end I ended up with one branch per input event like (Move Right → If head mounted display enabled then MoveRightVR() else MoveRight() and calling the event via an interface in different components.

Looks like the GameMode has a C++ function:

void AGameMode::SwapPlayerControllers(APlayerController* OldPC, APlayerController* NewPC)

You could create a C++ subclass of AGameMode and make a UFUNCTION that calls SwapPlayerControllers so you can do it from blueprint.

I’m not sure if this is a good idea or not.