Download

SceneComponent added in C++ won't allow for attached actors in a derived blueprint

I have a base class that inherits from ACharacter. I want this class to have two additional scene components that act as mounting positions for weapons. These two additional components are attached to the RootComponent. Here is the relevant code from the header file and the constructor.


UPROPERTY(BlueprintReadWrite, EditDefaultsOnly)
USceneComponent* leftWeaponSlot;

UPROPERTY(BlueprintReadWrite, EditDefaultsOnly)
USceneComponent* rightWeaponSlot;

AMech::AMech()
{
// -- Constructing Components -- //
	leftWeaponSlot  = CreateDefaultSubobject<USceneComponent>(TEXT("leftSlot"));
	leftWeaponSlot->SetupAttachment(RootComponent);
	leftWeaponSlot->RegisterComponent();

	rightWeaponSlot = CreateDefaultSubobject<USceneComponent>(TEXT("rightSlot"));
	rightWeaponSlot->SetupAttachment(RootComponent);
	rightWeaponSlot->RegisterComponent();

...
}

I have a blueprint class that inherits from AMech called TestMech. TestMech’s BeginPlay attempts to create an actor from another class, and attach it to one of TestMech’s SceneComponents. Despite this, the other class that I want to attach to my scene component does not anchor to it, instead always spawning at (0, 0, 0). I have tested the scene components and they are instantiated at runtime. I have also tested entire process using Scene Components added in the editor. This method works.

I feel that I must be misunderstanding some detail here.


UPDATE
If an instance of my blueprint class is in the editor at runtime (as opposed to being spawned), the spawned weapon WILL attached to the SceneComponent as desired. It seems like something is happening between the constructor in the C++ code and the constructor for the Blueprint; given that being in the editor means the blueprint actor has been constructed.

Hi

Make sure you call Super::… and from above code AMech::AMech() : Super()