ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 온라인 서브시스템과 세션 생성, 찾기
    Unreal Engine 2024. 4. 29. 00:00

     

    https://dev.epicgames.com/documentation/ko-kr/unreal-engine/online-subsystem-in-unreal-engine?application_version=5.1

     

    온라인 서브시스템

    온라인 플랫폼 관련 다양한 서브시스템 개요입니다.

    dev.epicgames.com

     

    Online Subsystem (온라인 서브시스템) 및 그 인터페이스는 Steam, Xbox Live, Facebook 등의 온라인 서비스 기능을 공통된 방식으로 액세스할 수 있는 방법을 제공합니다. 여러 플랫폼에 출시하거나 여러 온라인 서비스를 지원하는 게임을 만들 때, 온라인 서브시스템을 사용하면 개발자는 각 지원 서비스에 맞게 구성만 조정해 주면 됩니다.

     

    생성자

    IOnlineSubsystem* OnlineSubsystem = IOnlineSubsystem::Get();
    
    if (OnlineSubsystem)
    {
    	OnlineSessionInterface = OnlineSubsystem->GetSessionInterface();
    }

     

    BeginPlay()

    void ASessionManager::BeginPlay()
    {
    	Super::BeginPlay();
    
    	OnlineSessionInterface->OnCreateSessionCompleteDelegates.AddUObject(this, &ASessionManager::OnCreateSessionComplete);
    	OnlineSessionInterface->OnFindSessionsCompleteDelegates.AddUObject(this, &ASessionManager::OnFindSessionComplete);
    	OnlineSessionInterface->OnJoinSessionCompleteDelegates.AddUObject(this, &ASessionManager::OnJoinSessionComplete);
    	OnlineSessionInterface->OnDestroySessionCompleteDelegates.AddUObject(this, &ASessionManager::OnDestroySessionComplete);
    	OnlineSessionInterface->OnStartSessionCompleteDelegates.AddUObject(this, &ASessionManager::OnStartSessionComplete);
    }

     

    Delegate로 Callback 함수를 지정해 주면 정상적으로 작업을 완료했는지 알 수 있음

     

     

    CreateSession()

    auto ExistingSession = OnlineSessionInterface->GetNamedSession(SESSION_NAME);
    if (ExistingSession != nullptr)
    {
    	OnlineSessionInterface->DestroySession(SESSION_NAME);
    }
    else
    {
    	OnlineSessionInterface->AddOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate);
    
    	FOnlineSessionSettings SessionSettings;
    	SessionSettings.bIsLANMatch = true;
    	SessionSettings.NumPublicConnections = 2;
    	SessionSettings.bShouldAdvertise = true;
    
    	OnlineSessionInterface->CreateSession(0, SESSION_NAME, SessionSettings);
    }

     

    FindSession()

    SessionSearch = MakeShareable(new FOnlineSessionSearch());
    if (SessionSearch.IsValid()) {
    	// SessionSearch->bIsLanQuery = true;
    	OnlineSessionInterface->FindSessions(0, SessionSearch.ToSharedRef());
    
    	UE_LOG(LogTemp, Warning, TEXT("Starting Find Session"));
    }

     

    Delegates

    void ASessionManager::OnCreateSessionComplete(FName _SessionName, bool bWasSuccessful)
    {
    	if (bWasSuccessful)
    	{
    		UWorld* world = GetWorld();
    		if (world)
    		{
    			world->ServerTravel(FString("/Game/BasicMap?listen"));
    		}
    	}
    	else UE_LOG(LogTemp, Warning, TEXT("Failed to Create Session"));
    }
    
    void ASessionManager::OnFindSessionComplete(bool bWasSuccessful)
    {
    	if (bWasSuccessful && SessionSearch.IsValid() && TestWidget != nullptr) {
    		UE_LOG(LogTemp, Warning, TEXT("Finished Find Session"));
    		TArray<FString> ServerNames;
    
    		for (const FOnlineSessionSearchResult& t : SessionSearch->SearchResults) {
    			UE_LOG(LogTemp, Warning, TEXT("Found Session Names: %s"), *t.GetSessionIdStr());
    			ServerNames.Add(*t.GetSessionIdStr());
    		}
    
    		TestWidget->SetServerList(ServerNames);
    	}
    }
    
    void ASessionManager::OnJoinSessionComplete(FName _SessionName, EOnJoinSessionCompleteResult::Type Result)
    {
    
    }
    
    void ASessionManager::OnDestroySessionComplete(FName _SessionName, bool bWasSuccessful)
    {
    	if (bWasSuccessful) CreateGameSession();
    }
    
    void ASessionManager::OnStartSessionComplete(FName _SessionName, bool bWasSuccessful)
    {
    }
Designed by Tistory.