Just0Abhi

EosHandler.cpp

Sep 16th, 2025 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.85 KB | Gaming | 0 0
  1. //
  2.  
  3.  
  4. #include "EOS_OSS_Tutorial/Public/EosHandler.h"
  5. #include "OnlineSessionSettings.h"
  6. #include "OnlineSubsystem.h"
  7. #include "OnlineSubsystemUtils.h"
  8. #include "Online/OnlineSessionNames.h"
  9.  
  10. void UEosHandler::Login() const
  11. {
  12.     Subsystem = Online::GetSubsystem(GetWorld());
  13.     if(Subsystem == nullptr)
  14.     {
  15.         PrintToScreen("No online subsystem found!");
  16.         return;
  17.     }
  18.     const FString Message = FString::Printf(TEXT("Online subsystem found Name is %ls"), *Subsystem->GetSubsystemName().ToString());
  19.     PrintToScreen(Message);
  20.  
  21.     Identity = Subsystem->GetIdentityInterface();
  22.     if(!Identity.IsValid())
  23.     {
  24.         PrintToScreen("No identity found!");
  25.         return;
  26.     }
  27.  
  28.     if(const FUniqueNetIdPtr NetId = Identity.Pin()->GetUniquePlayerId(0))
  29.         if(Identity.Pin()->GetLoginStatus(0) == ELoginStatus::LoggedIn)
  30.             return;
  31.     LoginDelegateHandle = Identity.Pin()->AddOnLoginCompleteDelegate_Handle(0, FOnLoginCompleteDelegate::CreateUObject(this, &ThisClass::HandleLoginCompleted));
  32.  
  33.     FOnlineAccountCredentials Credentials("AccountPortal","", "");
  34.  
  35.     PrintToScreen("Logging into EOS...");
  36.  
  37.     if(!Identity.Pin()->Login(0, Credentials))
  38.     {
  39.         PrintToScreen("Failed to login... ");
  40.  
  41.         // Clear our handle and reset the delegate.
  42.         Identity.Pin()->ClearOnLoginCompleteDelegate_Handle(0, LoginDelegateHandle);
  43.         LoginDelegateHandle.Reset();
  44.     }
  45. }
  46.  
  47. void UEosHandler::HandleLoginCompleted(const int32 LocalUserNum, const bool bWasSuccessful, const FUniqueNetId& UserId, const FString& Error)
  48. {
  49.     if (bWasSuccessful)
  50.     {
  51.         PrintToScreen("Login callback completed!");
  52.     }
  53.     else //Login failed
  54.     {
  55.         // If your game is online only, you may want to return an error to the user and return to a menu that uses a different GameMode/PlayerController.
  56.  
  57.         PrintToScreen("EOS login failed.");
  58.     }
  59.  
  60.     // Clear our handle and reset the delegate.
  61.     Identity.Pin()->ClearOnLoginCompleteDelegate_Handle(LocalUserNum, LoginDelegateHandle);
  62.     LoginDelegateHandle.Reset();
  63. }
  64.  
  65. void UEosHandler::FindSessions(const FName SearchKey, const FString& SearchValue)
  66. {
  67.     if(Subsystem == nullptr)
  68.     {
  69.         PrintToScreen("No online subsystem found!");
  70.         return;
  71.     }
  72.  
  73.     Session = Subsystem->GetSessionInterface();
  74.     if(!Session.IsValid())
  75.     {
  76.         PrintToScreen("No Session found!");
  77.         return;
  78.     }
  79.  
  80.     TSharedRef<FOnlineSessionSearch> Search = MakeShared<FOnlineSessionSearch>();
  81.  
  82.     // Remove the default search parameters that FOnlineSessionSearch sets up.
  83.     Search->QuerySettings.SearchParams.Empty();
  84.  
  85.     Search->QuerySettings.Set(SearchKey, SearchValue, EOnlineComparisonOp::Equals); // Seach using our Key/Value pair
  86.     Search->QuerySettings.Set(SEARCH_LOBBIES, true, EOnlineComparisonOp::Equals);
  87.  
  88.     FindSessionsDelegateHandle = Session.Pin()->AddOnFindSessionsCompleteDelegate_Handle(
  89.         FOnFindSessionsCompleteDelegate::CreateUObject(this, &ThisClass::HandleFindSessionsCompleted, Search));
  90.  
  91.     PrintToScreen("Finding lobby.");
  92.  
  93.     if (!Session.Pin()->FindSessions(0, Search))
  94.     {
  95.         PrintToScreen("Finding lobby failed.");
  96.         // Clear our handle and reset the delegate.
  97.         Session.Pin()->ClearOnFindSessionsCompleteDelegate_Handle(FindSessionsDelegateHandle);
  98.         FindSessionsDelegateHandle.Reset();
  99.     }
  100. }
  101.  
  102. // ReSharper disable once CppPassValueParameterByConstReference
  103. void UEosHandler::HandleFindSessionsCompleted(const bool bWasSuccessful, TSharedRef<FOnlineSessionSearch> Search)
  104. {
  105.      if (bWasSuccessful)
  106.     {
  107.         // added code here to not run into issues when searching for sessions is successful, but the number of sessions is 0
  108.         if (Search->SearchResults.Num() == 0)
  109.         {
  110.             // CreateLobby();
  111.             return;
  112.         }
  113.         PrintToScreen("Found lobby.");
  114.         for(auto SessionInSearchResult : Search->SearchResults)
  115.         {
  116.             //Ensure the connection string is resolvable and store the info in ConnectString and in SessionToJoin
  117.             if (Session.Pin()->GetResolvedConnectString(SessionInSearchResult, NAME_GamePort, ConnectString))
  118.             {
  119.                 SessionToJoin = &SessionInSearchResult;
  120.             }
  121.  
  122.             // For this course we will join the first session found automatically. Usually you would loop through all the sessions and determine which one is best to join.
  123.             break;
  124.         }
  125.         JoinSession();
  126.     }
  127.     else
  128.     {
  129.         PrintToScreen("Find lobby failed.");
  130.     }
  131.  
  132.     // Clear our handle and reset the delegate.
  133.     Session.Pin()->ClearOnFindSessionsCompleteDelegate_Handle(FindSessionsDelegateHandle);
  134.     FindSessionsDelegateHandle.Reset();
  135. }
  136.  
  137. void UEosHandler::CreateLobby(const FName KeyName, FString KeyValue)
  138. {
  139.     Session = Subsystem->GetSessionInterface();
  140.     if(!Session.IsValid())
  141.     {
  142.         PrintToScreen("No Session found!");
  143.         return;
  144.     }
  145.  
  146.     CreateLobbyDelegateHandle = Session.Pin()->AddOnCreateSessionCompleteDelegate_Handle(FOnCreateSessionCompleteDelegate::CreateUObject(this, &ThisClass::HandleCreateLobbyCompleted));
  147.  
  148.     const TSharedRef<FOnlineSessionSettings> SessionSettings = MakeShared<FOnlineSessionSettings>();
  149.     SessionSettings->NumPublicConnections = 5; //We will test our sessions with 2 players to keep things simple
  150.     SessionSettings->bShouldAdvertise = true; //This creates a public match and will be searchable.
  151.     SessionSettings->bUsesPresence = true; //No presence on dedicated server. This requires a local user.
  152.     SessionSettings->bAllowJoinViaPresence = true;
  153.     SessionSettings->bAllowJoinViaPresenceFriendsOnly = false;
  154.     SessionSettings->bAllowInvites = true; //Allow inviting players into session. This requires presence and a local user.
  155.     SessionSettings->bAllowJoinInProgress = true; //Once the session is started, no one can join.
  156.     SessionSettings->bIsDedicated = false; //Session created on dedicated server.
  157.     SessionSettings->bUseLobbiesIfAvailable = true; //For P2P we will use a lobby instead of a session
  158.     SessionSettings->bUseLobbiesVoiceChatIfAvailable = true; //We will also enable voice
  159.     SessionSettings->bUsesStats = true; //Needed to keep track of player stats.
  160.     SessionSettings->Settings.Add(KeyName, FOnlineSessionSetting((KeyValue), EOnlineDataAdvertisementType::ViaOnlineService));
  161.  
  162.     PrintToScreen("Creating Lobby...");
  163.  
  164.     if(!Session.Pin()->CreateSession(0, LobbyName, *SessionSettings))
  165.     {
  166.         PrintToScreen("Failed to create Lobby!");
  167.     }
  168. }
  169.  
  170. void UEosHandler::HandleCreateLobbyCompleted(const FName EosLobbyName, const bool bWasSuccessful)
  171. {
  172.     if(!Session.IsValid())
  173.     {
  174.         PrintToScreen("No Session found!");
  175.         return;
  176.     }
  177.  
  178.     if(bWasSuccessful)
  179.     {
  180.         PrintToScreen(FString::Printf(TEXT("Lobby: %ls Created!"), *EosLobbyName.ToString()));
  181.         FURL TravelURL;
  182.         TravelURL.Map = Map;
  183.         GetWorld()->Listen(TravelURL);
  184.     }
  185.     else
  186.     {
  187.         PrintToScreen("Failed to create lobby!");
  188.     }
  189.  
  190.     // Clear our handle and reset the delegate.
  191.     Session.Pin()->ClearOnCreateSessionCompleteDelegate_Handle(CreateLobbyDelegateHandle);
  192.     CreateLobbyDelegateHandle.Reset();
  193. }
  194.  
  195. void UEosHandler::JoinSession()
  196. {
  197.     if(!Session.IsValid())
  198.     {
  199.         PrintToScreen("No Session found!");
  200.         return;
  201.     }
  202.  
  203.     JoinSessionDelegateHandle = Session.Pin()->AddOnJoinSessionCompleteDelegate_Handle(FOnJoinSessionCompleteDelegate::CreateUObject(this, &ThisClass::HandleJoinSessionCompleted));
  204.  
  205.     PrintToScreen("Joining Lobby.");
  206.  
  207.     if(!Session.Pin()->JoinSession(0, "SessionName", *SessionToJoin))
  208.     {
  209.         PrintToScreen("Join Lobby failed.");
  210.  
  211.         // Clear our handle and reset the delegate.
  212.         Session.Pin()->ClearOnJoinSessionCompleteDelegate_Handle(JoinSessionDelegateHandle);
  213.         JoinSessionDelegateHandle.Reset();
  214.     }
  215. }
  216.  
  217. void UEosHandler::HandleJoinSessionCompleted(FName SessionName, const EOnJoinSessionCompleteResult::Type Result)
  218. {
  219.      if(!Session.IsValid())
  220.     {
  221.         PrintToScreen("No Session found!");
  222.         return;
  223.     }
  224.  
  225.     if(Result == EOnJoinSessionCompleteResult::Success)
  226.     {
  227.         PrintToScreen("Joined lobby.");
  228.         if(APlayerController* Pc = GetWorld()->GetFirstPlayerController())
  229.             Pc->ClientTravel(ConnectString, TRAVEL_Absolute);
  230.     }
  231.  
  232.     // Clear our handle and reset the delegate.
  233.     Session.Pin()->ClearOnJoinSessionCompleteDelegate_Handle(JoinSessionDelegateHandle);
  234.     JoinSessionDelegateHandle.Reset();
  235. }
  236.  
Advertisement
Add Comment
Please, Sign In to add comment