




< Ability에 필요한 기능 >
1. 무기를 Spawn 및 부착하는 기능 ✔
2. 이미 부착된 무기가 있으면 해당 무기를 제거. ✔
3. 장착한 무기를 캐릭터에게 등록 ✔
4. 부착된 무기에 따라 InputMappingContext 변경
5. 부착된 무기에 따라 애니메이션 변경
🎮 AWeaponBase.h
class UBoxComponent; UCLASS() class BOSSFIGHT_API AWeaponBase : public APropBase { GENERATED_BODY() public: AWeaponBase(); FORCEINLINE EBFWeaponType GetWeaponType() const { return WeaponType; } protected: UPROPERTY(EditDefaultsOnly, BlueprintReadOnly) EBFWeaponType WeaponType; UPROPERTY(EditDefaultsOnly, BlueprintReadOnly) UBoxComponent* WeaponCollision; UPROPERTY(EditDefaultsOnly, BlueprintReadOnly) FName WeaponSocket; };🎮 AWeaponBase.cpp
AWeaponBase::AWeaponBase() { WeaponCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("WeaponCollision")); WeaponCollision->SetupAttachment(GetRootComponent()); WeaponCollision->SetCollisionProfileName(TEXT("Weapon")); WeaponCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision); Mesh->SetCollisionEnabled(ECollisionEnabled::NoCollision); }다음과 같이Collision을 추가하고, 프리셋을 설정 해 주었다.
🎮 UPawnEquipmentComponent.h
class AWeaponBase; UCLASS() class BOSSFIGHT_API UPawnEquipmentComponent : public UPawnExtensionComponent { GENERATED_BODY() public: UFUNCTION(BlueprintCallable, Category = "Equipment") void RegisterWeapon(AWeaponBase* NewWeapon); protected: TMap<EBFWeaponType, AWeaponBase*> CurrentWeaponMap; };🎮 UPawnEquipmentComponent.cpp
void UPawnEquipmentComponent::RegisterWeapon(AWeaponBase* NewWeapon) { if (!CurrentWeaponMap.IsEmpty()) { CurrentWeaponMap.Empty(); } if (NewWeapon) { CurrentWeaponMap.Add(NewWeapon->GetWeaponType(), NewWeapon); } }위와같이 간단하게 RegisterWeapon()함수를 통해 무기를 등록 하도록 구현 하였다.
🎮 APlayerWeapon.h
class APlayerWeapon; UCLASS() class BOSSFIGHT_API UPlayerEquipmentComponent : public UPawnEquipmentComponent { GENERATED_BODY() public: UFUNCTION(BlueprintPure, Category = "Equipment") APlayerWeapon* GetPlayerCurrentWeapon(EBFWeaponType Type) const; };🎮 APlayerWeapon.cpp
APlayerWeapon* UPlayerEquipmentComponent::GetPlayerCurrentWeapon(EBFWeaponType Type) const { return Cast<APlayerWeapon>( CurrentWeaponMap.FindRef(Type)); }간단하게 WeaponType을 통해 현재 무기를 반환 하는 함수를 구현 하였다.
🎮 ABFPlayerCharacter.h
UCLASS() class BOSSFIGHT_API ABFPlayerCharacter : public ABFBaseCharacter { GENERATED_BODY() #pragma region Components // 중략 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "EquipmentComponent", meta = (AllowPrivateAccess = "true")) UPlayerEquipmentComponent* PlayerEquipmentComponent; #pragma endregion // 중략 };🎮 ABFPlayerCharacter.cpp
ABFPlayerCharacter::ABFPlayerCharacter() { // 중략 CreateDefaultSubobject<UPlayerEquipmentComponent>(TEXT("PlayerEquipmentComponent")); }
🎮 UPawnEquipmentInterface.h
class UPawnEquipmentComponent; class UPlayerEquipmentComponent; // This class does not need to be modified. UINTERFACE(MinimalAPI) class UPawnEquipmentInterface : public UInterface { GENERATED_BODY() }; class BOSSFIGHT_API IPawnEquipmentInterface { GENERATED_BODY() // Add interface functions to this class. This is the class that will be inherited to implement this interface. public: virtual UPawnEquipmentComponent* GetPawnEquipmentComponent() const = 0; virtual UPlayerEquipmentComponent* GetPlayerEquipmentComponent() const; };해당 Interface를 BaseCharacter에상속하고 필요한 함수를 Palyer 및 BaseCharacter에 정의 해주었다.
UPlayerEquipmentComponent* UBFPlayerAbility::GetPlayerEquipmentComponentFromActorInfo() { if (IPawnEquipmentInterface* PawnEquipmentInterface = Cast<IPawnEquipmentInterface>(GetPlayerCharacterFromActorInfo())) { return PawnEquipmentInterface->GetPlayerEquipmentComponent(); } else { return nullptr; } }플레이어의 EquipmentComponent를 반환 하는 함수를 작성 하였다.

