How to add an ActorComponent created in blueprints to an Actor created in c++?

Ghar
Tue, 14 Jul 2015 20:27:01 +0000
Blueprint

I have a custom ActorComponent created with blueprints. Now I am creating a new Character in code, but I haven’t been able to find an example on how to add the component to the Character.

This is what I have so far:

h:

UCLASS()
class ZOMBIECOUNT_API AZCParentCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AZCParentCharacter();

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = ActorComponents)
	UActorComponent* ACTargetableComponent;
};

cpp:

AZCParentCharacter::AZCParentCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	static ConstructorHelpers::FObjectFinder <UActorComponent>ACTargetableC(TEXT("Blueprint'/Game/Blueprints/Components/AC_Targetable.AC_Targetable'"));
	if (ACTargetableC.Object != NULL)
	{
		ACTargetableComponent = ACTargetableC.Object;
	AddInstanceComponent(ACTargetableComponent);
	}
}

But when I open the project I get:

If I create a blueprint based on AZCParentCharacter, it doesn’t have the component in the components tab:

The path to the component must be right because I right clicked it in the Editor, then clicked “Copy Reference”, and pasted it in the function.

I have a feeling everything I’ve done is wrong lol. Any help is appreaciated.

3 posts - 1 participant