× Phaseshift FCOM Tutorials

Create a party using Netcode for GameObjects


Create a party system to connect your players to a session using Netcode for GameObjects, Lobby and Relay.

This tutorial assumes you have already setup Netcode for GameObjects, Multiplayer Play Mode, Lobby and Relay. We're going to be doing a client-host, peer-to-peer network. One client is the 'host'.

To properly debug, it's a good idea to open up two additional players using Multiplayer Play Mode and have a total of 3 players. This way you can test client to client connections too.

Authenticate with Unity Services


First, you need to ensure each client that is going to connect to Relay is authenticated. This allows secure connections peer to peer. Create a new MonoBehaviour script and let's call it 'PartyNetworkManager'. We need this to be a singleton and to not be destroyed on scene load with DontDestroyOnLoad. All the following code can be placed within this class, but you may choose to separate it to better distinguish between which code does what.

We can initialise this like so:

When Awake is called, it sets the static variable as itself and DontDestroyOnLoad(this) ensure it doesn't get lost between scene transitions. This class will also NOT need to be synced across the network, so MonoBehaviour for this class is fine. 

We can then authenticate the local player with Unity Services, we do this so we can use Relay and Lobby. This can be called at the Awake() stage, but you may choose to have it called whenever a player presses the 'Multiplayer' button on a main menu.

Create a Relay Allocation


To set Relay up with NetworkManager we need to create a Relay Allocation. You may want to call this when a player clicks a 'Create Party' button or similar. The following code creates a Relay allocation, and also assigns a join code that can be used by prospective clients when connecting to this party.

Hosting a Party


When an Allocation is created, we then create a Lobby with the joinCode from Relay. After which we start a hosted network session with NetworkManager.

You'll notice we can change lots of settings here such as connection type, party privacy etc. We can also use these parameters to add things like a password so when we Approve a Client wanting to connect, we can check if there's a match and allow them in.

After this code, you can now host a party! But how do we connect to it?

Connecting Clients to the Party


To connect clients to our party we need to first join to Relay using our relay join code. In our example we've used a simple string which is "joinCode", but this could be whatever you like.

The following code could be linked to a button called 'Quick Join Party' or similar.

You should now have your client connected to your host through Relay and using Lobby parties!

Documentation


To explore more on these topics, check out the documentation:

Netcode for GameObjects documentation

Liked this article?

Please consider sharing!
Author

Josh Lyell

Game Developer