Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2021
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. bool UPongGameInstance::JoinOnlineGame()
  2. {
  3.     ULocalPlayer* Player = GetFirstGamePlayer();
  4.  
  5.     TSharedPtr<const FUniqueNetId> PlayerId = Player->GetPreferredUniqueNetId().GetUniqueNetId();
  6.  
  7.     FOnlineSessionSearchResult Result; 
  8.  
  9.     bool bWasSuccessful = false;
  10.  
  11.     for(FOnlineSessionSearchResult SearchResult : SessionSearch->SearchResults)
  12.     {
  13.         if(SearchResult.Session.OwningUserId != PlayerId)
  14.         {
  15.             bWasSuccessful = JoinGameSession(PlayerId, GameSessionName, SearchResult);
  16.  
  17.             if(bWasSuccessful)
  18.             {
  19.                 break;
  20.             }
  21.         }
  22.     }  
  23.  
  24.     return bWasSuccessful;
  25.  
  26. }
  27.  
  28. void UPongGameInstance::OnJoinSessionComplete(
  29.     FName SessionName,
  30.     EOnJoinSessionCompleteResult::Type Result
  31. )
  32. {
  33.     GEngine->AddOnScreenDebugMessage(
  34.         -1,
  35.         10.f,
  36.         FColor::Yellow,
  37.         FString::Printf(
  38.             TEXT("The connection request %s."),        
  39.             Result == EOnJoinSessionCompleteResult::Success ?
  40.                 *FString(TEXT("succeeded")) : *FString(TEXT("failed"))
  41.         )
  42.     );
  43.  
  44.     //The first thing to do when working with online subsystems
  45.     const IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
  46.  
  47.     if (OnlineSubsystem)
  48.     {
  49.         IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
  50.  
  51.         if(SessionInterface.IsValid())
  52.         {          
  53.             //Must clean the handle when the connection request is completed, to use it again
  54.             SessionInterface->ClearOnJoinSessionCompleteDelegate_Handle(
  55.                 OnJoinSessionCompleteDelegateHandle
  56.             );         
  57.            
  58.             if(Result == EOnJoinSessionCompleteResult::Success)
  59.             {
  60.                 TravelTo(SessionInterface, SessionName, ETravelType::TRAVEL_Absolute);             
  61.             }
  62.         }
  63.  
  64.         return;
  65.     }
  66.  
  67.     WarnsAboutLackOfSubsystem();
  68.    
  69. }
  70.  
  71. void UPongGameInstance::TravelTo(
  72.     const IOnlineSessionPtr& SessionInterface,
  73.     const FName& SessionName,
  74.     ETravelType Type
  75. )
  76. {
  77.     FString TravelURL;
  78.  
  79.     APlayerController* PlayerController = GetFirstLocalPlayerController();
  80.  
  81.     //Creates the connection information necessary to travel to the server
  82.     if (PlayerController && SessionInterface->GetResolvedConnectString(SessionName, TravelURL))
  83.     {
  84.         GEngine->AddOnScreenDebugMessage(
  85.             -1,
  86.             10.f,
  87.             FColor::Green,
  88.             FString::Printf(
  89.                 TEXT("Travelling to %s"),
  90.                 *TravelURL
  91.             )
  92.         );
  93.  
  94.         PlayerController->ClientTravel(TravelURL, ETravelType::TRAVEL_Absolute);
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement