Multiplayer - EasterEggProductions/adventure-mode-godot GitHub Wiki

Table of Contents

Overview

placeholder

Join Codes

Join Codes are a feature whereby the host can share a code that other players can use to join their game quickly. It is designed to be short and somewhat memorable, ideal for sharing in a chatroom or spoken aloud in a voice call.

Join Codes are NOT unique or secure! The player's public IP & port (fetched from UPNP) is encoded directly into the join code so there is no reliance on a centralized service to federate join codes.

To keep the length short (8 characters) the port of the connection encoded in the join code is truncated to 0-99, and the 339 prefix of the port is fixed.

UPNP

When hosting a multiplayer session the game uses UPNP (Universal Plug and Play), to automatically configure port forwarding on a selected port within the range 33900-33999. This is done by temporarily attempting to bind an ENetMultiplayerPeer server to each port until a free port is found. This enables other players to join the hosts session without requiring manual router configuration, bypassing common NAT restrictions on consumer routers making the hosts game reachable as long as the host session is valid.

Once a valid port within range is selected the application:

UPNP methods are handled via Godot's built-in UPNP class and only run once the "Host" menu option is selected in the main menu, a separate thread is created as to not block the main game loop while setup for UPNP operations are being completed. If UPNP setup fails it currently is logged in stdout and hosting continues allowing LAN connections. A currently unused method _exit_tree() exists to remove any created UDP or TCP mappings and cleanly shuts down the UPNP thread if active.

Note: Some routers do not support or have UPNP enabled by default. Many public networks, mobile hotspots, and other networks may block UPNP.

Automatic Port Scanning & Selection

When a player is hosting a multiplayer session, Adventure Mode now automatically selects an available port instead of relying on a single hard-coded value. Previously, hosting would often fail if the static port was already in use resulting in having unreliable session creation.

To address this, the game scans a predefined port range 33900-33999, when the Host option is selected. For each port in the range, the system attempts to bind to an ENetMultiplayerPeer server temporarily. If the bind succeeds, the port is confirmed as available and is immediately released. The first available port found becomes the hosting port for the session.

If no ports are available within the range, the hosting fails gracefully and returns the player to the server menu and no multiplayer session is created.

This approach improves reliability by ensuring the server only starts on a valid and unused port. It also integrates cleanly with other session features by guaranteeing that a usable port is selected before any additional networking setup occurs.

Steam & ENet

The MultiplayerManager.gd script includes handling for switching between the ENet and Steam network API. By default, Godot's high-level multiplayer has an existing implementation for the ENet API, but also allows plugins for systems outside of what Godot offers. The "network mode" is set as an enum (named network_mode) within the MultiplayerManager.gd script. Users can navigate to this variable and switch between "Steam" and "ENet". Host and client handling later check the enum and instantiate the MultiplayerPeer object accordingly.

ENet

Steam

⚠️ **GitHub.com Fallback** ⚠️