How to get a component from a ClassDefaultObject?

Found solution on how to get all ActorComponents(both created in C++ and Blueprint) from another ActorComponent(non SceneComponent).


If you need to get ActorComponents from Actor itself replace GetOuter()->GetClass() on GetOwner()->GetClass()

code example

static TArray<FString> GetHitRegistratorsNames(UObject* OwnerObject)
{
        TArray<FString> Result = {};

        UBlueprintGeneratedClass* BlueprintGeneratedClass = Cast<UBlueprintGeneratedClass>(OwnerObject);
        if (!BlueprintGeneratedClass) return Result;
        
        TArray<UObject*> DefaultObjectSubobjects;
        BlueprintGeneratedClass->GetDefaultObjectSubobjects(DefaultObjectSubobjects);

        // Search for ActorComponents created from C++
        for (UObject* DefaultSubObject : DefaultObjectSubobjects)
        {
            if (DefaultSubObject->IsA(UCapsuleHitRegistrator::StaticClass()))
            {
                Result.Add(DefaultSubObject->GetName());
            }
        }
        
        // Search for ActorComponents created in Blueprint
        for (USCS_Node* Node : BlueprintGeneratedClass->SimpleConstructionScript->GetAllNodes())
        {
            if (Node->ComponentClass->IsChildOf(UCapsuleHitRegistrator::StaticClass()))
            {
                Result.Add(Node->GetVariableName().ToString());
            }
        }
        
        return Result;
}
2 Likes