🎮 APropBase.h
class UStaticMeshComponent; UCLASS() class BOSSFIGHT_API APropBase : public AActor { GENERATED_BODY() protected: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components") UStaticMeshComponent* Mesh; public: // Sets default values for this actor's properties APropBase(); UFUNCTION(BlueprintNativeEvent) void ActivateProp(); };🎮 APropBase.cpp
APropBase::APropBase() { PrimaryActorTick.bCanEverTick = false; Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh")); RootComponent = Mesh; } void APropBase::ActivateProp_Implementation() { // Default implementation does nothing }간단하게 스태틱 메쉬와 작동시 간단한 애니메이션이나, 특정조건에 Effect를 출력하는등 다용도롤 활용할수 있는 ActivateProp() 함수를BlueprintNativeEvent로 선언 하였다.
🎮 UInteractablePropInterface.h
// This class does not need to be modified. UINTERFACE(MinimalAPI) class UInteractablePropInterface : public UInterface { GENERATED_BODY() }; class BOSSFIGHT_API IInteractablePropInterface { GENERATED_BODY() public: virtual void Interact() = 0; };해당 Interface를 소유한 객체는 상호 작용 할 수 있게 된다.
🎮 AInteractableProp.h
class UBoxComponent; UCLASS() class BOSSFIGHT_API AInteractableProp : public APropBase, public IInteractablePropInterface { GENERATED_BODY() public: AInteractableProp(); virtual void Interact() override; protected: UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Collision") UBoxComponent* CollisionBox; void BeginPlay() override; };🎮 AInteractableProp.cpp
AInteractableProp::AInteractableProp() { CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox")); CollisionBox->SetupAttachment(GetRootComponent()); } void AInteractableProp::Interact() { ActivateProp(); } void AInteractableProp::BeginPlay() { Super::AActor::BeginPlay(); }간단하게 콜리전 Box를 추가하고, Interface에 선언한 함수를 정의 해주었다.




🎮 ABFPlayerCharacter.cpp
void ABFPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { //중략 BFInputComponent->BindNativeInputAction(InputConfigDataAsset, BFGameplayTag::InputTag_Interaction, ETriggerEvent::Started, this, &ThisClass::Input_Interection); } void ABFPlayerCharacter::Input_Interection() { FHitResult HitResult; UKismetSystemLibrary::SphereTraceSingleForObjects ( GetWorld(), GetActorLocation(), GetActorLocation(), InteractionDistance, ObjectType, false, TArray<AActor*>(), bShowPersistenDebugShape ? EDrawDebugTrace::Persistent : EDrawDebugTrace::None, HitResult, true ); if (HitResult.GetActor()) { if (IInteractablePropInterface* InteractableProp = Cast<IInteractablePropInterface>(HitResult.GetActor())) { InteractableProp->Interact(); } else { return; } } else { return; } }상호작용 키를 누르면 SphereTraceSingleForObjects()함수가 작동하여 Trace 실행하고, 반응한 객체가 있으면 DIP원칙에 따라 IInteractablePropInterface로 형변환 하여,Interact()함수를 실행 시킨다.
