Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- #include "EOS_OSS_Tutorial/Public/EosHandler.h"
- #include "OnlineSessionSettings.h"
- #include "OnlineSubsystem.h"
- #include "OnlineSubsystemUtils.h"
- #include "Online/OnlineSessionNames.h"
- void UEosHandler::Login() const
- {
- Subsystem = Online::GetSubsystem(GetWorld());
- if(Subsystem == nullptr)
- {
- PrintToScreen("No online subsystem found!");
- return;
- }
- const FString Message = FString::Printf(TEXT("Online subsystem found Name is %ls"), *Subsystem->GetSubsystemName().ToString());
- PrintToScreen(Message);
- Identity = Subsystem->GetIdentityInterface();
- if(!Identity.IsValid())
- {
- PrintToScreen("No identity found!");
- return;
- }
- if(const FUniqueNetIdPtr NetId = Identity.Pin()->GetUniquePlayerId(0))
- if(Identity.Pin()->GetLoginStatus(0) == ELoginStatus::LoggedIn)
- return;
- LoginDelegateHandle = Identity.Pin()->AddOnLoginCompleteDelegate_Handle(0, FOnLoginCompleteDelegate::CreateUObject(this, &ThisClass::HandleLoginCompleted));
- FOnlineAccountCredentials Credentials("AccountPortal","", "");
- PrintToScreen("Logging into EOS...");
- if(!Identity.Pin()->Login(0, Credentials))
- {
- PrintToScreen("Failed to login... ");
- // Clear our handle and reset the delegate.
- Identity.Pin()->ClearOnLoginCompleteDelegate_Handle(0, LoginDelegateHandle);
- LoginDelegateHandle.Reset();
- }
- }
- void UEosHandler::HandleLoginCompleted(const int32 LocalUserNum, const bool bWasSuccessful, const FUniqueNetId& UserId, const FString& Error)
- {
- if (bWasSuccessful)
- {
- PrintToScreen("Login callback completed!");
- }
- else //Login failed
- {
- // If your game is online only, you may want to return an error to the user and return to a menu that uses a different GameMode/PlayerController.
- PrintToScreen("EOS login failed.");
- }
- // Clear our handle and reset the delegate.
- Identity.Pin()->ClearOnLoginCompleteDelegate_Handle(LocalUserNum, LoginDelegateHandle);
- LoginDelegateHandle.Reset();
- }
- void UEosHandler::FindSessions(const FName SearchKey, const FString& SearchValue)
- {
- if(Subsystem == nullptr)
- {
- PrintToScreen("No online subsystem found!");
- return;
- }
- Session = Subsystem->GetSessionInterface();
- if(!Session.IsValid())
- {
- PrintToScreen("No Session found!");
- return;
- }
- TSharedRef<FOnlineSessionSearch> Search = MakeShared<FOnlineSessionSearch>();
- // Remove the default search parameters that FOnlineSessionSearch sets up.
- Search->QuerySettings.SearchParams.Empty();
- Search->QuerySettings.Set(SearchKey, SearchValue, EOnlineComparisonOp::Equals); // Seach using our Key/Value pair
- Search->QuerySettings.Set(SEARCH_LOBBIES, true, EOnlineComparisonOp::Equals);
- FindSessionsDelegateHandle = Session.Pin()->AddOnFindSessionsCompleteDelegate_Handle(
- FOnFindSessionsCompleteDelegate::CreateUObject(this, &ThisClass::HandleFindSessionsCompleted, Search));
- PrintToScreen("Finding lobby.");
- if (!Session.Pin()->FindSessions(0, Search))
- {
- PrintToScreen("Finding lobby failed.");
- // Clear our handle and reset the delegate.
- Session.Pin()->ClearOnFindSessionsCompleteDelegate_Handle(FindSessionsDelegateHandle);
- FindSessionsDelegateHandle.Reset();
- }
- }
- // ReSharper disable once CppPassValueParameterByConstReference
- void UEosHandler::HandleFindSessionsCompleted(const bool bWasSuccessful, TSharedRef<FOnlineSessionSearch> Search)
- {
- if (bWasSuccessful)
- {
- // added code here to not run into issues when searching for sessions is successful, but the number of sessions is 0
- if (Search->SearchResults.Num() == 0)
- {
- // CreateLobby();
- return;
- }
- PrintToScreen("Found lobby.");
- for(auto SessionInSearchResult : Search->SearchResults)
- {
- //Ensure the connection string is resolvable and store the info in ConnectString and in SessionToJoin
- if (Session.Pin()->GetResolvedConnectString(SessionInSearchResult, NAME_GamePort, ConnectString))
- {
- SessionToJoin = &SessionInSearchResult;
- }
- // For this course we will join the first session found automatically. Usually you would loop through all the sessions and determine which one is best to join.
- break;
- }
- JoinSession();
- }
- else
- {
- PrintToScreen("Find lobby failed.");
- }
- // Clear our handle and reset the delegate.
- Session.Pin()->ClearOnFindSessionsCompleteDelegate_Handle(FindSessionsDelegateHandle);
- FindSessionsDelegateHandle.Reset();
- }
- void UEosHandler::CreateLobby(const FName KeyName, FString KeyValue)
- {
- Session = Subsystem->GetSessionInterface();
- if(!Session.IsValid())
- {
- PrintToScreen("No Session found!");
- return;
- }
- CreateLobbyDelegateHandle = Session.Pin()->AddOnCreateSessionCompleteDelegate_Handle(FOnCreateSessionCompleteDelegate::CreateUObject(this, &ThisClass::HandleCreateLobbyCompleted));
- const TSharedRef<FOnlineSessionSettings> SessionSettings = MakeShared<FOnlineSessionSettings>();
- SessionSettings->NumPublicConnections = 5; //We will test our sessions with 2 players to keep things simple
- SessionSettings->bShouldAdvertise = true; //This creates a public match and will be searchable.
- SessionSettings->bUsesPresence = true; //No presence on dedicated server. This requires a local user.
- SessionSettings->bAllowJoinViaPresence = true;
- SessionSettings->bAllowJoinViaPresenceFriendsOnly = false;
- SessionSettings->bAllowInvites = true; //Allow inviting players into session. This requires presence and a local user.
- SessionSettings->bAllowJoinInProgress = true; //Once the session is started, no one can join.
- SessionSettings->bIsDedicated = false; //Session created on dedicated server.
- SessionSettings->bUseLobbiesIfAvailable = true; //For P2P we will use a lobby instead of a session
- SessionSettings->bUseLobbiesVoiceChatIfAvailable = true; //We will also enable voice
- SessionSettings->bUsesStats = true; //Needed to keep track of player stats.
- SessionSettings->Settings.Add(KeyName, FOnlineSessionSetting((KeyValue), EOnlineDataAdvertisementType::ViaOnlineService));
- PrintToScreen("Creating Lobby...");
- if(!Session.Pin()->CreateSession(0, LobbyName, *SessionSettings))
- {
- PrintToScreen("Failed to create Lobby!");
- }
- }
- void UEosHandler::HandleCreateLobbyCompleted(const FName EosLobbyName, const bool bWasSuccessful)
- {
- if(!Session.IsValid())
- {
- PrintToScreen("No Session found!");
- return;
- }
- if(bWasSuccessful)
- {
- PrintToScreen(FString::Printf(TEXT("Lobby: %ls Created!"), *EosLobbyName.ToString()));
- FURL TravelURL;
- TravelURL.Map = Map;
- GetWorld()->Listen(TravelURL);
- }
- else
- {
- PrintToScreen("Failed to create lobby!");
- }
- // Clear our handle and reset the delegate.
- Session.Pin()->ClearOnCreateSessionCompleteDelegate_Handle(CreateLobbyDelegateHandle);
- CreateLobbyDelegateHandle.Reset();
- }
- void UEosHandler::JoinSession()
- {
- if(!Session.IsValid())
- {
- PrintToScreen("No Session found!");
- return;
- }
- JoinSessionDelegateHandle = Session.Pin()->AddOnJoinSessionCompleteDelegate_Handle(FOnJoinSessionCompleteDelegate::CreateUObject(this, &ThisClass::HandleJoinSessionCompleted));
- PrintToScreen("Joining Lobby.");
- if(!Session.Pin()->JoinSession(0, "SessionName", *SessionToJoin))
- {
- PrintToScreen("Join Lobby failed.");
- // Clear our handle and reset the delegate.
- Session.Pin()->ClearOnJoinSessionCompleteDelegate_Handle(JoinSessionDelegateHandle);
- JoinSessionDelegateHandle.Reset();
- }
- }
- void UEosHandler::HandleJoinSessionCompleted(FName SessionName, const EOnJoinSessionCompleteResult::Type Result)
- {
- if(!Session.IsValid())
- {
- PrintToScreen("No Session found!");
- return;
- }
- if(Result == EOnJoinSessionCompleteResult::Success)
- {
- PrintToScreen("Joined lobby.");
- if(APlayerController* Pc = GetWorld()->GetFirstPlayerController())
- Pc->ClientTravel(ConnectString, TRAVEL_Absolute);
- }
- // Clear our handle and reset the delegate.
- Session.Pin()->ClearOnJoinSessionCompleteDelegate_Handle(JoinSessionDelegateHandle);
- JoinSessionDelegateHandle.Reset();
- }
Advertisement
Add Comment
Please, Sign In to add comment