안내 UI("E를 눌러 상호작용")는 콜리전 범위 진입/이탈로 켜지고 꺼지는데 편지 내용 위젯은 E키로 뜨고 닫힌다.

이 둘은 독립적으로 동작하고 있었고 그래서 편지가 펼쳐진 상태에서도 안내 UI가 겹쳐서 그 위에 계속 떠있는 문제가 생겼다

 

1. DarkLetter.h에 위젯 관련 변수 추가

class ULetterWidget;  // 전방 선언

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Letter")
TSubclassOf<ULetterWidget> LetterWidgetClass;   

UPROPERTY()
ULetterWidget* LetterWidgetInstance;

 

 

 

2. ActivateItem을 토글 로직으로 구현

void ADarkLetter::ActivateItem(AActor* Activator)
{
    if (Activator && Activator->ActorHasTag("Player"))
    {
        ATheRoomCharacter* PlayerCharacter = Cast<ATheRoomCharacter>(Activator);
        if (!PlayerCharacter) return;

        if (LetterWidgetInstance)
        {
            // 이미 떠있음 → 닫기
            LetterWidgetInstance->RemoveFromParent();
            LetterWidgetInstance = nullptr;
            PlayerCharacter->ShowInteraction();   // 안내 UI 다시 켜기
        }
        else if (LetterWidgetClass)
        {
            // 안 떠있음 → 띄우기
            LetterWidgetInstance = CreateWidget<ULetterWidget>(GetWorld(), LetterWidgetClass);
            LetterWidgetInstance->AddToViewport();
            PlayerCharacter->HideInteraction();   // 안내 UI 끄기
        }
    }
}

 

 

Activator는 AActor* 타입으로 들어오기 때문에 ShowInteraction이나 HideInteraction 같은 캐릭터 전용 함수를 호출하려면 ATheRoomCharacter로 캐스팅하는 과정이 필요하다.

 

LetterWidgetInstance의 존재 여부로 분기해서 떠있으면 닫고 안내 UI를 켜고, 닫혀있으면 띄우고 안내 UI를 끈다.

 

에디터에서는 BP_DarkLetter의 LetterWidgetClass 슬롯에 WBP_DarkLetter를 연결해뒀다

 

+ Recent posts