Advertisement
Guest User

Untitled

a guest
Jul 14th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "ServerGameMode.h"
  4. #include "Blueprint/UserWidget.h"
  5. #include "Widgets/Login.h"
  6. #include "Widgets/LoginWidget.h"
  7. #include "Engine/Engine.h"
  8. #include "OnlineSessionSettings.h"
  9. //const static FName SESSION_NAME = TEXT("");
  10. const static FName SERVER_NAME_SETTINGS_KEY = TEXT("ServerName");
  11.  
  12. void AServerGameMode::BeginPlay()
  13. {
  14. //AGameSession();
  15.  
  16. IOnlineSubsystem* SubSystem = IOnlineSubsystem::Get();
  17. if (SubSystem != nullptr) {
  18. UE_LOG(LogTemp, Warning, TEXT("Found Substem %s"), *SubSystem->GetSubsystemName().ToString());
  19. SessionInterface = SubSystem->GetSessionInterface();
  20. if (SessionInterface.IsValid())
  21. {
  22. Host("dsd");
  23. SessionInterface->OnCreateSessionCompleteDelegates.AddUObject(this, &AServerGameMode::OnCreateSessionComplete);
  24. SessionInterface->OnDestroySessionCompleteDelegates.AddUObject(this, &AServerGameMode::OnCreateSessionComplete);
  25.  
  26. //SessionInterface->OnFindSessionsCompleteDelegates.AddUObject(this, &AServerGameMode::OnFindSessionComplete);
  27. // SessionInterface->OnJoinSessionCompleteDelegates.AddUObject(this, &AServerGameMode::OnJoinSessionComplete);
  28.  
  29. }
  30.  
  31.  
  32. }
  33. else {
  34. UE_LOG(LogTemp, Warning, TEXT("Found no Substem"));
  35. }
  36.  
  37. }
  38.  
  39.  
  40. void AServerGameMode::OnFindSessionComplete(bool Sucess)
  41. {
  42.  
  43.  
  44. if (Sucess && SessionSearch.IsValid() && loginMenu != nullptr)
  45. {
  46. UE_LOG(LogTemp, Warning, TEXT("Finished Find Session"));
  47.  
  48. TArray<FServerData> ServerName;
  49.  
  50. for (const FOnlineSessionSearchResult& SearchResult : SessionSearch->SearchResults) {
  51. UE_LOG(LogTemp, Warning, TEXT("Found Session Name %s"), *SearchResult.GetSessionIdStr());
  52. FServerData Data;
  53.  
  54. Data.MaxPlayers = SearchResult.Session.SessionSettings.NumPublicConnections;
  55. Data.CurrentPlayers = Data.MaxPlayers - SearchResult.Session.NumOpenPublicConnections;
  56. Data.HostUserName = SearchResult.Session.OwningUserName;
  57. FString ServerNames;
  58. if (SearchResult.Session.SessionSettings.Get(SERVER_NAME_SETTINGS_KEY, ServerNames))
  59. {
  60. Data.Name = ServerNames;
  61. // UE_LOG(LogTemp, Warning, TEXT("Data Found Setting %s"), *TestSetting);
  62. }
  63. else
  64. {
  65. Data.Name = "Could Not Find the Server";
  66. // UE_LOG(LogTemp, Warning, TEXT("Data Not Found Setting"));
  67. }
  68. ServerName.Add(Data);
  69. }
  70. loginMenu->SetServerList(ServerName);
  71. }
  72.  
  73. }
  74. void AServerGameMode::OnCreateSessionComplete(FName SessionName, bool Sucess) {
  75.  
  76. if (!Sucess)
  77. {
  78. UE_LOG(LogTemp, Warning, TEXT("Could not Create Session"));
  79. return;
  80. }
  81. if (loginMenu != nullptr)
  82. {
  83. loginMenu->TearDown();
  84. }
  85. //UEngine* Engine = GetEngine();
  86. //if (!ensure(Engine != nullptr)) return;
  87. // Engine->AddOnScreenDebugMessage(0, 5, FColor::Red, TEXT("Hosting"));
  88. UWorld* World = GetWorld();
  89. if (!ensure(World != nullptr)) return;
  90. World->ServerTravel("/Game/Assets/Maps/Lobby?listen");
  91. }
  92.  
  93. void AServerGameMode::OndestroySessionComplete(FName SessionName, bool Sucess)
  94. {
  95. if (Sucess) {
  96. CreateSession();
  97. }
  98. }
  99. void AServerGameMode::Host(FString Servername)
  100. {
  101. DiresedServername = Servername;
  102. if (SessionInterface.IsValid()) {
  103.  
  104. auto ExistingSession = SessionInterface->GetNamedSession(GameSessionName);
  105. if (ExistingSession != nullptr)
  106. {
  107. SessionInterface->DestroySession(GameSessionName);
  108. }
  109. else
  110. {
  111. CreateSession();
  112.  
  113. }
  114.  
  115. }
  116.  
  117. }
  118.  
  119. void AServerGameMode::CreateSession()
  120. {
  121. if (SessionInterface.IsValid()) {
  122. FOnlineSessionSettings SessionSettings;
  123.  
  124. if (IOnlineSubsystem::Get()->GetSubsystemName() == "NULL") {
  125. SessionSettings.bIsLANMatch = true;
  126. }
  127. else {
  128. SessionSettings.bIsLANMatch = false;
  129. }
  130.  
  131. SessionSettings.NumPublicConnections = 5;
  132. SessionSettings.bShouldAdvertise = true;
  133. SessionSettings.bUsesPresence = false;
  134. SessionSettings.bIsDedicated = true;
  135. SessionSettings.Set(SERVER_NAME_SETTINGS_KEY, DiresedServername, EOnlineDataAdvertisementType::ViaOnlineServiceAndPing);
  136.  
  137. SessionInterface->CreateSession(0, GameSessionName, SessionSettings);
  138.  
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement