This script is our Autoload. It runs constantly in the background, managing the invisible network connections and keeping track of who is in the game.
Right-click in FileManager create a new script
Save it as NetworkManager.gd
Go to Project => Project Settings => Globals
The port is the specific address we use to send and receive data
We use the LocalHost IP for the server which tells the game to look for a server on the exact same computer it is running on.
Max connections caps the lobby at 4 players
peer is the engine's built-in network manager. It does all the heavy lifting for sending data over the internet.
players is a dictionary that acts as our guestbook. It will link a player's unique network ID to their custom username.
local username is a sticky note to temporarily hold our name while we wait for the server to let us in.
The connection timer is a countdown clock we use to cancel connections that take too long.
These are custom megaphones. When something important happens on the network, this script shouts it out so the Lobby UI knows to update the screen.
In the _ready function we are wiring Godot's built-in network events to our own custom functions.
We also create and set up a timer so that we can trigger a timeout message if a connection fails.
Host opens the port and listens for players
Tell Godot to use this peer for all traffic
Host is always ID 1, add ourselves to the player list
Emit the signal to update the list
When joining this peer tries to connect to the host IP
If the IP is valid we tell Godot we are a peer and start the 5 second timer for joining
Otherwise we run the connection failed function
Display a message when a player connects successfully
If someone leaves, erase them from the players dictionary.
If we are the server, we immediately tell everyone else to update their lists too.
This ONLY runs on the client, the exact second the handshake succeeds.
Tell the host (ID: 1) our username
Signal the lobby UI that we connected
What is a Remote Procedure Call? (rpc)
A Remote Procedure Call (RPC) is a communication protocol that allows one program to request a service from another program located on a different computer network, without needing to understand the underlying network details. It functions exactly like a local function call, but operates over the network.
@rpc: This tag tells Godot, "Anyone is allowed to call this function, and make sure it arrives in perfect order."
The server receives this function from a new player, adds them to the dictionary, and then calls sync_player_list.rpc(players) to broadcast the newly updated list to everybody.
This tag means "ONLY the server is allowed to trigger this."
Overwrites the client's old player dictionary with the master copy sent by the server, ensuring everyone is looking at the same data.
The host uses this function to force everyone to change their scene to the Arena at the exact same time.
Signal lobby that connection failed
Connected to the timeout signal of the connection_timer.
Runs exclusively when the full five seconds have passed without a successful server handshake.