How to get a list of subclass from a specific parent class in runtime?

I want to able to get a list of class names which subclass from a specific parent class in runtime. Is there any function I can do it. For example,

class Animal {}
class Tiger : public Animal {}
class Elephant : public Animal {}
class Panda : public Animal {}

a function like
TArray childClasses = GetAllClassNameFromParentClass(“Animal”), and childClasses become = {Tiger, Elephant, Panda}, and hopfully the function can also return any blueprint class too

Thanks

Hello,

UClass inherits from UStruct which has the member ‘Children’. The documentation is very sparse on whether these children are sub-types, properties of the struct, both, or something else.

Alternatively, you can iterate all UClasses using an object iterator and for every one call IsChildOf(YourSuper), if you want to get all sub-classes, or GetSuperClass() == YourSuper if you only want direct descendents of your class. Do this once at the beginning of the game and fill the result in an array.

void RetrieveAllSubclasses(UClass* Super, TArray<UClass*>& Results)
{
    if(Super == NULL)
    {
         return;
    }
    static TMap<FName, TArray<UClass*>> LookUpTable;
    TArray<UClass*>* pRes = LookUpTable->Find(Super->GetFName());
    if(pRes != NULL)
    {
        Results = *pRes;
        return;
    }
    Results.Empty(); // Empty to prevent malicious data injection
    for(TObjectIterator<UClass> It; It; ++It)
    {
        if(It->IsChildOf(Super))
        {
            Results.Add(*It);
        }
    }
    LookUpTable.Add(Super->GetFName(), Results);
}

Cheers,
Univise