やあ
制作中のゲームで状態異常を実装しようと思ったときに、どうすればいいんだーってAbilitySystemComponent
のソースを読んでたらタグが追加/削除されたときに処理ができるのを見つけたのでメモとして書いておきます。
やる
MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "AbilitySystemInterface.h"
#include "MyCharacter.generated.h"
class UAbilitySystemComponent;
UCLASS()
class MYPROJECT_API AMyCharacter
: public ACharacter, public IAbilitySystemInterface
{
GENERATED_BODY()
public:
AMyCharacter();
protected:
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Character|Ability")
UAbilitySystemComponent* AbilitySystemComponent;
public:
UAbilitySystemComponent* GetAbilitySystemComponent() const override;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Character|State")
FGameplayTag StunTag;
virtual void StunTagChanged(const FGameplayTag CallBackTag, int32 NewCount);
};
MyCharacter.cpp
#include "MyCharacter.h"
AMyCharacter::AMyCharacter()
{
AbilitySystemComponent = CreateDefaultSubobject<UAbilitySystemComponent>(TEXT("Ability System Comp"));
}
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
if (AbilitySystemComponent)
{
AbilitySystemComponent->RegisterGameplayTagEvent(StunTag, EGameplayTagEventType::NewOrRemoved).AddUObject(this, &AMyCharacter::StunTagChanged);
AbilitySystemComponent->RegisterGameplayTagEvent(StunTag, EGameplayTagEventType::AnyCountChange).AddUObject(this, &AMyCharacter::StunTagChanged);
}
}
UAbilitySystemComponent* AMyCharacter::GetAbilitySystemComponent() const
{
return AbilitySystemComponent;
}
void AMyCharacter::StunTagChanged(const FGameplayTag CallBackTag, int32 NewCount)
{
UE_LOG(LogTemp, Log, TEXT("StunTagChanged %s %d"), *CallBackTag.GetTagName().ToString(), NewCount);
if (NewCount > 0)
{
}
else
{
}
}
おまけ。GameplayEffect
が適用/削除されたときに処理をしたい場合。
void EffectAdded(
UAbilitySystemComponent* Source,
const FGameplayEffectSpec& SpecApplied,
FActiveGameplayEffectHandle ActiveHandle
);
void EffectRemoved(
const FActiveGameplayEffect& RemovedEffect
);
void AHoge::BeginPlay()
{
AbilitySystemComponent->OnGameplayEffectAppliedDelegateToSelf.AddUObject(
this, &AHoge::EffectAdded
);
AbilitySystemComponent->OnAnyGameplayEffectRemovedDelegate().AddUObject(
this, &AHoge::EffectRemoved
);
}
void AHoge::EffectAdded(
UAbilitySystemComponent* Source,
const FGameplayEffectSpec& EffectSpec,
FActiveGameplayEffectHandle EffectHandle
)
{
UE_LOG(LogTemp, Log, TEXT("EffectAdded"));
}
void AHoge::EffectRemoved(const FActiveGameplayEffect& RemovedEffect)
{
UE_LOG(LogTemp, Log, TEXT("EffectRemoved"));
}