ue4c++如果指定角色,则会在命中时取消存储
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
};
所以你知道你的"他者"是一个指向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
相关文章:
- 将字符串存储在c++中的稳定内存中
- 挂起和取消挂起一个文件DLL
- std::原子加载和存储都需要吗
- C++:将控制台输出存储在宏中更好吗
- 如何取消对nullptr的屏蔽,返回正确的对象
- C++取消引用指针.为什么会发生变化
- 使用QProcess执行命令,并将结果存储在QStringList中
- 访问存储在向量C++中的结构的多态成员
- 如何从存储在std::映射中的std::集中删除元素
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 存储模板类型以强制转换回派生<T>
- 类型总是使用其大小存储在内存中吗
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 使用无符号字符数组有效存储内存
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 带结构的二维矢量:如何存储元素
- 取消引用运算符不能重载
- 取消引用无符号字符指针并将其值存储到字符串中
- ue4c++如果指定角色,则会在命中时取消存储