# How to get a component from a ClassDefaultObject?

**URL:** <https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [February 4, 2017, 1:17am UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881 "2017-02-04T01:17:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Teiwaz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Teiwaz](https://forums.unrealengine.com/u/Teiwaz)\
**Post date:** [February 4, 2017, 1:17am UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/1 "2017-02-04T01:17:42Z")

</div>

I have the UClass of an AActor.

What I need is some default data off of a UActorComponent which is on the AActor.

In a sane world, I’d be able to just get the components by calling UClass-\>GetDefaultObject()-\>GetComponents(ArrayToPutThemIn) since the ClassDefaultObject is _supposed_ to just be an instance of the UClass. However, CDOs do not have any components, so all of the standard ways of accessing them fail.

Now, I know that data is on there somewhere. In theory, I could get at it by casting the UClass to a UBlueprintGeneratedClass and digging through the USimpleConstructionScript and UInheritableComponentHandler, but that’s a giant headache and it’s going to take ages digging through some pretty low-level engine to figure out how to do it. The actor construction code isn’t a big help, since it seems to rely on constructing the actor as it goes in order to make the component data available, which obviously isn’t something I can do in this case where I’m just trying to get the data _without_ building an actual instance of the AActor.

Does anyone know of an easier way to do this?

---

<div class="post-metadata">

**Author:** ![Teiwaz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Teiwaz](https://forums.unrealengine.com/u/Teiwaz)\
**Post date:** [February 4, 2017, 1:43am UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/2 "2017-02-04T01:43:46Z")

</div>

As usual, within 15 minutes of finally getting frustrated enough to post here, I found the answer.

For others with the same problem:

- Cast the actor class to a UBlueprintGeneratedClass

- Use UBrintGeneratedClass-\>SimpleConstructionScript-\>GetAllNodes() to get an array of USCS\_Nodes

- Iterate through the array looking for the USCS\_Node whose ComponentClass matches the component you’re looking for

- Return cast USCS node’s Template into your component class and return it, data’s all there

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_18cad5be](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_18cad5be](https://forums.unrealengine.com/u/anonymous_user_18cad5be)\
**Post date:** [August 10, 2017, 7:42am UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/3 "2017-08-10T07:42:59Z")

</div>

EDIT: Nice to hear you got it working 🙂

Here’s what we’re using, annotated with comments matching the above steps:

```
template
T* FindDefaultComponentByClass(const TSubclassOf InActorClass) const
{
	return (T*)FindDefaultComponentByClass(InActorClass, T::StaticClass());
}

UActorComponent* FindDefaultComponentByClass(const TSubclassOf InActorClass, const TSubclassOf InComponentClass) const
{
	// Cast the actor class to a UBlueprintGeneratedClass
	UBlueprintGeneratedClass* ActorBlueprintGeneratedClass = Cast<UBlueprintGeneratedClass>(InActorClass);

	// Use UBrintGeneratedClass->SimpleConstructionScript->GetAllNodes() to get an array of USCS_Nodes
	const TArray<USCS_Node*>& ActorBlueprintNodes = ActorBlueprintGeneratedClass->SimpleConstructionScript->GetAllNodes();

	// Iterate through the array looking for the USCS_Node whose ComponentClass matches the component you're looking for
	for (USCS_Node* Node : ActorBlueprintNodes)
	{
		if (Node->ComponentClass == InComponentClass)
		{
			// Return cast USCS node's Template into your component class and return it, data's all there
			return Node->ComponentTemplate;
		}
	}

	return nullptr;
}

```

---

<div class="post-metadata">

**Author:** ![JoshSDev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoshSDev](https://forums.unrealengine.com/u/JoshSDev)\
**Post date:** [March 23, 2018, 1:13am UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/4 "2018-03-23T01:13:43Z")

</div>

I’ve reworked that function you posted for my use case, @Nick.Pruehs.

This one works with subclasses and at runtime. It’s a standalone function made for use outside a class and returns all of the components instead of just the first. 🙂

```
 template <class T>
    void FindDefaultComponentsByClass(const UClass* InActorClass, UClass* InComponentClass, TArray<T*>& outArray)
    {
    	// Cast the actor class to a UBlueprintGeneratedClass
    	const UBlueprintGeneratedClass* ActorBlueprintGeneratedClass = Cast<UBlueprintGeneratedClass>(InActorClass);
    
    	// Use UBrintGeneratedClass->SimpleConstructionScript->GetAllNodes() to get an array of USCS_Nodes
    	const TArray<USCS_Node*>& ActorBlueprintNodes = ActorBlueprintGeneratedClass->SimpleConstructionScript->GetAllNodes();
    
    	// Iterate through the array looking for the USCS_Node whose ComponentClass matches the component you're looking for
    	for (USCS_Node* Node : ActorBlueprintNodes)
    	{
    		if (UClass::FindCommonBase(Node->ComponentClass, InComponentClass) == InComponentClass)
    		{
    			// Return cast USCS node's Template into your component class and return it, data's all there
    			outArray.Emplace(Cast<T>(Node->ComponentTemplate));
    		}
    	}
    }
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_c0bd212d](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_c0bd212d](https://forums.unrealengine.com/u/anonymous_user_c0bd212d)\
**Post date:** [May 17, 2018, 2:40pm UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/5 "2018-05-17T14:40:40Z")

</div>

The previously presented solution above has a flaw. Imagine you have an Actor base blueprint that has an ActorComponent attached to it and you have a child Actor blueprint derived from the first. If you then call ‘FindDefaultComponentByClass’ with the derived Actor class as parameter it will not find the desired component that was added to the parent class.

The solution presented below will find the desired component in any case:

```
UActorComponent* FindDefaultComponentByClass(const TSubclassOf<AActor> InActorClass,
                                             const TSubclassOf<UActorComponent> InComponentClass)
{
    if (!IsValid(InActorClass))
    {
        return nullptr;
    }

    // Check CDO.
    AActor* ActorCDO = InActorClass->GetDefaultObject<AActor>();
    UActorComponent* FoundComponent = ActorCDO->FindComponentByClass(InComponentClass);

    if (FoundComponent != nullptr)
    {
        return FoundComponent;
    }

    // Check blueprint nodes. Components added in blueprint editor only (and not in code) are not available from
    // CDO.
    UBlueprintGeneratedClass* RootBlueprintGeneratedClass = Cast<UBlueprintGeneratedClass>(InActorClass);
    UClass* ActorClass = InActorClass;

    // Go down the inheritance tree to find nodes that were added to parent blueprints of our blueprint graph.
    do
    {
        UBlueprintGeneratedClass* ActorBlueprintGeneratedClass = Cast<UBlueprintGeneratedClass>(ActorClass);
        if (!ActorBlueprintGeneratedClass)
        {
            return nullptr;
        }

        const TArray<USCS_Node*>& ActorBlueprintNodes =
            ActorBlueprintGeneratedClass->SimpleConstructionScript->GetAllNodes();

        for (USCS_Node* Node : ActorBlueprintNodes)
        {
            if (Node->ComponentClass->IsChildOf(InComponentClass))
            {
                return Node->GetActualComponentTemplate(RootBlueprintGeneratedClass);
            }
        }

        ActorClass = Cast<UClass>(ActorClass->GetSuperStruct());

    } while (ActorClass != AActor::StaticClass());

    return nullptr;
}

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_db3a13d3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_db3a13d3](https://forums.unrealengine.com/u/anonymous_user_db3a13d3)\
**Post date:** [August 27, 2021, 4:27am UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/6 "2021-08-27T04:27:06Z")

</div>

Thank you so much, it works!

---

<div class="post-metadata">

**Author:** ![Gubidonius](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/gubidonius/32/742377_2.png) [@Gubidonius](https://forums.unrealengine.com/u/Gubidonius)\
**Post date:** [March 11, 2023, 10:19pm UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/7 "2023-03-11T22:19:32Z")

</div>

Hi [PhilippOkoampah](https://forums.unrealengine.com/u/PhilippOkoampah), the SimpleConstructionScript doesn’t contain the root component of a class, this is a significant problem,

Calling GetRootNodes() also won’t help because SCS considers the first layer of AllNodes as roots (ain’t got a clue why)

It seems to me that the only member that stores at least some info about the complete list of nodes is the UBlueprintGeneratedClass::CustomPropertyListForPostConstruction, but it has only FProperties and UClasses, so if you have any ideas on another workaround, it will be very useful

---

<div class="post-metadata">

**Author:** ![Ciberus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ciberus/32/561261_2.png) [@Ciberus](https://forums.unrealengine.com/u/Ciberus)\
**Post date:** [July 6, 2023, 2:59pm UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/8 "2023-07-06T14:59:25Z")

</div>

Did you found solution?

---

<div class="post-metadata">

**Author:** ![Ciberus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ciberus/32/561261_2.png) [@Ciberus](https://forums.unrealengine.com/u/Ciberus)\
**Post date:** [July 8, 2023, 1:13pm UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/9 "2023-07-08T13:13:20Z")

</div>

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

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/0/c/b0c10a86a9e220e252d5a59361833fd46fe91606.png)  
If you need to get ActorComponents from Actor itself replace `GetOuter()->GetClass()` on `GetOwner()->GetClass()`

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/3/c/e3c4056c0cbd4b53c8e21acf5fc2c37bafb735a6.png)

code example

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![Andrei\_K\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Andrei\_K\_1](https://forums.unrealengine.com/u/Andrei_K_1)\
**Post date:** [September 18, 2023, 2:59pm UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/10 "2023-09-18T14:59:37Z")

</div>

Thanks a lot for this solution. Due to circumstances we had to work in 4.27 and this code worked out nicely for me.

---

<div class="post-metadata">

**Author:** ![Madmer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/madmer/32/1083599_2.png) [@Madmer](https://forums.unrealengine.com/u/Madmer)\
**Post date:** [July 7, 2025, 1:19pm UTC](https://forums.unrealengine.com/t/how-to-get-a-component-from-a-classdefaultobject/383881/11 "2025-07-07T13:19:36Z")

</div>

Thank you!  
This is really working solution in my case 😀
