How to properly attach scene component to other scene component in c++ constructor

S_Q_R
Thu, 12 Dec 2019 15:05:09 +0000
C++

Hi.
I got pretty simple question but I can’t manage to make it work so here is the code and motivation.

I want to make scene component that will have some collision shape and some functionality (lets call it Interactor). I want to be able to switch shapes on different derrived classes so I’m using UShapeComponent and initialize it with USphereComponent as a default one. Then I add my cpp version of Interactor to blueprint actor (lets call it User) and it works perfectly. Problem starts when I create BP version of Interactor. Then when I add BP_Interactor to User I got message
Ensure condition failed: false [File:D:\Build++UE4\Sync\Engine\Source\Runtime\Engine\Private\Components\SceneComponent.cpp] [Line: 1854]
Template Mismatch during attachment. Attaching instanced component to template component. Parent ‘BP_Interactor’ (Owner ‘None’) Self ‘InteractorCollision’ (Owner ‘BP_User’). and the attachment simply doesn’t work at all. I can move my Interactor component freerly and the InteractorCollision does not follows.

I’ve noticed that when I delete UPROPERTY(EditDefaultsOnly) from InteractorCollision attachments work well but I can’t switch shapes anymore so I could use derive straight from USphereCollision and get same result.

Header

UCLASS(Blueprintable,ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class ESCAPEROOM_API UInteractorComponent: public USceneComponent
{
	GENERATED_BODY()

public:
	// Sets default values for this component's properties
	UInteractorComponent();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

	UPROPERTY(EditDefaultsOnly)
	UShapeComponent* InteractorCollision;
};

and here is CPP

// Sets default values for this component's properties
UInteractorComponent::UInteractorComponent()
{
	InteractorCollision = CreateDefaultSubobject<USphereComponent>(TEXT("InteractorCollision"));
	InteractorCollision->SetupAttachment(this);
}

So short recap. How to create and attach one scene component inside other one’s construction script?

4 posts - 1 participant