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_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
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:
A common solution can be found in this wiki post it seems. In short, overwrite
GameViewportClient and set it as default, then within, overwrite
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.
There’s are also some points worth mentioning:
AControllerwill 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 call
CreatePlayersto create up to the set amount of players.
Skip Assigning Gamepad to Player 1will 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.