Creating a component

Hello!
I have a class that has UPrimitiveComponent which is created in child classes. But I’d like to create this component in blueprints and use it in cpp. How can I do this?

There’s my code:

ATTBaseInteractiveActor.h

UCLASS()
class TESTTASK_API ATTBaseInteractiveActor : public AActor
{
	GENERATED_BODY()
	
public:	
	
	ATTBaseInteractiveActor(const FObjectInitializer& ObjectInitializer);

	static FName CollisionComponentName;
	
	UFUNCTION(BlueprintNativeEvent)
	void StartInteraction();
	virtual void StartInteraction_Implementation() {}

	UFUNCTION(BlueprintNativeEvent)
	void FinishInteraction();
	virtual void FinishInteraction_Implementation() {}

	bool GetReadyToInteract() const { return bReadyToInteract; }

protected:

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Collision")
	UPrimitiveComponent* CollisionComponent;

	bool bReadyToInteract;

	UFUNCTION(BlueprintNativeEvent)
	void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
		int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	virtual void OnOverlapBegin_Implementation(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
		int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {}

	UFUNCTION(BlueprintNativeEvent)
	void OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
	virtual void OnOverlapEnd_Implementation(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
		UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {}
};

ATTBaseInteractiveActor.cpp

FName ATTBaseInteractiveActor::CollisionComponentName(TEXT("CollisionComponent"));

ATTBaseInteractiveActor::ATTBaseInteractiveActor(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
 	PrimaryActorTick.bCanEverTick = true;

	CollisionComponent = CreateDefaultSubobject<UPrimitiveComponent>(CollisionComponentName);
}

ATTPressurePlate.h

UCLASS()
class TESTTASK_API ATTPressurePlate : public ATTBaseInteractiveActor
{
	GENERATED_BODY()

public:

	ATTPressurePlate(const FObjectInitializer& ObjectInitializer);

protected:

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Mesh")
	UStaticMeshComponent* Base;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Mesh")
	UStaticMeshComponent* Plate;

	virtual void OnOverlapBegin_Implementation(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
	UPrimitiveComponent* OtherComp,int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;
	
	virtual void OnOverlapEnd_Implementation(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
		UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) override;

	int32 CountOfOverlappingObjects;

	virtual void BeginPlay() override;
};

ATTPressurePlate.cpp

ATTPressurePlate::ATTPressurePlate(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer.SetDefaultSubobjectClass<UStaticMeshComponent>(ATTBaseInteractiveActor::CollisionComponentName))
{
	Base =CreateDefaultSubobject<UStaticMeshComponent>("Base");
	SetRootComponent(Base);
	
	Plate = CreateDefaultSubobject<UStaticMeshComponent>("Plate");
	Plate->SetupAttachment(GetRootComponent());

	if (CollisionComponent)
	{
		CollisionComponent->SetupAttachment(Plate);
		CollisionComponent->SetVisibility(false);
		CollisionComponent->OnComponentBeginOverlap.AddDynamic(this, &ATTPressurePlate::OnOverlapBegin);
		CollisionComponent->OnComponentEndOverlap.AddDynamic(this, &ATTPressurePlate::OnOverlapEnd);
	}
}

You can use an FComponentReference property to reference your BP component, and then maybe in PostInitializeComponents try GetComponent() on the reference and cast that to UPrimitiveComponent and save that to another UPrimitiveComponent* property on your actor. After that you should be able to use it like you normally would

1 Like