Ok, so am going to assume that you know how to create a new project with the FPS template to start with. Am going to be using the 4.13.2 version of the engine, but i think this will be basic enough to try it in other recent versions.
Now lets setup the editor into multiplayer mode, this is achieve by opening the options of the Play Button on the top toolbar. Change the number of players at the bottom of the list to 2 and launch the game in a New Editor Window fashion.
You can also access the advanced settings at the bottom of the same list and change the size of the new players windows to your liking.
Each window will have the name of the instanced players, check that one is created for the listen server and the other for the client 1.
Form here we can notice some things happening, for once we can see the players move around the map and that they can see each other, but nothing else that we perform is noticed neither by the server or the client side.
So why is that? That is because default multiplayer mode on UE4 will show to every connected client all the actor first spawned by the server, and that means the FPS characters possessed by each client (client and server in this case) as well every box, walls, floor, sky, lights, etc.
The movement we see form each character is being Replicated because by default the movement of the FirstPersonCharacter blueprints are set to do so. You can check this by opening the FirstPersonCharacter blueprint and filter the details window with the word “Replication”.
This is crucial for the server to know that we want to inform other clients that the movement of this character is relevant to be replicated to other connecting clients. All Actors on the map have this checkbox, also the components inside but we will talk about components later.
So, whats up with the projectiles? Why can´t i see them spawning other that in the own player window? Well that should be easy now that we know about the Replicates checkbox.
Open the FirstPersonProjectile blueprint and check the Replicates on and fire up the game again.
Something very strange happens, the client can see the projectiles form the server side as well as the impulse generated on the boxes, but the server cant see the projectiles from the client, nor any interaction of the client projectiles on the boxes. This is working as spected, as the server can spawn new actor in game, and as it is the server those actors interactions will be replicated to other clients. But now the client is not informing that he is performing an action like spawning projectiles.
Even when the client FirstPersonBlueprint was spawned by the server, new actors spawned form this blueprint are not necessarily replicated by default. The blueprint acts only on its realm unless we use a Remote Procedure call.
And that is what we are going to do next.
First we need to separate what is going to be needed to be spawned by the server and what is relevant to be shared to other clients. We need for sure to let the server do the spawn of the projectile because only the server can spawn actors relevant to connected clients. Then we need to inform other players of the sound and animations generated by that firing to other players around.
Lets first spawn the projectile.
When i talk about a server spawning doesn’t means that we need another server side blueprint, nor communication between different blueprints. You will see that being a server is not an entity, nor a place or process alone, is a authority thing. There will be places in our blueprint that will have the authority to act like a server while the rest will just act as a single player blueprint.
Lets see this graphically.
Open the FirstPersonCharacter blueprint and look for the “Spawn projectile” section on the event graph. Set aside the animation montage and the play sound from the actual spawning, if you will you can strip the vr and mobile support nodes and give yourselves some work space.
Ok, we need to first add a custom event, name it RPC Fire, and set in its properties to replicate “Run on Server” and check the Reliable box.
Now connect it to the SpawnActorFirstPersonProjectile, and summon that custom event from the InputActionFire.
Play the game now and you will see the client firing projectiles on the server side window.