bool UUModGameInstance::JoinGame(FString ip, int32 port) { IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::Get(); IOnlineSessionPtr Sessions = NULL; if (OnlineSub == NULL) { return false; } Sessions = OnlineSub->GetSessionInterface(); if (!Sessions.IsValid()) { return false; } if (!CurSessionName.IsEmpty()){ DestroyCurSession(Sessions); return false; } ULocalPlayer* const Player = GetFirstGamePlayer(); FString str = ip + FString(":"); str.AppendInt(port); ConnectIP = str; Player->PlayerController->ClientTravel("LoadScreen", ETravelType::TRAVEL_Absolute); DelayedServerConnect = true; return false; }
// Find All Online Sessions void URadeGameInstance::FindOnlineGames() { ULocalPlayer* const Player = GetFirstGamePlayer(); FindSessions(Player->GetPreferredUniqueNetId(), GameSessionName, true, true); }
void UtrnetDemoGameInstance::JoinOnlineGame() { ULocalPlayer* const Player = GetFirstGamePlayer(); // Just a SearchResult where we can save the one we want to use, for the case we find more than one! FOnlineSessionSearchResult SearchResult; if (!SessionSearch.IsValid()) { GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("No Session Search Relult,Pls using 'find Gmes' first "))); return; } // If the Array is not empty, we can go through it if (SessionSearch->SearchResults.Num() <= 0) { GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("No Session found!!!,Can NOT do join!"))); return; } // find sessions,join first session GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("FindSessions Num: %d "), SessionSearch->SearchResults.Num())); for (int32 i = 0; i < SessionSearch->SearchResults.Num(); i++) { // To avoid something crazy, we filter sessions from ourself if (SessionSearch->SearchResults[i].Session.OwningUserId != Player->GetPreferredUniqueNetId()) { SearchResult = SessionSearch->SearchResults[i]; // Once we found sounce a Session that is not ours, just join it. Instead of using a for loop, you could // use a widget where you click on and have a reference for the GameSession it represents which you can use // here JoinSessionA(Player->GetPreferredUniqueNetId(), GameSessionName, SearchResult); break; } } }
void UtrnetDemoGameInstance::StartOnlineGame() { // Creating a local player where we can get the UserID from ULocalPlayer* const Player = GetFirstGamePlayer(); // Call our custom HostSession function. GameSessionName is a GameInstance variable HostSession(Player->GetPreferredUniqueNetId(), GameSessionName, true, true, 4); }
// Host Speicific Map void URadeGameInstance::StartOnlineGameMap(FString MapName, int32 MaxPlayerNumber) { // Creating a local player where we can get the UserID from ULocalPlayer* const Player = GetFirstGamePlayer(); //Player->GetPreferredUniqueNetId() TheMapName = MapName; // Call our custom HostSession function. GameSessionName is a GameInstance variable HostSession(Player->GetPreferredUniqueNetId(), GameSessionName, TheMapName, true, true, MaxPlayerNumber); }
void UAmethystGameInstance::BeginWelcomeScreenState() { LoadFrontEndMap(WelcomeScreenMap); ULocalPlayer* const LocalPlayer = GetFirstGamePlayer(); LocalPlayer->SetCachedUniqueNetId(nullptr); check(!WelcomeMenuUI.IsValid()); WelcomeMenuUI = MakeShareable(new FAmethystWelcomeMenu); WelcomeMenuUI->Construct(this); WelcomeMenuUI->AddToGameViewport(); }
// Host Default Map void URadeGameInstance::StartOnlineGame() { // Creating a local player where we can get the UserID from ULocalPlayer* const Player = GetFirstGamePlayer(); TheMapName = "BattleArena"; // Call our custom HostSession function. GameSessionName is a GameInstance variable HostSession(Player->GetPreferredUniqueNetId(), *PlayerName, TheMapName, true, true, 16); }
void UUModGameInstance::Tick(float DeltaTime) { if (DelayedRunMap) { ticks++; if (ticks >= 5) { if (IsLAN) { FString opt = FString("game=") + AUModGameMode::StaticClass()->GetPathName() + FString("?listen?bIsLanMatch"); UGameplayStatics::OpenLevel(GetWorld(), FName(*CurSessionMapName), true, opt); } else { FString opt = FString("game=") + AUModGameMode::StaticClass()->GetPathName() + FString("?listen"); UGameplayStatics::OpenLevel(GetWorld(), FName(*CurSessionMapName), true, opt); } IsLAN = false; CurSessionMapName = NULL; DelayedRunMap = false; ticks = 0; } } else if (DelayedServerConnect) { ticks++; if (ticks >= 5) { ULocalPlayer* const Player = GetFirstGamePlayer(); Player->PlayerController->ClientTravel(ConnectIP, ETravelType::TRAVEL_Absolute); ticks = 0; } } if (!CurSessionName.IsEmpty()) { IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::Get(); IOnlineSessionPtr Sessions = NULL; if (OnlineSub == NULL) { return; } Sessions = OnlineSub->GetSessionInterface(); if (!Sessions.IsValid()) { return; } EOnlineSessionState::Type state = Sessions->GetSessionState(*CurSessionName); if (state == EOnlineSessionState::Ended) { Disconnect(FString("Lost Connection...")); } } }
void UUModGameInstance::Disconnect(FString error) { IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::Get(); IOnlineSessionPtr Sessions = NULL; if (OnlineSub == NULL) { return; } Sessions = OnlineSub->GetSessionInterface(); if (!Sessions.IsValid()) { return; } if (!CurSessionName.IsEmpty()) { DestroyCurSession(Sessions); } netError = error; ULocalPlayer* const Player = GetFirstGamePlayer(); Player->PlayerController->ClientTravel("LoadScreen", ETravelType::TRAVEL_Absolute); }
// Join Any Avaiable Online Game void URadeGameInstance::JoinOnlineGame() { ULocalPlayer* const Player = GetFirstGamePlayer(); // Just a SearchResult where we can save the one we want to use, for the case we find more than one! FOnlineSessionSearchResult SearchResult; // If the Array is not empty, we can go through it if (SessionSearch->SearchResults.Num() > 0) { for (int32 i = 0; i < SessionSearch->SearchResults.Num(); i++) { // To avoid something crazy, we filter sessions from ourself if (SessionSearch->SearchResults[i].Session.OwningUserId != Player->GetPreferredUniqueNetId()) { SearchResult = SessionSearch->SearchResults[i]; JoinSession(Player->GetPreferredUniqueNetId(), GameSessionName, SearchResult); break; } } } }
void UAmethystGameInstance::BeginMainMenuState() { // Make sure we're not showing the loadscreen /* TO DO: GameViewportClient UAmethystGameViewportClient * AmethystViewport = Cast<UAmethystGameViewportClient>(GetGameViewportClient()); if (AmethystViewport != NULL) { AmethystViewport->HideLoadingScreen(); } // Set presence to menu state for the owning player SetPresenceForLocalPlayers(FVariantData(FString(TEXT("OnMenu")))); // load startup map LoadFrontEndMap(MainMenuMap); // player 0 gets to own the UI ULocalPlayer* const Player = GetFirstGamePlayer(); MainMenuUI = MakeShareable(new FAmethystMainMenu()); MainMenuUI->Construct(this, Player); MainMenuUI->AddMenuToGameViewport(); */ ULocalPlayer* const Player = GetFirstGamePlayer(); #if !Amethyst_CONSOLE_UI // The cached unique net ID is usually set on the welcome screen, but there isn't // one on PC/Mac, so do it here. if (Player != nullptr) { Player->SetControllerId(0); Player->SetCachedUniqueNetId(Player->GetUniqueNetIdFromCachedControllerId()); } #endif }
bool UUModGameInstance::StartNewGame(bool single, bool local, int32 max, FString map, FString hostName) { ULocalPlayer* const Player = GetFirstGamePlayer(); Player->PlayerController->ClientTravel("LoadScreen", ETravelType::TRAVEL_Absolute); TSharedPtr<const FUniqueNetId> UserId = Player->GetPreferredUniqueNetId(); IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::Get(); IOnlineSessionPtr Sessions = NULL; if (OnlineSub == NULL) { return false; } Sessions = OnlineSub->GetSessionInterface(); if (!Sessions.IsValid() || !UserId.IsValid()) { return false; } if (!CurSessionName.IsEmpty()){ DestroyCurSession(Sessions); return false; } TSharedPtr<class FOnlineSessionSettings> SessionSettings = MakeShareable(new FOnlineSessionSettings()); if (single) { SessionSettings->bIsLANMatch = true; IsLAN = true; } else { SessionSettings->bIsLANMatch = local; IsLAN = local; } SessionSettings->bUsesPresence = true; if (single) { SessionSettings->NumPublicConnections = 1; } else { SessionSettings->NumPublicConnections = max; } SessionSettings->NumPrivateConnections = 0; SessionSettings->bAllowInvites = true; SessionSettings->bAllowJoinInProgress = true; SessionSettings->bShouldAdvertise = true; SessionSettings->bAllowJoinViaPresence = true; SessionSettings->bAllowJoinViaPresenceFriendsOnly = false; SessionSettings->Set(SETTING_MAPNAME, map, EOnlineDataAdvertisementType::ViaOnlineService); SessionSettings->Set(SETTING_GAMEMODE, AUModGameMode::StaticClass()->GetPathName(), EOnlineDataAdvertisementType::ViaOnlineService); if (single) { hostName = TEXT("UMod_SinglePlayer"); } OnCreateSessionCompleteDelegateHandle = Sessions->AddOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegate); CurSessionMapName = map; FName name = FName(*hostName); CurSessionName = hostName; return Sessions->CreateSession(*UserId, name, *SessionSettings); }
void UUModGameInstance::ReturnToMainMenu() { netError = TEXT(""); ULocalPlayer* const Player = GetFirstGamePlayer(); Player->PlayerController->ClientTravel("MainMenu", ETravelType::TRAVEL_Absolute); }
// Join Selected Online Session void URadeGameInstance::JoinSelectedOnlineGame(FAvaiableSessionsData SessionData) { ULocalPlayer* const Player = GetFirstGamePlayer(); JoinSession(Player->GetPreferredUniqueNetId(), GameSessionName, SessionData.SessionData); }