ue4c++如果指定角色,则会在命中时取消存储

ue4 c++ destory on hit if specifc actor

本文关键字:取消 存储 如果 定角色 ue4c++      更新时间:2023-10-16

我正在制作一枚炮弹,我想摧毁敌人的物体,这些物体将是基本的漂浮物体
我该如何获得它,以便在被特定角色击中时摧毁这两个物体。

 void APrimaryGun::NotifyHit(UPrimitiveComponent* MyComp, AActor* Other, UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit)
{
    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 1.5, FColor::Red, "Hit");
    }
    Destroy();
    Other->Destroy();
}

以上是我目前所拥有的,但摧毁了它击中的任何不是我想要的东西。

我认为这应该是一个简单的if语句,但不确定如何写。
提前感谢

Other是任何AActor,所以每次你击中AActor时,你都会摧毁炮弹和Other。我想你想发生的是,如果你的炮弹击中任何东西,炮弹就会被摧毁,如果它击中的物体是正确的类型,那么这个物体也会被摧毁。

据推测,你想被炮弹摧毁的物体来自AActor。类似于:

class DestroyableEnemy : public AActor
    { //class definition
    };

所以你知道你的"他者"是一个指向AActor的指针,你想知道的是,特别是,它是否是一个可摧毁敌人(或你给它命名的任何东西)的指针。在C++中可以实现这一点的两种方法是dynamic_cast和typeid运算符。我知道如何立即完成的方法是使用dynamic_cast。你要试着把普通的AActor变成一个可摧毁的敌人。如果它是一个可摧毁的敌人,你会得到一个指向它的指针。如果它不是,你只得到一个空指针。

DestroyableEnemy* otherEnemy = dynamic_cast<DestroyableEnemy*>(Other);
if( otherEnemy ){ 
   //if otherEnemy isn't null, the cast succeeded because Other was a destroyableEnemy, and you go to this branch
    otherEnemy->Destroy();
}else{
    // otherEnemy was null because it was some other type of AActor
    Other->SomethingElse(); //maybe add a bullet hole? Or nothing at all is fine
};

改编自:https://en.wikibooks.org/wiki/C%2B%2B_Programming/RTTI