How do I initialize a custom component that inherits from AActorComponent in C++?

I have this code:

#include "CustomComponent.h"

ASomeActor::ASomeActor(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
    class UCustomComponent* customComp;
    customComp = ObjectInitializer.CreateDefaultSubobject<UCustomComponent>(this, TEXT("CustomComponent"));
}

And it’s crashing every time I try to open up the blueprint actor that inherits from this custom actor class. When I run this in a Debugging Editor, it breaks at the line that is commented // THIS LINE of the StaticAllocateObject function in UObjectGlobals.cpp:
else
{
// See if object already exists.
Obj = StaticFindObjectFastInternal( /Class=/ NULL, InOuter, InName, true );

		// Temporary: If the object we found is of a different class, allow the object to be allocated.
		// This breaks new UObject assumptions and these need to be fixed.
		if (Obj && !Obj->GetClass()->IsChildOf(InClass))
		{
			UE_LOG(LogUObjectGlobals, Fatal,
				TEXT("Objects have the same fully qualified name but different paths.\n")
				TEXT("\tNew Object: %s %s.%s\n")
				TEXT("\tExisting Object: %s"),
				*InClass->GetName(), InOuter ? *InOuter->GetPathName() : TEXT(""), *InName.ToString(),
				*Obj->GetFullName());     // THIS LINE
		}
	}

What is the proper way of initializing a custom component that has inherited from AActorComponent ?

1 Like

Bump

I would like to know, too.

Looks like it’s complaining that the name of your custom component is not unique for the parent. Do you already have a component named “CustomComponent” attached to your actor when the error occurs?

There is nothing wrong with:

UProjectileMovementComponent* ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement"));

have you tried debugging with VS by attaching VS to the UE process and setting breakpoints?