How do I check if a class is a child class inside a function?

Hello.
In our game, our two base actor classes are ABaseEntity and AWorldObject. When an object collides, the hit object gets passed in to our OnHit(AActor* hitActor) function. The hit actor will be a subclass of ABaseEntity or AWorldObject, but I need a common base class to pass in anything.

Inside my function, I would like to be able to check if the hitActor is a child of BaseEntity or a child of WorldObject so I can react to it properly. If I pass in something like an ABasePlayer object (which is derived from ABaseEntity), inside the function hitActor will come in only as an AActor due to casting. From there, I could cast it back, but I’m having difficulty with the syntax of checking what class it is. Although the following code doesn’t work as is, I imagine it will be something along the lines of

MyClass::OnHit(AActor* hitActor)
    {
    	// Check if a BaseEntity object was hit
    	if ((Cast<UStruct>(hitActor)->IsChildOf(ABaseEntity::StaticClass()))
    	{
              // react to hitting
              // Cast<ABaseEntity>(hitActor)->ReduceHealth(some amount);
    	}
    }

I’ve also tried passing in the hit actor as a TSubclassOf< AActor > instead of as an AActor*, but I was having difficulties getting that to work as well.

Think simpler

ABaseEntity* BE = Cast<ABaseEntity>(hitActor);  
if(BE)
{
    //do someting if BaseEntity
    return;
}

AWorldObject* WO = Cast<AWorldObject>(hitActor);
if(WO)
{
    // do something if WorldObject
    return;
}

Casts return NULL when casting fails.

I usually write it like that

if(auto BE = Cast<ABaseEntity>(hitActor))
{
    //do BE stuff
    return;
}
else if(auto WO = Cast<AWorldObject>(hitActor))
{
    //do WO stuff
    return;
}

You want to place casts that are more likely to succeed at the top, because casts that fail are more expensive.

Ah, this should probably work. I’ll give it a try. Thanks!