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.
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.
Override SwapPlayerControllers and make it blueprint callable
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
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. =)