포트폴리오 제작/Project_P
Project_P Damage UI 제작(1) 로직 구성
k99812
2025. 3. 26. 17:07
기존 IPPGameInterface 에 함수 추가
IPPGameInterface.h
class PROJECT_P_API IPPGameInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
virtual void OnPlayerDead() = 0;
virtual void OnTakeDamage(const float& Damage, const FVector& ActorPosition) = 0;
};
- Damage UI 도 데미지를 받으면 게임모드를 통해 PlayerController에서 UI를 생성함
그래서 GameMode를 간접참조하기 위해 기존 인터페이스에 함수 추가 - virtual void OnTakeDamage(const float& Damage, const FVector& ActorPosition) = 0
- GameMode 에서 상속받을 함수 생성
- 플레이어 컨트롤러에서 UI를 생성하기 위해 필요한 정보, Damage 값이랑, 액터 위치를 매개변수로 받음
GameMode 에서 인터페이스 함수 구현
GameMode.h
UCLASS()
class PROJECT_P_API APPGameMode : public AGameModeBase, public IPPGameInterface
{
GENERATED_BODY()
public:
APPGameMode();
virtual void OnPlayerDead() override;
virtual void OnTakeDamage(const float& Damage, const FVector& ActorPosition) override;
};
- virtual void OnTakeDamage(const float& Damage, const FVector& ActorPosition) override;
- 새로 생성한 인터페이스 함수 상속
GameMode.cpp
void APPGameMode::OnTakeDamage(const float& Damage, const FVector& ActorPosition)
{
APPPlayerController* PlayerController = Cast<APPPlayerController>(GetWorld()->GetFirstPlayerController());
if (PlayerController)
{
PlayerController->ActorTakedDamage(Damage, ActorPosition);
}
}
- PlayerController->ActorTakedDamage(Damage, ActorPosition);
- 플레이어 컨트롤러를 가져와 플레이어 컨트롤러에 있는 함수를 실행
플레이어컨트롤러 함수 생성
PlayerController.h
public:
void ActorTakedDamage(const float& Damage, const FVector& ActorPosition);
- GameMode에서 함수에 접근할 수 있게 public으로 설정
- 함수에 전달받을 매개변수도 지정
데미지 이벤트 설정
데미지 이벤트의 경우 어빌리티 시스템이 제공해주는 어트리뷰트가 변화하면 실행되는 델리게이트를 사용
APPGASCharacterNonPlayer.h
protected:
virtual void TakeDamage(const FOnAttributeChangeData& ChangeData);
- 몬스터 클래스의 부모클래스에서 함수를 정의
- 어트리뷰트 체인지 델리게이트의 경우 const FOnAttributeChangeData& ChangeData 를 매개변수로 넘김
그래서 콜백함수에서 매개변수로 선언해야됨
APPGASCharacterNonPlayer.cpp
void APPGASCharacterNonPlayer::PossessedBy(AController* NewController)
{
Super::PossessedBy(NewController);
ASC->GetGameplayAttributeValueChangeDelegate(UPPGruntAttributeSet::GetDamageAttribute()).
AddUObject(this, &APPGASCharacterNonPlayer::TakeDamage);
}
- PossessedBy 함수에서 ASC(어빌리티 시스템 컴포넌트)를 통해 어트리뷰트 체인지 델리게이트에 함수 바인드
APPGASCharacterGrunt.cpp
void APPGASCharacterGrunt::TakeDamage(const FOnAttributeChangeData& ChangeData)
{
Super::TakeDamage(ChangeData);
if (ChangeData.NewValue > 0)
{
IPPGameInterface* IPPGameMode = Cast<IPPGameInterface>(GetWorld()->GetAuthGameMode());
if (IPPGameMode)
{
IPPGameMode->OnTakeDamage(ChangeData.NewValue, GetActorLocation());
}
}
}
- 몬스터 클래스에서 부모함수를 상속 받은 후
인터페이스를 통해 게임모드에 받은 데미지, 액터위치 전달