unreal engine4 - ue4 c++ destory on hit if specifc actor -


i making projectile want destroy enemy objects basic floating objects
how both objects destroyed if hit specific 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(); } 

above have @ moment destroys hits not want.

i believe should simple if statement unsure how write it.
thanks in advance

other aactor, every time hit aactor you're going destroy projectile , other. assume want happen if projectile hits anything, projectile destroyed, , if object hits right type, object destroyed.

presumably objects want destroyed projectile derived aactor. like:

class destroyableenemy : public aactor     { //class definition      }; 

so know other pointer aactor, want know if, specifically, it's pointer destroyableenemy (or whatever you've named it). 2 ways can in c++ dynamic_cast , typeid operator. way know how offhand dynamic_cast. you're going try , cast generic aactor destroyableenemy. if destroyableenemy, pointer it. if isn't, null pointer.

destroyableenemy* otherenemy = dynamic_cast<destroyableenemy*>(other); if( otherenemy ){     //if otherenemy isn't null, cast succeeded because other destroyableenemy, , go branch     otherenemy->destroy(); }else{     // otherenemy null because other type of aactor     other->somethingelse(); //maybe add bullet hole? or nothing @ fine }; 

adapted from: https://en.wikibooks.org/wiki/c%2b%2b_programming/rtti


Comments