How to Make GDevelop's Built-in Multiplayer as Stable as Possible [Guide]

- General Practices:

Problem 1: After playing for a while, the frame rate drops significantly, especially on low-end devices.
Cause: This is due to the fact that multiplayer puts a significant load on the network and device, especially if the player is the host.
Solutions:
• Avoid synchronizing large amounts of data; keep only the essentials.
• Disable synchronization of global and scene variables.
image
• Replace scene timers with an object timer that doesn’t have multiplayer behavior, or a variable to which you’ll add TimeDelta() every frame.


image
• Please note that some extensions also contain variables and timers whose synchronization should be disabled, as described above.
image
• Reduce the synchronization rate, for example to 10; the game will still run smoothly if you use behaviors to change the positions of multiplayer objects.
image

Problem 2: Sometimes there are more characters on scene than expected, for example, the maximum number of players is 4, but there are 8 characters on scene.
Cause: Sometimes the engine cannot correctly detect when a player leaves the lobby; this only happens on the host side.
Solution:
• On the host side, track the number of characters owned by players and remove any excess characters. In the event below, only the first character created will be removed, as that is the one causing the problem.

Problem 3: Sometimes your character is deleted (even if you’re the host).
Cause: Sometimes the engine might think you’ve left the game and delete your character.
Solution:
• Add a check to see if the character you own is already in the scene. If not, create a new one.

- Connection issues:

Problem 1: “Unable to connect to other players.”
Cause: Sometimes a player’s device loses connection to the broker server.
Solution:
• Connect players to the broker server manually using the code: gdjs.multiplayerPeerJsHelper.useDefaultBrokerServer();. Then check if the player is connected using the code: gdjs.multiplayerPeerJsHelper.isReady(). Only then allow them to connect to the lobby.

Problem 2: Infinite connection screen.
Cause: There are several possible causes, such as the host minimizing the tab, an unstable internet connection, etc. These are all consequences of using p2p, which is what the built-in multiplayer uses.
Solutions:
• Add a connection timeout using a timer, force a restart of the game using the code: location.reload(); or terminate the connection to the lobby if this is possible in your game. You can also add error handling so the player understands what went wrong.


• Force disconnect players from multiplayer if they minimize the tab for more than 30 seconds, for example. Use the code in the screenshot. It should only be called once per game. This can be done using “on first scene loaded.”

- ICE candidate TURN/STUN servers:

The main source of multiplayer issues arises from the use of peer-to-peer (P2P). Not all providers and network configurations allow direct connections between devices over the internet. This is why we need TURN/STUN servers to facilitate connections between players and, if a connection is impossible, to connect them through an intermediary server. Although the built-in multiplayer uses its own TURN/STUN servers, their quality leaves much to be desired.

You can add your own ICE candidates (TURN/STUN servers) using the code: gdjs.multiplayerPeerJsHelper.useCustomICECandidate(url, username, password); Each new line is a new candidate; be sure to call it before connecting to the broker server.

I recommend using Cloudflare Turn, which offers 1 TB of free data per month, but it can be complicated since you have to create a function on your own server to exchange authentication data. Metered is similar, offering 500 MB to 20 GB of free data per month, but without the need for manual authentication data exchange.

Even using TURN/STUN servers doesn’t guarantee a 100% stable connection and multiplayer gaming experience; you still need to use everything listed above for the best possible results.

- Other tips:

• You can create your own custom lobbies using my extension and a custom object found in the GDevelop Asset Store GDevelop Custom Lobbies.
• You can avoid the GDevelop login window using an extension developed by arthuro555 Auto Guest Extension.
• Almost everything listed in this guide is available in my Chess Template, including a ready-made extension that combines all the JS code above.
• All of these issues were discovered and resolved during the development of my game, Powerline Guardians. You can see the results of all these improvements and fixes by playing it on Crazy Games or Google Play.

6 Likes