플러그인 UI - 1

개발 일지|2024. 12. 1. 21:16

플러그인 UI 동작 구현

  • HostButton과 JoinButton을 UserWidget 클래스파일의 헤더로 가져온다
  • UPROPERTY()의 meta=bindwidget 속성을 주면 변수의 이름을 UI 버튼 변수명과 완전히 일치하게 설정 -> UI의 버튼에 접근할 수 있게 된다
  • 각 버튼의 콜백 함수 작성
protected:
	virtual bool Initialize() override;

private:
	UPROPERTY(meta=(BindWidget))
	class UButton* HostButton;

	UPROPERTY(meta = (BindWidget))
	UButton* JoinButton;

	UFUNCTION()
	void HostButtonClicked();

	UFUNCTION()
	void JoinButtonClicked();

 

  • 이후 cpp 파일에서 버튼 클릭 콜백 함수 테스트용 디버그 로직과 initialize 함수에 AddDynamic으로 콜백함수 바인딩
bool UMenuUI::Initialize()
{
	if (!Super::Initialize()) {
		return false;
	}

	if (HostButton) {
		HostButton->OnClicked.AddDynamic(this, &UMenuUI::HostButtonClicked);
	}
	if (JoinButton) {
		JoinButton->OnClicked.AddDynamic(this, &UMenuUI::JoinButtonClicked);
	}

	return true;
}

void UMenuUI::HostButtonClicked()
{
	if (GEngine) {
		GEngine->AddOnScreenDebugMessage(
			-1,
			15.f,
			FColor::Yellow,
			FString(TEXT("Host Button Clicked"))
		);
	}
}

void UMenuUI::JoinButtonClicked()
{
	if (GEngine) {
		GEngine->AddOnScreenDebugMessage(
			-1,
			15.f,
			FColor::Yellow,
			FString(TEXT("Join Button Clicked"))
		);
	}
}
  • Initialize 함수의 로직을 생성자에 작성하면, 위젯이 생성되기 전 너무 이른 시점에 로직이 작동하기 때문에 오류가 발생하므로 Initialize 함수에 작성 -> Initialize 함수는 위젯이 생성된 후 작동

MultiplayerSessionSubsystem 함수 구현

  • 이제 UI에 매핑될 함수 내부를 구현한다.
  • 지난번 함수 선언만 해뒀던 MultiplayerSessionSubsystem으로 가 cpp에 함수 기능 구현

CreateSession

  • SessionSettings의 bIsLanMatch 옵션을 삼항연산자로 true와 false 중 상황에 따라 적용되도록 수정
    • NULL일때 true, Steam일때 false
  • 세션 생성에 실패시 등록된 델리게이트 제거
void UMultiplayerSessionsSubsystem::CreateSession(int32 NumPublicConnections, FString MatchType)
{
	if (!SessionInterface.IsValid()) {
		return;
	}

	auto ExistingSession = SessionInterface->GetNamedSession(NAME_GameSession);
	if (ExistingSession != nullptr) {
		SessionInterface->DestroySession(NAME_GameSession);
	}

	// Store the Delegate in a FDelegateHandle so we can later remove it from the delegate list
	CreateSessionCompleteDelegateHandle = SessionInterface->AddOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegate);

	LastSessionSettings = MakeShareable(new FOnlineSessionSettings());
	// Subsystem == Steam -> true, NULL -> false
	LastSessionSettings->bIsLANMatch = IOnlineSubsystem::Get()->GetSubsystemName() == "NULL" ? true : false;
	LastSessionSettings->NumPublicConnections = NumPublicConnections;
	LastSessionSettings->bAllowJoinInProgress = true;
	LastSessionSettings->bAllowJoinViaPresence = true;
	LastSessionSettings->bShouldAdvertise = true;
	LastSessionSettings->bUsesPresence = true;
	LastSessionSettings->Set(FName("MatchType"), MatchType, EOnlineDataAdvertisementType::ViaOnlineServiceAndPing);

	const ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController();
	if (!SessionInterface->CreateSession(*LocalPlayer->GetPreferredUniqueNetId(), NAME_GameSession, *LastSessionSettings)) {
		SessionInterface->ClearOnCreateSessionCompleteDelegate_Handle(CreateSessionCompleteDelegateHandle);
	}

}
  • MenuUI에서 Host 버튼 클릭시 최종적으로 Listen 옵션으로 Lobby맵으로 이동하도록 구현
void UMenuUI::HostButtonClicked()
{
	if (GEngine) {
		GEngine->AddOnScreenDebugMessage(
			-1,
			15.f,
			FColor::Yellow,
			FString(TEXT("Host Button Clicked"))
		);
	}
	if (MultiplayerSessionsSubsystem) {
		MultiplayerSessionsSubsystem->CreateSession(4, FString("FreeForAll"));
		UWorld* World = GetWorld();
		if (World) {
			World->ServerTravel("/Game/FirstPerson/Maps/Lobby?listen");
		}
	}
}
  • 이후 UI 전용으로 설정되었던 InputMode를 GameOnly로 변경하는 함수 구현
  • MenuTearDown() 함수를 레벨 이동 시점에 호출하도록 NativeDestruct() 함수 override

void UMenuUI::NativeDestruct()
{
	MenuTearDown();

	Super::NativeDestruct();
}

void UMenuUI::MenuTearDown()
{
	RemoveFromParent();
	UWorld* World = GetWorld();
	if (World) {
		APlayerController* PlayerController = World->GetFirstPlayerController();
		if (PlayerController) {
			FInputModeGameOnly InputModeData;
			PlayerController->SetInputMode(InputModeData);
			PlayerController->SetShowMouseCursor(false);
		}
	}
}
  • 이후 테스트 해보면 정상적으로 세션 생성이 완료되는것 확인 가능

'개발 일지' 카테고리의 다른 글

Tracking Player  (0) 2024.12.03
플러그인 UI - 2  (0) 2024.12.02
플러그인 등록  (0) 2024.11.30
Join Session  (0) 2024.11.29
OnlineSubsystem Steam 연결 및 Create Session  (0) 2024.11.27

댓글()