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.