如果特定演员,ue4 c ++在击中时销毁
ue4 c++ destory on hit if specifc actor
我正在制作一个射弹,我想摧毁敌人的物体,这些物体将是基本的浮动物体
我如何获得它,以便在被特定演员击中时两个物体都被摧毁。
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
};
所以你知道你的 Other 是一个指向 AActor 的指针,你想知道的是,具体来说,它是否是一个指向 DestroyableEnemy(或你给它命名的任何东西)的指针。您可以在 C++ 中执行此操作的两种方法是 dynamic_cast 和 typeid 运算符。我知道如何立即做到这一点的方法是使用 dynamic_cast。您将尝试将通用 AActor 转换为 DestroyableEnemy。如果它是一个 DestroyableEnemy,你会得到一个指向它的指针。如果不是,你只会得到一个空指针。
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
};
我正在制作一个射弹,我想摧毁敌人的物体,这些物体将是基本的浮动物体
我如何获得它,以便在被特定演员击中时两个物体都被摧毁。
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
};
所以你知道你的 Other 是一个指向 AActor 的指针,你想知道的是,具体来说,它是否是一个指向 DestroyableEnemy(或你给它命名的任何东西)的指针。您可以在 C++ 中执行此操作的两种方法是 dynamic_cast 和 typeid 运算符。我知道如何立即做到这一点的方法是使用 dynamic_cast。您将尝试将通用 AActor 转换为 DestroyableEnemy。如果它是一个 DestroyableEnemy,你会得到一个指向它的指针。如果不是,你只会得到一个空指针。
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
};