-
온라인 서브시스템과 세션 생성, 찾기Unreal Engine 2024. 4. 29. 00:00
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) { }
'Unreal Engine' 카테고리의 다른 글
Unreal Engine5 LNK 외부 참조 에러 대부분 이걸로 해결 (0) 2024.05.09 Gameplay Ability System (0) 2024.04.30 캐릭터의 HP (ActorComponent) Replicate (0) 2024.04.23 네트워크 환경에서 공격과 피격 구현 (0) 2024.04.23 4 principles for network multiplay (0) 2024.04.23