UE4 Server Browser (C++)

In my previous post, I went over my implementation of a LAN server browser in UE4 using blueprints.  After completing the blueprint version, I decided to go back and attempt to write it over in C++. Additionally, I’ve made the functions BlueprintCallable to allow integration into different level blueprints and widgets.

Setup:

Take a look at my BP post, and be sure to modify DefaultEngine.ini and ProjectName.build.cs to support an online subsystem.  The online subsystem serves as an abstraction of the online functionality available in all of the different platforms: it’s a way of making online development for Xbox, Steam, Facebook, etc… as seamless as possible.

Each function for online session handling follows a basic formula:

  1. Get the current online subsystem
  2. If it’s valid, get one of UE4’s session objects to handle the request
  3. Have an event called on success/failure

 

Hosting a session:

 

After the online subsystem is obtained, the session interface is grabbed from the subsystem.  From there, it’s just filling out the type of lobby to be hosted (max players, LAN, private connections, etc…) and calling Create Session with the session interface.

On session creation, be sure to clear out the delegate, and if everything is successful, then open the next level and set it to listen (this allows the server to listen for incoming connections):

 

Finding a session:

Starting off, just like hosting, be sure to get the online subsystem and session interface.  From there, fill out an FOnlineSessionSearch object:

When the session find is complete, return mSessionSearch->SearchResults to get the session result structures, which can be used for creating widgets for server entries.

 

Joining a session:

When passed the name of the session and the search result, all that needs to be called is Sessions->JoinSession().  When complete, after clearing the delegate, all that needs to be done is actually migrating to the host’s game:

 

Destroying a session:

To quit, simply call Sessions->DestroySession, using GameSessionName, which is stored in OnlineSubsystemTypes.  When done, just navigate back to the main menu:

 

These tricks cover the basics to creating, finding, joining, and quitting, and are simply an alternative to using the nodes that accomplish this in blueprints.

Leave a comment