Can't get child class instance from "OnHit" function

Hello!
I’m trying to make a function which changes action by hit object and use “OnHit” function.
Some objects are made from [Y class] inherited “AActor” class.
“OnHit” function has “AActor* OtherActor” arugement, but I can’t cast “AActor*” to [Y class]*.
So I can’t get information of hit object.

How should I deal with it?

Thanks for reading and sorry for my poor English!

if your hit object is a Y class or inherits from a Y class you should be totally able to cast to it?

You created a new actor right?
something like

class  AYClass : public AActor  

and the object you are hitting is of that type?
and you use

auto* HitActorAsYClass = Cast<AYClass>(OtherActor);
if (HitActorAsYClass)
  HitActorAsYClass->DoSomething();

Thank you for advice!

Hit object have various class. So it’s class type can be not only [Y class] but also [A class] or [B class] …etc.

It may be fine by a following way

auto* HitActorAsYClass = Cast<AYClass>(OtherActor);
if (HitActorAsYClass) {
   HitActorAsYClass->DoSomething();
} else {
   auto* HitActorAsAClass = Cast<AAClass>(OtherActor);
   if (HitActorAsAClass) {
      HitActorAsAClass->DoSomething();
   } else {
      auto* HitActorAsBClass = Cast<ABClass>(OtherActor);
      if (HitActorAsBClass) {
         HitActorAsBClass->DoSomething();
      } else {
         // if hit actor class is C, D, E...
      }
   } 
}

But it would become nested branch.
Ideally I want to do like this (it has error)

auto* HitActor = Cast<OtherActor->GetClass()>(OtherActor);
HitActor->DoSomething();

casting can not work with runtime classes, it needs to know what to cast to at compile time

sounds like what you want to do is use an interface

basically all your classes that should DoSomething need to implement that interface and in the hit you check if the object implements it, and if so call it

Thank you for quick replay!
I will learn it !

if you want I can write a small example,
just takes a little bit more time then a fast answer :stuck_out_tongue:

I have to cast [OtherActor] to InterfaceClass and then, check it can be used by “not NULL”.

I think I can implement now!
Thank you ! I spent more than 10 hours around this problem! Thank you !