To bind actions and axes in the character blueprint class, I may have multiple such character instances for each new APlayerController. Merely 1 Action Name (e.g. avoiding UseSkill_P1
, UseSkill_P2
, etc) is required for each character by adding the BindAction nodes in the Player Controller. However, for more functionality, a similar implementation is necessary in cpp.
There exist similar questions, but they are either: not specific, outdated, or lack answers. With this question, I am hoping to provide a good reference point for anyone wanting to setup a local multiplayer (couch co-op) game.
What I've tried:
When I bind actions and axes (delegates) in the APlayerController
's SetupInputComponent
(e.g: InputComponent->BindAction("Attack", IE_Pressed, this, &AMyController::DoSomething);
, then spawn multiple players with UGameInstance::CreateLocalPlayer(index, err, true)
, the editor will crash on pressing Play in Editor (Alt+P).
The error thrown during runtime is: Access violation - code c0000005 (first/second chance not available)
and referencing the line where the first ActionBind
appeared, e.g: UE4Editor_MyProject!AMyController::SetupInputComponent() [d:\path\to\project\source\private\mycontroller.cpp:66]
A common solution can be found in this wiki post it seems. In short, overwrite GameViewportClient
and set it as default, then within, overwrite InputAxis
and InputKey
and increment the player controller index for each input device (gamepad) used.
As mentioned previously, this would require the project to specify a unique Action Name for each player index e.g. Attack_P4
.
There’s are also some points worth mentioning:
- An
AController
will not be created for each input device connected. - 1-viewport-multiplayer isn’t supported as split-screen. (Weird naming, but good for search engines).
- Spawning new Local Players is generally done in your override of
AGameMode::BeginPlay
. I made it up to the deriving BPs of my Game Mode to callCreatePlayers
to create up to the set amount of players. - Setting
Skip Assigning Gamepad to Player 1
will allow keyboard to be used for player 1.
Hopefully there are some common best practices to tackle this problem. Any suggestion is very much appreciated!
PS Here is the related gamedev question.