Create Session

Epic Online Services (EOS) gives players the ability to host online gaming sessions through the Sessions Interface.

This is implemented using Sessions Interface.

If you just want to create session and not need extra info, just check the shared Blueprint Code.

Overall Idea for Beginners

To create a session in Unreal Engine using Epic Online Services, you'll need to:

  1. Initialize the Epic Online Services SDK in your game.

  2. Create a session configuration, specifying the session parameters like the maximum number of players, session name, and session type.

  3. Create a session, passing in the session configuration and a callback function that will be called when the session is created.

  4. Host the session, which will allow other players to join and play the game.

Once you have created the session and hosted it, you can use Epic Online Services to manage player connections, matchmake players, and handle matchmaking errors.

Blueprint Code with Explanation

You can first get a reference of the subsystem like follows ->

The reference of EIK_Subsystem can be taken anywhere, like in Game Instance, Widgets or maybe Character Blueprint.

Here, as in the code, you first need to get a reference of the EIK_Subsystem and then call Create EOS Session. You now need to bind a callback which you can make by dragging from result and selecting Custom Event.

Is Dedicated Server Boolean - Set this to TRUE if you are creating a session from a dedicated server or else, for Listen Servers, set this to FALSE.

Is Lan Boolean - Set this to TRUE will make the game un-noticeable on the EOS DevPortal website. Expected to be false.

Number Of Public Connection Integer - Number of players who can join the session through matchmaking. Invites are not managed by this number.

Region Enum - This allows to select regions(one of it's kind matchmaking). If you make it No Selection, it will appear it in all cases normally.

Custom Settings TMAP - You may set custom settings like game mode etc like this -:

Result Callback

On Result Callback, you can check SessionName and also if create session was Success. You can also ServerTravel if login was Success.

C++ Code with Explanation

EIK_GameInstance.cpp
void UEIK_Subsystem::CreateEOSSession(const FBP_CreateSession_Callback& Result, TMap<FString, FSessionSettingsStruct> Custom_Settings, FString SessionName, 
                       bool bIsDedicatedServer, 
                       bool bIsLan, 
                       int32 NumberOfPublicConnections, 
                       ERegionInfo Region)
{
   CreateSession_CallbackBP = Result;
   IOnlineSubsystem *SubsystemRef = Online::GetSubsystem(this->GetWorld());
   if(SubsystemRef)
   {
      IOnlineSessionPtr SessionPtrRef = SubsystemRef->GetSessionInterface();
      if(SessionPtrRef)
      {
         FOnlineSessionSettings SessionCreationInfo;
         SessionCreationInfo.bIsDedicated = bIsDedicatedServer;
         SessionCreationInfo.bAllowInvites = true;
         SessionCreationInfo.bIsLANMatch = bIsLan;
         SessionCreationInfo.NumPublicConnections = NumberOfPublicConnections;
         SessionCreationInfo.bUseLobbiesIfAvailable = false;
         SessionCreationInfo.bUseLobbiesVoiceChatIfAvailable = false;
         SessionCreationInfo.bUsesPresence =true;
         SessionCreationInfo.bAllowJoinViaPresence = true;
         SessionCreationInfo.bAllowJoinViaPresenceFriendsOnly = false;
         SessionCreationInfo.bShouldAdvertise = true;
         SessionCreationInfo.bAllowJoinInProgress = true;
         
         SessionCreationInfo.Settings.Add( FName(TEXT("REGIONINFO")), FOnlineSessionSetting(UEnum::GetValueAsString(Region), EOnlineDataAdvertisementType::ViaOnlineService));
         if(bIsDedicatedServer)
         {
            SessionCreationInfo.Settings.Add( FName(TEXT("PortInfo")), FOnlineSessionSetting(GetWorld()->URL.Port, EOnlineDataAdvertisementType::ViaOnlineService));
         }
         SessionCreationInfo.Set(SEARCH_KEYWORDS, FString(SessionName), EOnlineDataAdvertisementType::ViaOnlineService);
         for (auto& Settings_SingleValue : Custom_Settings)
         {
            if (Settings_SingleValue.Key.Len() == 0)
            {
               continue;
            }

            FOnlineSessionSetting Setting;
            Setting.AdvertisementType = EOnlineDataAdvertisementType::ViaOnlineService;
            Setting.Data.SetValue(Settings_SingleValue.Value.Value);
            SessionCreationInfo.Set(FName(*Settings_SingleValue.Key), Setting);
         }
         SessionPtrRef->OnCreateSessionCompleteDelegates.AddUObject(this, &UEIK_Subsystem::OnCreateSessionCompleted);
         SessionPtrRef->CreateSession(0,*SessionName,SessionCreationInfo);
      }
   }
}

Explanation

  1. The function takes in several parameters, including a callback function, custom settings, session name, and Boolean values for whether the server is dedicated or LAN.

  2. The function gets a reference to the online subsystem using the "Online::GetSubsystem" function.

  3. If the online subsystem is valid, the function gets a reference to the session interface using "SubsystemRef->GetSessionInterface()".

  4. The function creates an "FOnlineSessionSettings" object, which holds various settings for the session. These settings include whether the session is dedicated or LAN, the number of public connections, and whether players can join via presence.

  5. The function adds custom settings to the session using the "SessionCreationInfo.Settings.Add" function.

  6. The function sets up a delegate function to be called when the session is created using "SessionPtrRef->OnCreateSessionCompleteDelegates.AddUObject".

  7. The function creates the session using "SessionPtrRef->CreateSession".

  8. Once the session is created, the delegate function "OnCreateSessionCompleted" will be called.

Thank you!

Last updated