The Session System in Unreal Engine 4 gives you access to many powerful online features such as the server browser, setting a maximum player limits, setting private and public slots and much more.
Using this system is very straightforward and only takes a few blueprint nodes to get working.
In this guide I will show you how to setup multiplayer in Unreal Engine 4 using the session system. We will setup a simple session and connect to it through another player.
Online and LAN Support
Unreal Engine’s default online subsystem can not find sessions outside of the local network the player is in.
To find and connect to sessions outside of your local network without a direct IP address connection you will need to use a different subsystem such as Steam.
Click here to read the documentation for the Steam subsystem
Starting a Multiplayer Session
Creating the Session
To start I created a simple widget with two buttons. One is host and one is play.
On the host button event use the create session blueprint node.
On the player controller pin, connect a “Get Player Controller” node and for the public connections pin set it to the maximum amount of players you want for this game.
Public connections is the maximum amount of players that can be connected to your server at once.
If “Use Lan” is checked the session you create will be only available on your local internet.
Opening Levels to Multiplayer
To create a multiplayer game using your level, add a new “Open Level” node and set the “Level Name” pin to the level you wish to play on. In my case this is FirstPersonExampleMap.
The important step is to click the triangle at the bottom of this node to expose the extra settings.
The “Absolute” checkbox and the “Options” text box pins are now visible. In the Options pin type exactly:
This code sets the level up to listen for multiplayer connections as a Listen Server.
With these steps complete, the code should look like this.
Connecting to your Multiplayer Session
Finding sessions using blueprints is simple. The Find Sessions node handles all this for us. It outputs different execution pins based on if it succeeded or failed and the results that it found as an array.
If you aren’t familiar with arrays click here to learn about arrays in Unreal Engine 4.
To start I created the Find Sessions node attached to the Play button in my widget.
On the player controller pin connect a “Get Player Controller” node and the Max Results pin is set to 100.
The Use Lan checkbox, if checked, will only search for sessions in your local area.
Joining the Session
To join a session in Unreal Engine 4, create a Join Session node.
This node requires the player’s controller and a session from the Find Sessions array in the previous step.
Firstly I will create a Get Player Controller node and attach it to the Join Session node.
From the Results output pin of the Find Sessions node in the previous step I created a Get node. This will find the first session in the list.
This is then attached to the Search Results input pin of the Join Session node.
Finally, the Join Session node is attached to the On Success execution pin of the Find Sessions node.
Opening the Game Clients
In Unreal Engine 4, there is a drop down menu next to the play button which gives you access to multiplayer options.
On this menu make sure “New Editor Window (PIE)” is selected and the “Number of Players” slider is set to anything above 1.
Next, click the Advanced Settings button at the bottom of the menu.
Near the bottom of the Advanced Settings menu, find the a section called Multiplayer Options.
Un-tick the checkbox “Auto Connect to Server”. This prevents the server auto connecting without the session system.
Finally, when you press the play button in your editor two clients will open.
The custom widget discussed earlier, displays the Host button and Play buttons.
On the first client I pressed the Host button. This reloaded the level.
On the second client I pressed the Play button and waited until the Find Sessions node had finished searching for multiplayer sessions.
Once this finished it auto connected me into the first clients game.
Connecting to Sessions Outside your Local Internet Network
The session created in this guide will can be used in your local network and through the internet.
If you connecting to a session in your local internet, you do not have to do anything else.
If you wish to host your session to global internet users, make sure to port forward UDP/TCP 7777. Setting up port forwarding is different per router so check your specific router instructions.
Download the Project Files
Your multiplayer session is created and players can join!
This guide only brushed over the multiplayer system to start with sessions in Unreal Engine 4.
To learn more about replication and the session system, the Unreal Engine documentation as it is a good place to start.
Expanding the System
To challenge yourself, try create a widget that fills itself with the results of the Find Session nodes.
This, if done correctly, will create a server browser similar to the ones found in games such as Team Fortress 2, Counter Strike and many more.