void UtrnetDemoGameInstance::OnFindSessionsComplete(bool bWasSuccessful) { isLoading_ = false; GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("OFindSessionsComplete bSuccess: %d"), bWasSuccessful)); // Get SessionInterface of the OnlineSubsystem IOnlineSessionPtr Sessions = GetSession(); if (!Sessions.IsValid()) { return; } // Clear the Delegate handle, since we finished this call Sessions->ClearOnFindSessionsCompleteDelegate_Handle(OnFindSessionsCompleteDelegateHandle); // Just debugging the Number of Search results. Can be displayed in UMG or something later on GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("Num Search Results: %d"), SessionSearch->SearchResults.Num())); // If we have found at least 1 session, we just going to debug them. You could add them to a list of UMG Widgets, like it is done in the BP version! if (SessionSearch->SearchResults.Num() > 0) { // "SessionSearch->SearchResults" is an Array that contains all the information. You can access the Session in this and get a lot of information. // This can be customized later on with your own classes to add more information that can be set and displayed for (int32 SearchIdx = 0; SearchIdx < SessionSearch->SearchResults.Num(); SearchIdx++) { // OwningUserName is just the SessionName for now. I guess you can create your own Host Settings class and GameSession Class and add a proper GameServer Name here. // This is something you can't do in Blueprint for example! GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("Session Number: %d | Sessionname: %s "), SearchIdx + 1, *(SessionSearch->SearchResults[SearchIdx].Session.OwningUserName))); } } }
void UAdvancedSessionsLibrary::GetSessionSettings(int32 &NumConnections, bool &bIsLAN, bool &bIsDedicated, bool &bIsAnticheatEnabled, int32 &BuildUniqueID, TArray<FSessionPropertyKeyPair> &ExtraSettings, TEnumAsByte<EBlueprintResultSwitch::Type> &Result) { IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(); if (!SessionInterface.IsValid()) { UE_LOG(AdvancedSessionsLog, Warning, TEXT("GetSessionSettings couldn't get the session interface!")); Result = EBlueprintResultSwitch::Type::OnFailure; return; } FOnlineSessionSettings* settings = SessionInterface->GetSessionSettings(GameSessionName); if (!settings) { UE_LOG(AdvancedSessionsLog, Warning, TEXT("GetSessionSettings couldn't get the session settings!")); Result = EBlueprintResultSwitch::Type::OnFailure; return; } BuildUniqueID = settings->BuildUniqueId; NumConnections = settings->NumPublicConnections; bIsLAN = settings->bIsLANMatch; bIsDedicated = settings->bIsDedicated; bIsAnticheatEnabled = settings->bAntiCheatProtected; FSessionPropertyKeyPair NewSetting; for (auto& Elem : settings->Settings) { NewSetting.Key = Elem.Key; NewSetting.Data = Elem.Value.Data; ExtraSettings.Add(NewSetting); } Result = EBlueprintResultSwitch::Type::OnSuccess; }
bool APartyBeaconClient::RequestReservation(const FOnlineSessionSearchResult& DesiredHost, const FUniqueNetIdRepl& RequestingPartyLeader, const TArray<FPlayerReservation>& PartyMembers) { bool bSuccess = false; if (DesiredHost.IsValid()) { UWorld* World = GetWorld(); IOnlineSubsystem* OnlineSub = Online::GetSubsystem(World); if (OnlineSub) { IOnlineSessionPtr SessionInt = OnlineSub->GetSessionInterface(); if (SessionInt.IsValid()) { FString ConnectInfo; if (SessionInt->GetResolvedConnectString(DesiredHost, BeaconPort, ConnectInfo)) { FString SessionId = DesiredHost.Session.SessionInfo->GetSessionId().ToString(); return RequestReservation(ConnectInfo, SessionId, RequestingPartyLeader, PartyMembers); } } } } if (!bSuccess) { OnFailure(); } return bSuccess; }
void UtrnetDemoGameInstance::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result) { isLoading_ = false; GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("OnJoinSessionComplete %s, %d"), *SessionName.ToString(), static_cast<int32>(Result))); // Get SessionInterface from the OnlineSubsystem IOnlineSessionPtr Sessions = GetSession(); if (!Sessions.IsValid()) { return; } // Clear the Delegate again Sessions->ClearOnJoinSessionCompleteDelegate_Handle(OnJoinSessionCompleteDelegateHandle); // Get the first local PlayerController, so we can call "ClientTravel" to get to the Server Map // This is something the Blueprint Node "Join Session" does automatically! APlayerController * const PlayerController = GetFirstLocalPlayerController(); // We need a FString to use ClientTravel and we can let the SessionInterface contruct such a // String for us by giving him the SessionName and an empty String. We want to do this, because // Every OnlineSubsystem uses different TravelURLs FString TravelURL; if (PlayerController && Sessions->GetResolvedConnectString(SessionName, TravelURL)) { // Finally call the ClienTravel. If you want, you could print the TravelURL to see // how it really looks like PlayerController->ClientTravel(TravelURL, ETravelType::TRAVEL_Absolute); } }
/** * Delegate fired when a session start request has completed * * @param SessionName the name of the session this callback is for * @param bWasSuccessful true if the async action completed without error, false if there was an error */ void URadeGameInstance::OnStartOnlineGameComplete(FName SessionName, bool bWasSuccessful) { //GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("OnStartSessionComplete %s, %d"), *SessionName.ToString(), bWasSuccessful)); // Get the Online Subsystem so we can get the Session Interface IOnlineSubsystem* OnlineSub = IOnlineSubsystem::Get(); if (OnlineSub) { // Get the Session Interface to clear the Delegate IOnlineSessionPtr Sessions = OnlineSub->GetSessionInterface(); if (Sessions.IsValid()) { // Clear the delegate, since we are done with this call Sessions->ClearOnStartSessionCompleteDelegate_Handle(OnStartSessionCompleteDelegateHandle); } } // If the start was successful, we can open a NewMap if we want. Make sure to use "listen" as a parameter! if (bWasSuccessful) { //BattleArena UGameplayStatics::OpenLevel(GetWorld(), *TheMapName, true, "listen"); } }
/** * Delegate fired when a session create request has completed * * @param SessionName the name of the session this callback is for * @param bWasSuccessful true if the async action completed without error, false if there was an error */ void URadeGameInstance::OnCreateSessionComplete(FName SessionName, bool bWasSuccessful) { //GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("OnCreateSessionComplete %s, %d"), *SessionName.ToString(), bWasSuccessful)); // Get the OnlineSubsystem so we can get the Session Interface IOnlineSubsystem* OnlineSub = IOnlineSubsystem::Get(); if (OnlineSub) { // Get the Session Interface to call the StartSession function IOnlineSessionPtr Sessions = OnlineSub->GetSessionInterface(); if (Sessions.IsValid()) { // Clear the SessionComplete delegate handle, since we finished this call Sessions->ClearOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegateHandle); if (bWasSuccessful) { // Set the StartSession delegate handle OnStartSessionCompleteDelegateHandle = Sessions->AddOnStartSessionCompleteDelegate_Handle(OnStartSessionCompleteDelegate); //printr(SessionName.ToString()); // Our StartSessionComplete delegate should get called after this Sessions->StartSession(SessionName); } } } }
// Join Session bool URadeGameInstance::JoinSession(TSharedPtr<const FUniqueNetId> UserId, FName SessionName, const FOnlineSessionSearchResult& SearchResult) { // Return bool bool bSuccessful = false; // Get OnlineSubsystem we want to work with IOnlineSubsystem* OnlineSub = IOnlineSubsystem::Get(); if (OnlineSub) { // Get SessionInterface from the OnlineSubsystem IOnlineSessionPtr Sessions = OnlineSub->GetSessionInterface(); if (Sessions.IsValid() && UserId.IsValid()) { // Set the Handle again OnJoinSessionCompleteDelegateHandle = Sessions->AddOnJoinSessionCompleteDelegate_Handle(OnJoinSessionCompleteDelegate); // Call the "JoinSession" Function with the passed "SearchResult". The "SessionSearch->SearchResults" can be used to get such a // "FOnlineSessionSearchResult" and pass it. Pretty straight forward! bSuccessful = Sessions->JoinSession(*UserId, SessionName, SearchResult); } } return bSuccessful; }
/** Starts the online game using the session name in the PlayerState */ void AShooterPlayerController::ClientStartOnlineGame_Implementation() { if (!IsPrimaryPlayer()) return; AShooterPlayerState* ShooterPlayerState = Cast<AShooterPlayerState>(PlayerState); if (ShooterPlayerState) { IOnlineSubsystem* OnlineSub = IOnlineSubsystem::Get(); if (OnlineSub) { IOnlineSessionPtr Sessions = OnlineSub->GetSessionInterface(); if (Sessions.IsValid()) { UE_LOG(LogOnline, Log, TEXT("Starting session %s on client"), *ShooterPlayerState->SessionName.ToString() ); Sessions->StartSession(ShooterPlayerState->SessionName); } } } else { // Keep retrying until player state is replicated GetWorld()->GetTimerManager().SetTimer(FTimerDelegate::CreateUObject(this, &AShooterPlayerController::ClientStartOnlineGame_Implementation), 0.2f, false); } }
void URadeGameInstance::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result) { // Get the OnlineSubsystem we want to work with IOnlineSubsystem* OnlineSub = IOnlineSubsystem::Get(); if (OnlineSub) { // Get SessionInterface from the OnlineSubsystem IOnlineSessionPtr Sessions = OnlineSub->GetSessionInterface(); if (Sessions.IsValid()) { // Clear the Delegate again Sessions->ClearOnJoinSessionCompleteDelegate_Handle(OnJoinSessionCompleteDelegateHandle); // Get the first local PlayerController, so we can call "ClientTravel" to get to the Server Map // This is something the Blueprint Node "Join Session" does automatically! APlayerController * const PlayerController = GetFirstLocalPlayerController(); // We need a FString to use ClientTravel and we can let the SessionInterface contruct such a // String for us by giving him the SessionName and an empty String. We want to do this, because // Every OnlineSubsystem uses different TravelURLs FString TravelURL; if (PlayerController && Sessions->GetResolvedConnectString(SessionName, TravelURL)) { // Finally call the ClienTravel. If you want, you could print the TravelURL to see // how it really looks like PlayerController->ClientTravel(TravelURL, ETravelType::TRAVEL_Absolute); } } } }
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; }
void AFPSGPlayerController::onClickEndGame() const { //Make sure it is the server calling the function if (Role < ROLE_Authority && GetNetMode() == NM_Client) return; //GEngine->AddOnScreenDebugMessage(-1, 40.0f, FColor::Cyan, "AFPSGPlayerController::onClickEndGame"); IOnlineSubsystem* onlineSubsystem = IOnlineSubsystem::Get(); IOnlineSessionPtr sessions = onlineSubsystem != NULL ? onlineSubsystem->GetSessionInterface() : NULL; if (sessions.IsValid()) { UWorld* world = GetWorld(); AFPSGGameState* gameState = world != NULL ? world->GetGameState<AFPSGGameState>() : NULL; if (gameState != NULL) { //Retrieve the session state and match state EOnlineSessionState::Type sessionState = sessions->GetSessionState(GameSessionName); FName matchState = gameState->GetMatchState(); //Only attempt to end the game if both the session and match are in progress if (sessionState == EOnlineSessionState::Type::InProgress && matchState == MatchState::InProgress) { gameState->onClickEndGame(); } } } }
void UOnlineSessionClient::OnJoinSessionComplete(FName SessionName, EOnJoinSessionCompleteResult::Type Result) { UE_LOG(LogOnline, Verbose, TEXT("OnJoinSessionComplete %s bSuccess: %d"), *SessionName.ToString(), static_cast<int32>(Result)); IOnlineSessionPtr SessionInt = GetSessionInt(); if (SessionInt.IsValid()) { SessionInt->ClearOnJoinSessionCompleteDelegate_Handle(OnJoinSessionCompleteDelegateHandle); if (Result == EOnJoinSessionCompleteResult::Success) { FString URL; if (SessionInt->GetResolvedConnectString(SessionName, URL)) { APlayerController* PC = GetGameInstance()->GetFirstLocalPlayerController(GetWorld()); if (PC) { if (bIsFromInvite) { URL += TEXT("?bIsFromInvite"); bIsFromInvite = false; } PC->ClientTravel(URL, TRAVEL_Absolute); } } else { UE_LOG(LogOnline, Warning, TEXT("Failed to join session %s"), *SessionName.ToString()); } } } }
void UOnlineSessionClient::ClearOnlineDelegates() { IOnlineSessionPtr SessionInt = GetSessionInt(); if (SessionInt.IsValid()) { SessionInt->ClearOnSessionUserInviteAcceptedDelegate_Handle(OnSessionUserInviteAcceptedDelegateHandle); } }
/** * Async task is given a chance to trigger it's delegates */ virtual void TriggerDelegates() override { IOnlineSessionPtr SessionInt = Subsystem->GetSessionInterface(); if (SessionInt.IsValid()) { SessionInt->TriggerOnDestroySessionCompleteDelegates(SessionName, bWasSuccessful); } }
void UCheatManager::DumpOnlineSessionState() { IOnlineSessionPtr SessionInt = Online::GetSessionInterface(GetWorld()); if (SessionInt.IsValid()) { SessionInt->DumpSessionState(); } }
/** * Async task is given a chance to trigger it's delegates */ void FOnlineAsyncTaskSteamUpdateServer::TriggerDelegates() { IOnlineSessionPtr SessionInt = Subsystem->GetSessionInterface(); if (SessionInt.IsValid()) { SessionInt->TriggerOnUpdateSessionCompleteDelegates(SessionName, bWasSuccessful); } }
void UOnlineSessionClient::OnEndSessionComplete(FName SessionName, bool bWasSuccessful) { UE_LOG(LogOnline, Verbose, TEXT("OnEndSessionComplete %s bSuccess: %d"), *SessionName.ToString(), bWasSuccessful); IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(GetWorld()); if (SessionInterface.IsValid()) { SessionInterface->ClearOnEndSessionCompleteDelegate_Handle(EndSessionCompleteHandle); } }
void UtrnetDemoGameInstance::DestroySession() { IOnlineSessionPtr Sessions = GetSession(); if (Sessions.IsValid()) { OnDestroySessionCompleteDelegateHandle = Sessions->AddOnDestroySessionCompleteDelegate_Handle(OnDestroySessionCompleteDelegate); Sessions->DestroySession(GameSessionName); } }
void UOnlineSessionClient::OnEndForJoinSessionComplete(FName SessionName, bool bWasSuccessful) { UE_LOG(LogOnline, Verbose, TEXT("OnEndForJoinSessionComplete %s bSuccess: %d"), *SessionName.ToString(), bWasSuccessful); IOnlineSessionPtr SessionInt = GetSessionInt(); if (SessionInt.IsValid()) { SessionInt->ClearOnEndSessionCompleteDelegate_Handle(OnEndForJoinSessionCompleteDelegateHandle); } DestroyExistingSession_Impl(OnDestroyForJoinSessionCompleteDelegateHandle, SessionName, OnDestroyForJoinSessionCompleteDelegate); }
/** * Give the async task a chance to marshal its data back to the game thread * Can only be called on the game thread by the async task manager */ virtual void Finalize() override { IOnlineSessionPtr SessionInt = Subsystem->GetSessionInterface(); if (SessionInt.IsValid()) { FNamedOnlineSession* Session = SessionInt->GetNamedSession(SessionName); if (Session) { SessionInt->RemoveNamedSession(SessionName); } } }
/** Ends and/or destroys game session */ void AShooterPlayerController::CleanupSessionOnReturnToMenu() { bool bPendingOnlineOp = false; // end online game and then destroy it AShooterPlayerState* ShooterPlayerState = Cast<AShooterPlayerState>(PlayerState); if (ShooterPlayerState) { IOnlineSubsystem* OnlineSub = IOnlineSubsystem::Get(); if (OnlineSub) { IOnlineSessionPtr Sessions = OnlineSub->GetSessionInterface(); if (Sessions.IsValid()) { EOnlineSessionState::Type SessionState = Sessions->GetSessionState(ShooterPlayerState->SessionName); UE_LOG(LogOnline, Log, TEXT("Session %s is '%s'"), *ShooterPlayerState->SessionName.ToString(), EOnlineSessionState::ToString(SessionState)); if (EOnlineSessionState::InProgress == SessionState) { UE_LOG(LogOnline, Log, TEXT("Ending session %s on return to main menu"), *ShooterPlayerState->SessionName.ToString() ); Sessions->AddOnEndSessionCompleteDelegate(OnEndSessionCompleteDelegate); Sessions->EndSession(ShooterPlayerState->SessionName); bPendingOnlineOp = true; } else if (EOnlineSessionState::Ending == SessionState) { UE_LOG(LogOnline, Log, TEXT("Waiting for session %s to end on return to main menu"), *ShooterPlayerState->SessionName.ToString() ); Sessions->AddOnEndSessionCompleteDelegate(OnEndSessionCompleteDelegate); bPendingOnlineOp = true; } else if (EOnlineSessionState::Ended == SessionState || EOnlineSessionState::Pending == SessionState) { UE_LOG(LogOnline, Log, TEXT("Destroying session %s on return to main menu"), *ShooterPlayerState->SessionName.ToString() ); Sessions->AddOnDestroySessionCompleteDelegate(OnDestroySessionCompleteDelegate); Sessions->DestroySession(ShooterPlayerState->SessionName); bPendingOnlineOp = true; } else if (EOnlineSessionState::Starting == SessionState) { UE_LOG(LogOnline, Log, TEXT("Waiting for session %s to start, and then we will end it to return to main menu"), *ShooterPlayerState->SessionName.ToString() ); Sessions->AddOnStartSessionCompleteDelegate(OnStartSessionCompleteEndItDelegate); bPendingOnlineOp = true; } } } } if (!bPendingOnlineOp) { GEngine->HandleDisconnect(GetWorld(), GetWorld()->GetNetDriver()); } }
void UAdvancedSessionsLibrary::GetSessionState(TEnumAsByte<EBPOnlineSessionState::Type> &SessionState) { IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(); if (!SessionInterface.IsValid()) { UE_LOG(AdvancedSessionsLog, Warning, TEXT("GetSessionState couldn't get the session interface!")); return; } SessionState = ((EBPOnlineSessionState::Type)SessionInterface->GetSessionState(GameSessionName)); }
bool AQosBeaconHost::DoesSessionMatch(const FString& SessionId) const { UWorld* World = GetWorld(); IOnlineSessionPtr SessionInt = Online::GetSessionInterface(World); FNamedOnlineSession* Session = SessionInt.IsValid() ? SessionInt->GetNamedSession(SessionName) : NULL; if (Session && Session->SessionInfo.IsValid() && !SessionId.IsEmpty() && Session->SessionInfo->GetSessionId().ToString() == SessionId) { return true; } return false; }
void APlayerState::RegisterPlayerWithSession(bool bWasFromInvite) { if (GetNetMode() != NM_Standalone) { IOnlineSessionPtr SessionInt = Online::GetSessionInterface(GetWorld()); if (SessionInt.IsValid() && UniqueId.IsValid()) { // Register the player as part of the session const APlayerState* PlayerState = GetDefault<APlayerState>(); SessionInt->RegisterPlayer(PlayerState->SessionName, *UniqueId, bWasFromInvite); } } }
void UAdvancedSessionsLibrary::IsPlayerInSession(const FBPUniqueNetId &PlayerToCheck, bool &bIsInSession) { IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(); if (!SessionInterface.IsValid()) { UE_LOG(AdvancedSessionsLog, Warning, TEXT("IsPlayerInSession couldn't get the session interface!")); bIsInSession = false; return; } bIsInSession = SessionInterface->IsPlayerInSession(GameSessionName, *PlayerToCheck.GetUniqueNetId()); }
/** * Function to host a game! * * @Param UserID User that started the request * @Param SessionName Name of the Session * @Param bIsLAN Is this is LAN Game? * @Param bIsPresence "Is the Session to create a presence Session" * @Param MaxNumPlayers Number of Maximum allowed players on this "Session" (Server) */ bool URadeGameInstance::HostSession(TSharedPtr<const FUniqueNetId> UserId, FName SessionName, FString MapName, bool bIsLAN, bool bIsPresence, int32 MaxNumPlayers) { // Get the Online Subsystem to work with IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::Get(); if (OnlineSub) { // Get the Session Interface, so we can call the "CreateSession" function on it IOnlineSessionPtr Sessions = OnlineSub->GetSessionInterface(); if (Sessions.IsValid() && UserId.IsValid()) { /* Fill in all the Session Settings that we want to use. There are more with SessionSettings.Set(...); For example the Map or the GameMode/Type. */ SessionSettings = MakeShareable(new FOnlineSessionSettings()); SessionSettings->bIsLANMatch = bIsLAN; SessionSettings->bUsesPresence = bIsPresence; SessionSettings->NumPublicConnections = MaxNumPlayers; SessionSettings->NumPrivateConnections = 0; SessionSettings->bAllowInvites = true; SessionSettings->bAllowJoinInProgress = true; SessionSettings->bShouldAdvertise = true; SessionSettings->bAllowJoinViaPresence = true; SessionSettings->bAllowJoinViaPresenceFriendsOnly = false; SessionSettings->Set(SETTING_MAPNAME, MapName, EOnlineDataAdvertisementType::ViaOnlineService); // Set the delegate to the Handle of the SessionInterface OnCreateSessionCompleteDelegateHandle = Sessions->AddOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegate); //printr(SessionName.ToString()); // Our delegate should get called when this is complete (doesn't need to be successful!) return Sessions->CreateSession(*UserId, SessionName, *SessionSettings); } } else { //GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, TEXT("No OnlineSubsytem found!")); } return false; }
void UAdvancedFriendsLibrary::SendSessionInviteToFriends(APlayerController *PlayerController, const TArray<FBPUniqueNetId> &Friends, TEnumAsByte<EBlueprintResultSwitch::Type> &Result) { if (!PlayerController) { UE_LOG(AdvancedFriendsLog, Warning, TEXT("SendSessionInviteToFriend Had a bad Player Controller!")); Result = EBlueprintResultSwitch::Type::OnFailure; return; } if (Friends.Num() < 1) { UE_LOG(AdvancedFriendsLog, Warning, TEXT("SendSessionInviteToFriend Had no friends in invitation array!")); Result = EBlueprintResultSwitch::Type::OnFailure; return; } IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(); if (!SessionInterface.IsValid()) { UE_LOG(AdvancedFriendsLog, Warning, TEXT("SendSessionInviteToFriend Failed to get session interface!")); Result = EBlueprintResultSwitch::Type::OnFailure; return; } ULocalPlayer* Player = Cast<ULocalPlayer>(PlayerController->Player); if (!Player) { UE_LOG(AdvancedFriendsLog, Warning, TEXT("SendSessionInviteToFriend failed to get LocalPlayer!")); Result = EBlueprintResultSwitch::Type::OnFailure; return; } TArray<TSharedRef<const FUniqueNetId>> List; for (int i = 0; i < Friends.Num(); i++) { TSharedRef<const FUniqueNetId> val(Friends[i].UniqueNetId.ToSharedRef()); //TSharedRef<const FUniqueNetId> val(Friends[i].GetUniqueNetId()); List.Add(val); } if (SessionInterface->SendSessionInviteToFriends(Player->GetControllerId(), GameSessionName, List)) { Result = EBlueprintResultSwitch::Type::OnSuccess; return; } Result = EBlueprintResultSwitch::Type::OnFailure; return; }
void UtrnetDemoGameInstance::FindSessions(TSharedPtr<const FUniqueNetId> UserId, FName SessionName, bool bIsLAN, bool bIsPresence) { isLoading_ = true; // Get the SessionInterface from our OnlineSubsystem IOnlineSessionPtr Sessions = GetSession(); if (Sessions.IsValid() && Sessions.IsValid() && UserId.IsValid()) { /* Fill in all the SearchSettings, like if we are searching for a LAN game and how many results we want to have! */ SessionSearch = MakeShareable(new FOnlineSessionSearch()); SessionSearch->bIsLanQuery = bIsLAN; SessionSearch->MaxSearchResults = 20; SessionSearch->PingBucketSize = 50; // We only want to set this Query Setting if "bIsPresence" is true if (bIsPresence) { SessionSearch->QuerySettings.Set(SEARCH_PRESENCE, bIsPresence, EOnlineComparisonOp::Equals); } TSharedRef<FOnlineSessionSearch> SearchSettingsRef = SessionSearch.ToSharedRef(); // Set the Delegate to the Delegate Handle of the FindSession function OnFindSessionsCompleteDelegateHandle = Sessions->AddOnFindSessionsCompleteDelegate_Handle(OnFindSessionsCompleteDelegate); // Finally call the SessionInterface function. The Delegate gets called once this is finished if(!Sessions->FindSessions(*UserId, SearchSettingsRef)) { OnFindSessionsComplete(false); } } else { // If something goes wrong, just call the Delegate Function directly with "false". OnFindSessionsComplete(false); } }
void UOnlineSessionClient::RegisterOnlineDelegates() { OnJoinSessionCompleteDelegate = FOnJoinSessionCompleteDelegate ::CreateUObject(this, &ThisClass::OnJoinSessionComplete); OnEndForJoinSessionCompleteDelegate = FOnEndSessionCompleteDelegate ::CreateUObject(this, &ThisClass::OnEndForJoinSessionComplete); OnDestroyForJoinSessionCompleteDelegate = FOnDestroySessionCompleteDelegate::CreateUObject(this, &ThisClass::OnDestroyForJoinSessionComplete); OnDestroyForMainMenuCompleteDelegate = FOnDestroySessionCompleteDelegate::CreateUObject(this, &ThisClass::OnDestroyForMainMenuComplete); OnSessionUserInviteAcceptedDelegate = FOnSessionUserInviteAcceptedDelegate::CreateUObject(this, &ThisClass::OnSessionUserInviteAccepted); IOnlineSessionPtr SessionInt = GetSessionInt(); if (SessionInt.IsValid()) { OnSessionUserInviteAcceptedDelegateHandle = SessionInt->AddOnSessionUserInviteAcceptedDelegate_Handle(OnSessionUserInviteAcceptedDelegate); } }
void UOnlineSessionClient::EndOnlineSession(FName SessionName) { IOnlineSessionPtr SessionInterface = Online::GetSessionInterface(GetWorld()); if (SessionInterface.IsValid()) { FNamedOnlineSession* Session = SessionInterface->GetNamedSession(SessionName); if (Session && Session->SessionState == EOnlineSessionState::InProgress) { EndSessionCompleteHandle = SessionInterface->AddOnEndSessionCompleteDelegate_Handle(FOnStartSessionCompleteDelegate::CreateUObject(this, &UOnlineSessionClient::OnEndSessionComplete)); SessionInterface->EndSession(SessionName); } } }