Multiplayer - EasterEggProductions/adventure-mode-godot GitHub Wiki
placeholder
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.
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:
- Discovers any local network UPNP-capable devices using upnp.discover()
- Retrieves and validates the network gateway via upnp.get_gateway()
- Requests external port mappings for both TCP and UDP protocols using upnp.add_port_mapping()
Note: Some routers do not support or have UPNP enabled by default. Many public networks, mobile hotspots, and other networks may block UPNP.
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.
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.