Client Server Model UE5
The main things that multiplayer is all about is Input and State.
- State is everything in the world, velocity, positions, etc…
- In the state we add inputs which is telling the player to move or perform an action.
- During the tick you combine the current state with the current actions and the result is the 2nd state.
- This leads to what the player sees in happening in the game
- Rinse and Repeat
For multiplayer the players state must be updating together.
- One way to do this is Peer-to-Peer
- If you make a change to the input from one system you make a state change to every player. You must listen to everyone elses input before you do the next Tick as well.
- Peer-to-Peer is one of the least reliable methods for multiplayer
- A more reliable version is to go with the Client-Server model.
- Where a computer recieves input from the other devices and send the state updates to the clients.
Let’s try to do this ourselves. Open up a command prompt window. The goal is to launch a game from the command line.
- Get the UnrealEditor.Exe from your Unreal Version file normally in your programs folder, and paste it in the command prompt.
- Do the same for the Project you created as well.
- To make sure if launches a game place a -game at the end.
This is great for testing especially if there is a bug that keeps crashing the game.
You can also put -log at the end to get more infomration about what’s happening in the game.
If you want to load a specific map you can pick it by exe gamelocation /GAME/ThirdPersonCPP/Maps/ThirdPersonExampleMap — Game
You could also put in an IP address instead of a map. This is the way to connect to a server. So we will swap out -game for -server
Your server will now launch. You can verify this as the log window will launch as well.
Now we can connect a client.
- Instead of a map we will put in your IP address and server with game.
- If when you launch and are getting a black screen there might be a problem where you aren’t on the correct port. The port might have to be specified.
- On the server log do a ctrl+f and search for listening. This will show what port the server is listening on.
The game will now launch.
- Go back to the command prompt and launch a second instance. Now both will be connected to the server.
Congratulations! First steps of local multiplayer via client-server have been achieved!