UE5/C++ 基于GAS创建攻击伤害 5.3.1 碰撞体伤害事件注册

1.

创建继承至HitBoxCollision C++的命名为AMMOARPGBoxHit

打开AMMOARPGBoxHit.h

#pragma once

#include "CoreMinimal.h"
#include "Hit/ComboSkillHitBox.h"
#include "MMOARPGBoxHit.generated.h"
/**
 * 
 */
UCLASS(BlueprintType, Blueprintable)
class AMMOARPGBoxHit : public AHitBoxCollision
{
	GENERATED_BODY()

public:
	AMMOARPGBoxHit(const FObjectInitializer& ObjectInitializer);

	virtual void HandleDamage(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	// Called every frame
	virtual void Tick(float DeltaTime) override;
};

进行实现,传入的Actor必须是实现了IAbilitySystemInterface接口

#include "MMOARPGBoxHit.h"
#include "AbilitySystemBlueprintLibrary.h"
#include "GameplayTags.h"
#include "../../Game/Character/Core/MMOARPGCharacterBase.h"


AMMOARPGBoxHit::AMMOARPGBoxHit(const FObjectInitializer& ObjectInitializer):Super(ObjectInitializer)
{

}

void AMMOARPGBoxHit::HandleDamage(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	Super::HandleDamage(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);


	if (GetInstigator()!= OtherActor)//施法者不能对自己造成伤害
	{
		if (AMMOARPGCharacterBase* InPawn = Cast(GetInstigator()))
		{
			if (AMMOARPGCharacterBase* InTarget = Cast(OtherActor))
			{
				FGameplayEventData EventData;//准备触发数据
				EventData.Instigator = GetInstigator();//施法者
				EventData.Target = InTarget;//目标

				//发送伤害事件数据
				UAbilitySystemBlueprintLibrary::SendGameplayEventToActor(GetInstigator(), FGameplayTag::RequestGameplayTag(TEXT("Player.Attack.ComboLinkage")), EventData);
			}
		}
	}
}

void AMMOARPGBoxHit::BeginPlay()
{
	Super::BeginPlay();
}

void AMMOARPGBoxHit::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

你可能感兴趣的:(ue5,c++,游戏引擎)