Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2021
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. bool UPongGameInstance::HostGameSession(
  2.     TSharedPtr<const FUniqueNetId> UserId,
  3.     FName SessionName,
  4.     bool bIsLAN,
  5.     bool bIsPresence,
  6.     int32 MaxNumPlayers
  7. )
  8. {
  9.     //The first thing to do when working with online subsystems    
  10.     const IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
  11.  
  12.     if(OnlineSubsystem)
  13.     {
  14.         //Is necessary to obtain a session interface to do stuff here
  15.         IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
  16.  
  17.         if(SessionInterface.IsValid() && UserId.IsValid())
  18.         {
  19.             SessionSettings = MakeShareable(new FOnlineSessionSettings());
  20.  
  21.             SessionSettings->bIsLANMatch = bIsLAN;
  22.             SessionSettings->bUsesPresence = bIsPresence;
  23.             SessionSettings->NumPublicConnections = MaxNumPlayers;
  24.             SessionSettings->NumPrivateConnections = 0;
  25.             SessionSettings->bAllowInvites = true;
  26.             SessionSettings->bAllowJoinInProgress = true;
  27.             SessionSettings->bAllowJoinViaPresence = true;
  28.             SessionSettings->bAllowJoinViaPresenceFriendsOnly = false;
  29.             SessionSettings->bShouldAdvertise = true;
  30.            
  31.             SessionSettings->Set(
  32.                 SETTING_MAPNAME,
  33.                 FString("PongMap"),
  34.                 EOnlineDataAdvertisementType::ViaOnlineService
  35.             );
  36.            
  37.             //Indicates the delegate to be called when the session creation is completed
  38.             OnCreateSessionCompleteDelegateHandle = SessionInterface->
  39.                 AddOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegate);
  40.  
  41.             //The delegate should be called after the session creation
  42.             return SessionInterface->CreateSession(*UserId, SessionName, *SessionSettings);
  43.         }
  44.    
  45.     }
  46.  
  47.     WarnsAboutLackOfSubsystem();   
  48.  
  49.     return false;
  50.  
  51. }
  52.  
  53. void UPongGameInstance::OnCreateSessionComplete(FName SessionName, bool bWasSuccessful)
  54. {
  55.     GEngine->AddOnScreenDebugMessage(
  56.         -1,
  57.         10.f,
  58.         FColor::Yellow,
  59.         FString::Printf(
  60.             TEXT("The %s creation %s."),
  61.             *SessionName.ToString(),
  62.             bWasSuccessful ? *FString(TEXT("succeeded")) : *FString(TEXT("failed"))
  63.         )
  64.     );
  65.  
  66.     //The first thing to do when working with online subsystems
  67.     const IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
  68.  
  69.     if(OnlineSubsystem)
  70.     {
  71.         //Is necessary to obtain a session interface to do stuff here
  72.         IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
  73.  
  74.         if(SessionInterface.IsValid())
  75.         {
  76.             //Must clean the handle when the session creation is done, to use it again
  77.             SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(
  78.                 OnCreateSessionCompleteDelegateHandle
  79.             );
  80.  
  81.             if(bWasSuccessful)
  82.             {
  83.                 //Indicates the delegate to be called when the session is started
  84.                 OnStartSessionCompleteDelegateHandle = SessionInterface->
  85.                     AddOnStartSessionCompleteDelegate_Handle(OnStartSessionCompleteDelegate);
  86.  
  87.                 //The delegate should be called after the session start
  88.                 SessionInterface->StartSession(SessionName);
  89.  
  90.                 return;
  91.             }      
  92.         }
  93.     }
  94.  
  95.     WarnsAboutLackOfSubsystem();
  96.  
  97. }
  98.  
  99. void UPongGameInstance::OnStartSessionComplete(FName SessionName, bool bWasSuccessful)
  100. {
  101.     GEngine->AddOnScreenDebugMessage(
  102.         -1,
  103.         10.f,
  104.         FColor::Yellow,
  105.         FString::Printf(
  106.             TEXT("The %s initialization %s."),
  107.             *SessionName.ToString(),
  108.             bWasSuccessful ? *FString(TEXT("succeeded")) : *FString(TEXT("failed"))
  109.         )
  110.     );
  111.  
  112.     //The first thing to do when working with online subsystems
  113.     const IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
  114.  
  115.     if(OnlineSubsystem)
  116.     {
  117.         //Is necessary to obtain a session interface to do stuff here
  118.         IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
  119.  
  120.         if(SessionInterface.IsValid())
  121.         {
  122.             //Must clean the handle when the session initialization is done, to use it again
  123.             SessionInterface->ClearOnStartSessionCompleteDelegate_Handle(
  124.                 OnStartSessionCompleteDelegateHandle
  125.             );
  126.  
  127.             if (bWasSuccessful)
  128.             {
  129.                 UGameplayStatics::OpenLevel(
  130.                     GetWorld(),
  131.                     "PongMap?listen",
  132.                     true,
  133.                     FString::Printf(
  134.                         TEXT("?%s"),
  135.                         SessionSettings->bIsLANMatch ?
  136.                             TEXT("bIsLanMatch=1") : TEXT("bIsLanMatch=0")
  137.                     )
  138.  
  139.                 );     
  140.             }
  141.  
  142.             return;
  143.         }
  144.     }
  145.  
  146.     WarnsAboutLackOfSubsystem();
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement