I was trying to find the picture of teenage me playing guitar naked like Blink 182 when...
(click the image for a larger version)
Note: uses UE4.2, there were some issues with split-screen in earlier versions.
Step-by-step instructions for enabling split-screen
1.
And that's it.
Mostly. As long as your player object has a camera, this will all just magically work once you add another player either by placing another player in the UI or through adding one in BluePrints using CreatePlayer().
Problem modifying the second player
I hit a snag trying to act on the second player through BluePrints.
I had created a Character-derived player class called Player_BP and set up the BluePrint graph for it so that when the "change colour" key-event fires, the material of the player mesh changes.
I did this by clicking on the mesh from the components tab on the left of the graph view and dragging it into the BluePrint graph as a GET operation node, then used the Change Material node to change the material, simple enough.
This works great for the first player, but if you create a new character instance in the game of type Player_BP and have it react to the 2nd-player change-colour input, it still changes the colour of the first instance.
I thought I was creating a class with member variables, and that making a new instance would run that script on the new instance.
This led me to investigate when variables are considered static, the document states that a variable is static unless:
- Exposed as editable
- Set to something using the SET node
This doesn't work as I expected, but I understand the reasoning. However, I didn't feel like exposing it as editable (making a new variable to do so?) or setting it manually using SET, so I manually got the second player character and modified the material like that.
(the first block shows how I first assumed it would work for all instances, the second block shows how I handled it afterwards to address each instance manually, using a for loop in my game, checking if the player is equal to NONE before trying to use it)
Level layout for two
Having finally got the colours of my players changing correctly I realised that I had one tiny flaw: I'm a moron and hadn't thought that they'd be sharing the same level, so when one player changed the colour of the platforms, it would change it for the second player too.
While that could be chaotic fun, I don't think it would actually work.
Dismayed and on the point of giving up, I thought that I could simply make a clone of the platforms somewhere off screen from player 1, and make it a simple race to the top, but I really wanted the players to be able to see their competition on the same set of platforms as they were on.
Then I realised: I truly am an idiot. Mirror it! Simply flip the camera around 180 degrees when creating the second player, create double-layered platforms and BAM!
The picture above shows how I really do have double platforms and players on different Z-planes, as well as enlarged collision-boxes around the players so that they can collide with each other despite not being on the same Z-plane.
I have several plans for reworking this so that it looks less like two separate levels stuck together.
Dealing with collisions
Disclaimer: I still don't really know what I'm doing with this. While I understand Box2D and Chipmunk collision groups and masks, this makes little sense to me.
So, I wanted to have the two players collide when they're the same colour, and pass through each other when opposing colours.
I guessed that I needed to change the group or mask or something of one of them when the user changed colours, but I couldn't find anything like a mask or group.
There are Channels however. After struggling for a while to to understand creating and using custom channels, I simply went with the following hack.
Setting up the collision presets in the details tab of my player class so that pawns collide with pawns, makes sense, right?:
Adjusting the collision channels on the second player in BluePrints to set the collision response to Ignore if the players are different colours: