How to get a component from a ClassDefaultObject?

EDIT: Nice to hear you got it working :slight_smile:

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;
}
5 Likes