AnimInstance()를 UPlayerAvatarInstance로 캐스팅해 EPlayerState가 Locomotion이면서 Attack카운트다운시간이 0일경우 true를 리턴한다.
bool APlayerAvatar::CanAttack()
{
auto animInst = Cast<UPlayerAvatarAnimInstance>(GetMesh()->GetAnimInstance());
return (_AttackCountingDown <= 0.0f && animInst->State == EPlayerState::Locomotion);
}
Attack()함수는 타이머를 초기화한다. 애니메이션은 AnimationBlueprint에서 처리한다.
void APlayerAvatar::Attack()
{
_AttackCountingDown = AttackInterval;
}
ProjectSetting>Engine>Input을 열고 InputAction을 추가한다.
Tick()함수는 AttackCountDown
// Called every frame
void APlayerAvatar::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
UPlayerAvatarAnimInstance* animInst = Cast<UPlayerAvatarAnimInstance>(GetMesh()->GetAnimInstance());
animInst->Speed = GetCharacterMovement()->Velocity.Size2D();
if (_AttackCountingDown == AttackInterval)
{
animInst->State = EPlayerState::Attack;
}
if (_AttackCountingDown > 0.0f)
{
_AttackCountingDown -= DeltaTime;
}
}
플레이해보면 공격우클릭을 하면 한번만 공격하고 돌아오지 않는다 Animation Assets의 Hero_Anim_Attack_Anim을 더블클릭해서 열고
노티파이트랙의 우클릭후 AttackEnds를 입력
HitEnds, DieEnds도 동일하게 되어 있다.
노티파이를 등록하면 AnimationBlueprints EventGraph창에서 노드로 추가가능하다
OnStateAnimationEnds함수 구현하기
OnStateAnimationEnds함수는 PlayerAvatarAnimInstance.h에 선언되었기 때문에 블루투스에서 노드로 추가할수 있었다.
UFUNCTION(BlueprintCallable)
void OnStateAnimationEnds();
cpp에서 다음과 같이 구현하자,
3개의 노티파이가 하나의 함수에서 처리해야하기 때문에 우선 EPlayerState를 체크해 처리를 나눈다.
Hit의 경우 헬스가 0이라면 죽음처리를 하자.
void UPlayerAvatarAnimInstance::OnStateAnimationEnds()
{
if (State == EPlayerState::Attack)
{
State = EPlayerState::Locomotion;
}
else
{
auto playerAvatar = Cast<APlayerAvatar>(GetOwningActor());
if (State == EPlayerState::Hit)
{
if (playerAvatar->GetHealthPoints() > 0.0f)
{
State = EPlayerState::Locomotion;
}
else
{
State = EPlayerState::Die;
}
}
else if (State == EPlayerState::Die)
{
playerAvatar->DieProcess();
}
}
}
Die Process는 Tick을 멈추고 가비지처리를 요청한다. Destroy()로 한줄도 대신할수 있다
void APlayerAvatar::DieProcess()
{
PrimaryActorTick.bCanEverTick = false;
K2_DestroyActor();
GEngine->ForceGarbageCollection(true);
//Destroy(); You can also use this line instead of the above three lines.
}
액터가 인스턴스화되면, 엔진은 메모리를 할당해 액터의 정보를 저장한다. 그동안 반환된 액터의 포인터는 메모리 블록이 시작되는 주소를 저장한다. 액터가 파괴되어도 메모리들이 반환되지 않는다. 가비지 컬렉션이 시작될 때만 새로운 메모리할당을 위해 파괴된 오브젝트의 메모리가 반환된다.
가비지컬렉션은 무거우므로 되도록이면 게임에 방해되지 않게 적당한 타이밍에 실행시켜야 한다.
'언리얼러닝 > C++스크립트게임개발' 카테고리의 다른 글
EnhancedInput vs LegacyInput, Input Error (0) | 2025.05.27 |
---|---|
07 PlayerController 캐릭터 제어하기 SimpleMoveToLocation (1) | 2025.05.24 |
게임 액터 생성하기 (0) | 2025.05.16 |
언리얼 게임플레이 프레임워크 클래스 학습하기 (0) | 2025.05.16 |
게임프레임워크 링크 (0) | 2025.05.14 |