**This is an old revision of the document!**
Table of Contents
Peer-to-peer
GDevelop supports peer-to-peer (P2P) connections to enable basic multiplayer games. This works through the concept of remote events. Connect multiple instances of the game using their ID, before remotely triggering conditions on the other instances.
Selecting a broker server
Before being able to send data, a running game, called a “client”, must connect to other clients. For this, it needs a way to self identify and find other clients. To do so we need a broker server. It's a server with a fixed, well-known address that stores all the adresses of the connected clients and give them to the clients, so that they can connect to each other. You have two options here:
* Setting up your own server (recommended), which you can run on your computer as a test.
* Using a default, public server.
Set up your own (local) server
You can set up your own server easily. You will need to install Node.js. The LTS version is recommended.
Open a command line. To do so on Windows, you can press the Windows and r keys, then type cmd
in the popup, and enter.
In the command line, type the first time only (to install the server): npm install peer -g
Then, whenever you want to start the server, just type in a command line peerjs --port <the port>
.
You can use any port that is not already used by your machine.
You can pass other options to the PeerJS server, see its website for more information.
You can then use “Use custom broker server” action to connect to your server. You can enter localhost
as the host to point directly to your own address.
Use the default server
You can also use the default server provided by PeerJS.
To use that server use the action “Use the default server”.
Connecting
To connect instances, you need to enter their ID in the other instances. The ID can be found with the expression P2P::GetID()
. To connect, use the “Connect to other instance” action and pass as parameter the ID of another instance. Both instances will then connect automatically. You can then send an event from one instance to the other one to make sure that the connection is established.
Interacting with connected games
Once you got connected, you can trigger actions remotely. You can select another specific game instance (using its id) or send an event to all connected instances.
Choosing if you want to activate data loss mode
You might be wondering what the “data loss” parameter is for. Due to how GDevelop is made, only one occurence of a remote event can be handled when the the event sheet is executed (this happens roughly 60 times per second). To help optimize events execution, we provide the choice to use the dataloss mode.
- With the no dataloss mode, every remote event is queued, and on every frame if there is one in the queue, we take the oldest one and handle it. This makes sure every data is processed/taken into account.
- With the dataloss mode activated, it doesn't queue the data but only store the latest occurence of the remote event. This means only the latest data is processed and outdated data will be discarded.
Here are two examples:
- if you use a synchronised score counter, you don't want to lose any data, as missing only one point of the counter would desynchronise the counters, so the dataloss mode would be deactivated.
- If you want to synchronise positions, only the last position sent is relevant, not older positions. In this canse, you would activate the dataloss mode to prevent delays/lags.