How i made a simple MULTIPLAYER server for my game!


1. The server itself:

To make a multiplayer game you need a way to send data to every player. There are a few options out there to manage this. You could make something like a end to end connection maybe with techniques like WebRTC. Or like i have done, use a real server. 
So the problem is simple. There is a number of X players on the game. They all need to get the X and Y coordinates from the other players continuously (if it is a 3d game than you need things like the Z and the rotation as well).
So the main Idea is to manage a way every player can send his coordinates to a server and receive a list with all coordinates from all other players.
To get this to work we need a server to connect to. I rent a root server by the European company "hetzner". You can choose what every you want. But keep in mind that you need more server power if your game gets viral. So maybe some cloud options would be an idea.
It does not matter which system you prefer. What matters, you need something like a web server running on it or some service which can manage the communication. In my case i have a debian system running on the server. And installed "Node" for our game service.
Side tip: If you have a root server now. You can host your game or company page as well on your server. Just follow a tutorial how to host a own website on a server.

2. The algorithm: 

It does not matter if you made your game in Unity, Godot, Unreal or with your own engine. The only thing that matters is, that you learn how to open a web socket connection and listen on it. Just google it for the engine of your choice.
Since my Game was made with JS it was easy i can just sign in to a specific web socket and send data and listen to incoming data.
So the idea is the following: On server side we open a web socket. This is basically a gate where everyone else can connect to.

var credentials = { key: privateKey, cert: certificate };
var wsshttpsServer = https.createServer(credentials);
wsshttpsServer.listen(1337);
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
    server: wsshttpsServer
});

Now we need a event we can listen to, so players can connect and send their positions.
So lets name this event "position". In the event we need a player identification (a unique id) and X and Y number. So we need a data structure like this {id: '193382', X: 10, Y: 40}. So if a player sends the event "position" with the data {id: '193382', X: 10, Y: 40} the server notice this. Now we can create a algorithm what we want to do if this event appears. 
We need a way to manage all players. So we create something like a player array. If we get a position event we have to look if a player with the id in the event is already in our array. If the player is already in the array we just have to update the X and Y values. If the player is not in our array we can create a new item in the array with this player data.
Now we have a array or list of all players connected to the web socket and the X and Y positions of them. That is great the server knows everything what we need. Now there are two options: send the the full array/list of player data ever X milliseconds to all clients on the web socket. Or make a event which answers with the list. We choose the first way since it is simple if we don't have any kind of lobby system. And the data tick is server side which is better to avoid problems.

3. Inside the engine (User/Client side): 

The rest is very simple but depends on your engine. I explain it very easy so you can adapt it to your engine. On User side you need a script which connects you to the web socket and sends your position every X milliseconds.

function sendData() {
 let data = { messageType: 'position',
              playerId: playerId, 
              x: x, 
              y: y
 };
 webSocket.send(JSON.stringify(data));
}

Since you are logged in to the web socket you receive the player list with all other players and positions. Know you have to simple iterate through the list an create a obstacle in your game for each player. Be sure you only do this once for each id. And know update the X and Y values of this obstacles with the values you receive every tick.
That's it.  So in reality ever player is on his own world but has elements in his world with the positions of the other online players.

4. Don't forget to remove: 

Since it is possible that players can lose their connection or go offline, you need a way to detect this and remove the player from the list. So you don't have offline players with their last position still on the game. To do so you have several options. The web socket normally informs you about a connection loss. So you can just listen to that. Or you ad a timestamp to the position data structure, so you always know when you received the last position from a user, and kick him out of the list if a time X reached. 

5. Whats next?: 

That was of course not the end. But know you can have a lot of fun with this small multiplayer server. Normally a web socket is limited by 15.000 connections so if you go viral you need some server management. Also you maybe need more interactions than just the positions. But since you know how you send position data, everything else is very easy to build on top. Go on and build something cool.
If you need some code example just take a look at my game "Versteckt" the whole code is on Github. You can copy it like you want (the code not the art). But please give me some credit if i helped you:) Just a link on your project would be great.
In my game code you can also take a look how i implemented a lobby system and a "Invite with code" system like among us or phasmophobia.

https://github.com/curious-lynx-studio/Versteckt

Files

versteckt-the-game-0.1.2.AppImage 94 MB
May 11, 2021
versteckt-the-game 0.1.2.exe 66 MB
May 11, 2021

Leave a comment

Log in with itch.io to leave a comment.