Setting up the base networking code
In the A crash course in computer networking section, we saw that we wanted to set up a client-server network architecture and that we could use IP addresses and ports to find computers over the internet. In this section, we’ll start implementing this.
We’re going to make our multiplayer game work like this: every time you start playing, it spins up a server in the background. This way, anyone can join after one person starts the match.
Creating the client-server connection
If we want to connect our players through a client-server model, we need to be able to set up one computer as a server and the others as clients that connect to this server. Let’s start by writing some code.
- In the
menu.gd
script, add a constant at the top that indicates which port we want to use:const PORT: int = 7890
- Now add these two functions to the bottom of the script:
func host_game(): var peer = ENetMultiplayerPeer...