0 - 常用小技巧

本文记录一些UE5开发中的常用小技巧,随时间补充。

Actor

Actor伤害

UE5中对Actor受到的伤害内置了3种类型:

  • 任意伤害(Any Damage)
  • 点伤害(Point Damage)
  • 范围伤害(Radial Damage)

要想让Actor受到伤害,得用UGameplayStatics::ApplyDamage系列函数:

// #include "Kismet/GameplayStatics.h"

static float UGameplayStatics::ApplyDamage(
    AActor* DamagedActor, 
    float BaseDamage, 
    AController* EventInstigator, 
    AActor* DamageCauser, 
    TSubclassOf<class UDamageType> DamageTypeClass);

static float ApplyPointDamage(
    AActor* DamagedActor, 
    float BaseDamage, 
    const FVector& HitFromDirection, 
    const FHitResult& HitInfo, 
    AController* EventInstigator, 
    AActor* DamageCauser, 
    TSubclassOf<class UDamageType> DamageTypeClass);

static bool ApplyRadialDamage(
    const UObject* WorldContextObject, 
    float BaseDamage, 
    const FVector& Origin, 
    float DamageRadius, 
    TSubclassOf<class UDamageType> DamageTypeClass, 
    const TArray<AActor*>& IgnoreActors, 
    AActor* DamageCauser = NULL, 
    AController* InstigatedByController = NULL, 
    bool bDoFullDamage = false, 
    ECollisionChannel DamagePreventionChannel = ECC_Visibility);

Actor受到伤害后,会执行AActor::TakeDamage系列函数,需要我们重写这些函数以实现自定义逻辑:

// 任意类型
virtual float AActor::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser);

// 点伤害类型
virtual float AActor::InternalTakePointDamage(float Damage, struct FPointDamageEvent const& PointDamageEvent, class AController* EventInstigator, AActor* DamageCauser);

// 范围伤害类型
virtual float AActor::InternalTakeRadialDamage(float Damage, struct FRadialDamageEvent const& RadialDamageEvent, class AController* EventInstigator, AActor* DamageCauser);

Actor所有权

有时会让Actor“拥有”其他Actor,可通过AActor::SetOwner()进行设定。如果该Actor的拥有者是Pawn类型的,可以通过AActor::SetInstigator()实现。


参考资料

  • UE5 中角色伤害触发机制(学习中)_ue5 ufunction-CSDN博客
  • Course: Unreal Engine 5 C++ The Ultimate Game Developer Course | Udemy