본문 바로가기
포트폴리오 제작/Project_P

Project_P 캐릭터 달리기 구현 Sprint GA 생성

by k99812 2024. 8. 23.

Sprint GA 생성


 

  • GameplayAbility 클래스를 상속받아 PPGA_Sprint 생성

 

PPGA _Sprint 헤더파일


#include "CoreMinimal.h"
#include "Abilities/GameplayAbility.h"
#include "PPGA_Sprint.generated.h"

/**
 * 
 */
UCLASS()
class PROJECT_P_API UPPGA_Sprint : public UGameplayAbility
{
	GENERATED_BODY()
	
public:
	UPPGA_Sprint();

	virtual void ActivateAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo, const FGameplayEventData* TriggerEventData) override;

	virtual void EndAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo, bool bReplicateEndAbility, bool bWasCancelled) override;

	virtual void InputReleased(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo) override;

protected:
	float WalkSpeed;

	float SprintSpeed;
};

 

  • UPPGA_Sprint();
    • 생성자
  • virtual void ActivateAbility(~~~) override;
    • GA 실행시 GA가 정상적으로 실행되면 호출되는 함수
    • override해 GA실행시 필요한 코드를 추가
  • virtual void EndAbility(~~~) override;
    • GA 종료시 호출되는 함수
    • override해 GA종료시 필요한 코드를 추가
  • virtual void InputReleased(~~~) override;
    • 인풋이 종료될때 실행되는 함수
    • 캐릭터 액터에서 인풋(ETriggerEvent::Completed) 바인드 함수에서
      ASC->AbilitySpecInputReleased 함수 실행시 호출 됨
  • float WalkSpeed;
    float SprintSpeed;
    • 캐릭터 WalkSpeed와 SprintSpeed를 저장할 변수
    • 추후 GAS에서 제공하는 어트리뷰트를 사용할 예정
      • 현재는 생성자에서 초기화, 추후 어트리뷰트로 초기화

 

PPGA _Sprint Cpp파일


UPPGA_Sprint

UPPGA_Sprint::UPPGA_Sprint() : SprintSpeed(1000.0f), WalkSpeed(500.0f)
{
	InstancingPolicy = EGameplayAbilityInstancingPolicy::InstancedPerActor;
}

 

  • SprintSpeed(1000.0f), WalkSpeed(500.0f)
    • SprintSpeed, WalkSpeed 초기화
  • InstancingPolicy = EGameplayAbilityInstancingPolicy::InstancedPerActor;
    • 인스턴싱폴리시 설정
    • 인스턴스가 액터마다 하나만 생성됨

 

ActivateAbility

void UPPGA_Sprint::ActivateAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo, const FGameplayEventData* TriggerEventData)
{
	Super::ActivateAbility(Handle, ActorInfo, ActivationInfo, TriggerEventData);

	ACharacter* Character = CastChecked<ACharacter>(ActorInfo->AvatarActor.Get());
	UCharacterMovementComponent* MovementComponent = Character->GetCharacterMovement();

	if (MovementComponent)
	{
		MovementComponent->MaxWalkSpeed = SprintSpeed;
	}
}

 

  • Super::ActivateAbility(Handle, ActorInfo, ActivationInfo, TriggerEventData);
    • override 함수이므로 부모 로직 호출
  • ACharacter* Character = CastChecked<ACharacter>(ActorInfo->AvatarActor.Get());
    • ActorInfo->AvatarActor.Get()
      • ActorInfo를 사용하여 ASC에 등록된 AvatarActor(캐릭터)를 가져옴
    • CastChecked를 이용하여 Actor클래스를 캐릭터로 형변환
  •  UCharacterMovementComponent* MovementComponent = Character->GetCharacterMovement();
    • 가져온 캐릭터에서 MovementComponent를 가져 옴
  • if (MovementComponent)
    • 무브먼트를 가져 왔으면 (nullptr이 아니면)
    • MovementComponent->MaxWalkSpeed = SprintSpeed;
      • 캐릭터 스피드를 SprintSpeed로 변경

 

EndAbility

void UPPGA_Sprint::EndAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo, bool bReplicateEndAbility, bool bWasCancelled)
{
	Super::EndAbility(Handle, ActorInfo, ActivationInfo, bReplicateEndAbility, bWasCancelled);

	ACharacter* Character = CastChecked<ACharacter>(ActorInfo->AvatarActor.Get());
	UCharacterMovementComponent* MovementComponent = Character->GetCharacterMovement();

	if (MovementComponent)
	{
		MovementComponent->MaxWalkSpeed = WalkSpeed;
	}
}

 

  • Super::EndAbility(Handle, ActorInfo, ActivationInfo, bReplicateEndAbility, bWasCancelled);
    • override 함수이므로 부모 로직 호출
  • EndAbility 가 호출되면 캐릭터 속도를 되돌림
  • ACharacter* Character = CastChecked<ACharacter>(ActorInfo->AvatarActor.Get());
    • ActorInfo->AvatarActor.Get()
      • ActorInfo를 사용하여 ASC에 등록된 AvatarActor(캐릭터)를 가져옴
    • CastChecked를 이용하여 Actor클래스를 캐릭터로 형변환
  • UCharacterMovementComponent* MovementComponent = Character->GetCharacterMovement();
    • 가져온 캐릭터에서 MovementComponent를 가져 옴
  • if (MovementComponent)
    • 무브먼트를 가져 왔으면 (nullptr이 아니면)
    • MovementComponent->MaxWalkSpeed = WalkSpeed;
      • 캐릭터 MaxWalkSpeed 복구

 

InputReleased

void UPPGA_Sprint::InputReleased(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo)
{
	bool bReplicateEndAbility = true;
	bool bWasCancelled = false;
	EndAbility(Handle, ActorInfo, ActivationInfo, bReplicateEndAbility, bWasCancelled);
}

 

  • override함수지만 부모함수에서 구현부가 작성되어 있지 않아
    Super 키워드로 부모함수를 호출하지 않아도 됨
  • InputReleased 함수가 호출되면 EndAbility를 호출하여 Sprint GA를 종료