Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool UPongGameInstance::HostGameSession(
- TSharedPtr<const FUniqueNetId> UserId,
- FName SessionName,
- bool bIsLAN,
- bool bIsPresence,
- int32 MaxNumPlayers
- )
- {
- //The first thing to do when working with online subsystems
- const IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
- if(OnlineSubsystem)
- {
- //Is necessary to obtain a session interface to do stuff here
- IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
- if(SessionInterface.IsValid() && UserId.IsValid())
- {
- SessionSettings = MakeShareable(new FOnlineSessionSettings());
- SessionSettings->bIsLANMatch = bIsLAN;
- SessionSettings->bUsesPresence = bIsPresence;
- SessionSettings->NumPublicConnections = MaxNumPlayers;
- SessionSettings->NumPrivateConnections = 0;
- SessionSettings->bAllowInvites = true;
- SessionSettings->bAllowJoinInProgress = true;
- SessionSettings->bAllowJoinViaPresence = true;
- SessionSettings->bAllowJoinViaPresenceFriendsOnly = false;
- SessionSettings->bShouldAdvertise = true;
- SessionSettings->Set(
- SETTING_MAPNAME,
- FString("PongMap"),
- EOnlineDataAdvertisementType::ViaOnlineService
- );
- //Indicates the delegate to be called when the session creation is completed
- OnCreateSessionCompleteDelegateHandle = SessionInterface->
- AddOnCreateSessionCompleteDelegate_Handle(OnCreateSessionCompleteDelegate);
- //The delegate should be called after the session creation
- return SessionInterface->CreateSession(*UserId, SessionName, *SessionSettings);
- }
- }
- WarnsAboutLackOfSubsystem();
- return false;
- }
- void UPongGameInstance::OnCreateSessionComplete(FName SessionName, bool bWasSuccessful)
- {
- GEngine->AddOnScreenDebugMessage(
- -1,
- 10.f,
- FColor::Yellow,
- FString::Printf(
- TEXT("The %s creation %s."),
- *SessionName.ToString(),
- bWasSuccessful ? *FString(TEXT("succeeded")) : *FString(TEXT("failed"))
- )
- );
- //The first thing to do when working with online subsystems
- const IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
- if(OnlineSubsystem)
- {
- //Is necessary to obtain a session interface to do stuff here
- IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
- if(SessionInterface.IsValid())
- {
- //Must clean the handle when the session creation is done, to use it again
- SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(
- OnCreateSessionCompleteDelegateHandle
- );
- if(bWasSuccessful)
- {
- //Indicates the delegate to be called when the session is started
- OnStartSessionCompleteDelegateHandle = SessionInterface->
- AddOnStartSessionCompleteDelegate_Handle(OnStartSessionCompleteDelegate);
- //The delegate should be called after the session start
- SessionInterface->StartSession(SessionName);
- return;
- }
- }
- }
- WarnsAboutLackOfSubsystem();
- }
- void UPongGameInstance::OnStartSessionComplete(FName SessionName, bool bWasSuccessful)
- {
- GEngine->AddOnScreenDebugMessage(
- -1,
- 10.f,
- FColor::Yellow,
- FString::Printf(
- TEXT("The %s initialization %s."),
- *SessionName.ToString(),
- bWasSuccessful ? *FString(TEXT("succeeded")) : *FString(TEXT("failed"))
- )
- );
- //The first thing to do when working with online subsystems
- const IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
- if(OnlineSubsystem)
- {
- //Is necessary to obtain a session interface to do stuff here
- IOnlineSessionPtr SessionInterface = OnlineSubsystem->GetSessionInterface();
- if(SessionInterface.IsValid())
- {
- //Must clean the handle when the session initialization is done, to use it again
- SessionInterface->ClearOnStartSessionCompleteDelegate_Handle(
- OnStartSessionCompleteDelegateHandle
- );
- if (bWasSuccessful)
- {
- UGameplayStatics::OpenLevel(
- GetWorld(),
- "PongMap?listen",
- true,
- FString::Printf(
- TEXT("?%s"),
- SessionSettings->bIsLANMatch ?
- TEXT("bIsLanMatch=1") : TEXT("bIsLanMatch=0")
- )
- );
- }
- return;
- }
- }
- WarnsAboutLackOfSubsystem();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement