.h
virtual class UAbilitySystemComponent* GetAbilitySystemComponent() const override;
UPROPERTY(EditAnywhere, Category = GAS)
TObjectPtr<class UAbilitySystemComponent> ASC;
.cpp
ASC = CreateDefaultSubobject<UAbilitySystemComponent>(TEXT("ASC"));
void AABGASFountain::PostInitializeComponents()
{
Super::PostInitializeComponents();
ASC->InitAbilityActorInfo(this, this);
FGameplayAbilitySpec RotateSkillSpec(UABGA_Rotate::StaticClass());
ASC->GiveAbility(RotateSkillSpec);
}
ABGAS_LOG(LogABGAS, Log, TEXT("Active!"));
ASC->TryActivateAbility(RotateSpec->Handle);
초기화하고
ASC에 AB를 등록할 때는 ASC->GiveAbility(스펙)으로
ASC에 등록된 어빌리티를 실행할때는 TryActiveAbility(스펙의 핸들)로
INPUT
PlayerState <ㅡ> PlayerCharacter
플레이어 캐릭터(폰)에서 빙의될때 플레이어 스테이트의 ASC를 폰으로 넘겨줌
void AABGASCharacterPlayer::PossessedBy(AController* NewController)
{
Super::PossessedBy(NewController);
AABGAS_PlayerState* GASPS = GetPlayerState<AABGAS_PlayerState>();
if (GASPS)
{
ASC = GASPS->GetAbilitySystemComponent();
ASC->InitAbilityActorInfo(GASPS, this);
int32 InputId = 0;
for (const auto& StartAbility : StartAbilities)
{
FGameplayAbilitySpec StartSpec(StartAbility);
StartSpec.InputID = InputId++;
ASC->GiveAbility(StartSpec);
SetUpGASInputComponent();
}
}
}
여기서 InputId는 플레이어 입력을 위해 사용됨
void AABGASCharacterPlayer::SetUpGASInputComponent()
{
if (IsValid(ASC) && IsValid(InputComponent))
{
UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &AABGASCharacterPlayer::GASInputPressed, 0);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &AABGASCharacterPlayer::GASInputReleased, 0);
}
}
void AABGASCharacterPlayer::GASInputPressed(int32 InputId)
{
FGameplayAbilitySpec* Spec = ASC->FindAbilitySpecFromInputID(InputId);
if (Spec)
{
Spec->InputPressed = true;
if (Spec->IsActive())
{
ASC->AbilitySpecInputPressed(*Spec);
}
else
{
ASC->TryActivateAbility(Spec->Handle);
}
}
}
제일 끝 인수 0은 InputId에 0을 넘겨줌
Spec의 인풋아이디를 체크해서 어빌리티를 찾고 그 어빌리티를 활성화!
개선
void AABGASCharacterPlayer::PossessedBy(AController* NewController)
{
Super::PossessedBy(NewController);
AABGAS_PlayerState* GASPS = GetPlayerState<AABGAS_PlayerState>();
if (GASPS)
{
ASC = GASPS->GetAbilitySystemComponent();
ASC->InitAbilityActorInfo(GASPS, this);
for (const auto& StartAbility : StartAbilities)
{
FGameplayAbilitySpec StartSpec(StartAbility);
ASC->GiveAbility(StartSpec);
}
for (const auto& StartInputAbility : StartInputAbilities)
{
FGameplayAbilitySpec StartSpec(StartInputAbility.Value);
StartSpec.InputID = StartInputAbility.Key;
ASC->GiveAbility(StartSpec);
}
SetUpGASInputComponent();
//디버그용
APlayerController* PlayerController = CastChecked<APlayerController>(NewController);
PlayerController->ConsoleCommand(TEXT("showdebug abilitysystem"));
}
}
'GAS > 개인저장용' 카테고리의 다른 글
[Error Handling] Gameplay Effect (0) | 2025.02.06 |
---|