Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool UPongGameInstance::JoinOnlineGame()
- {
- ULocalPlayer* Player = GetFirstGamePlayer();
- TSharedPtr<const FUniqueNetId> PlayerId = Player->GetPreferredUniqueNetId().GetUniqueNetId();
- FOnlineSessionSearchResult Result;
- bool bWasSuccessful = false;
- for(FOnlineSessionSearchResult SearchResult : SessionSearch->SearchResults)
- {
- if(SearchResult.Session.OwningUserId != PlayerId)
- {
- bWasSuccessful = JoinGameSession(PlayerId, GameSessionName, SearchResult);
- if(bWasSuccessful)
- {
- break;
- }
- }
- }
- return bWasSuccessful;
- }
- void UPongGameInstance::OnJoinSessionComplete(
- FName SessionName,
- EOnJoinSessionCompleteResult::Type Result
- )
- {
- GEngine->AddOnScreenDebugMessage(
- -1,
- 10.f,
- FColor::Yellow,
- FString::Printf(
- TEXT("The connection request %s."),
- Result == EOnJoinSessionCompleteResult::Success ?
- *FString(TEXT("succeeded")) : *FString(TEXT("failed"))
- )
- );
- //The first thing to do when working with online subsystems
- const IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
- if (OnlineSubsystem)
- {
- IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
- if(SessionInterface.IsValid())
- {
- //Must clean the handle when the connection request is completed, to use it again
- SessionInterface->ClearOnJoinSessionCompleteDelegate_Handle(
- OnJoinSessionCompleteDelegateHandle
- );
- if(Result == EOnJoinSessionCompleteResult::Success)
- {
- TravelTo(SessionInterface, SessionName, ETravelType::TRAVEL_Absolute);
- }
- }
- return;
- }
- WarnsAboutLackOfSubsystem();
- }
- void UPongGameInstance::TravelTo(
- const IOnlineSessionPtr& SessionInterface,
- const FName& SessionName,
- ETravelType Type
- )
- {
- FString TravelURL;
- APlayerController* PlayerController = GetFirstLocalPlayerController();
- //Creates the connection information necessary to travel to the server
- if (PlayerController && SessionInterface->GetResolvedConnectString(SessionName, TravelURL))
- {
- GEngine->AddOnScreenDebugMessage(
- -1,
- 10.f,
- FColor::Green,
- FString::Printf(
- TEXT("Travelling to %s"),
- *TravelURL
- )
- );
- PlayerController->ClientTravel(TravelURL, ETravelType::TRAVEL_Absolute);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement