Download

Questions regarding setting up components in c++

So I’ve been trying to do something fairly simple like adding components in C++, and have some confusions regarding organizing the hierarchy.




ABaseOffensiveConstruct::ABaseOffensiveConstruct()
{
constructBase = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Construct Base"));

rotatingConstructPart = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Rotating Construct Part"));

muzzle = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Muzzle"));

}



When I do this, “constructBase” becomes the root even with the setupAttachment(GetRootComponent()) line commented out. Why is this? I would imagine if nothing is specified, any component added should become the child of the default root, not become the default root itself?

Even more confusing, “rotatingConstructPart” and “muzzle” show up as children of “constructBase”. Why? Here’s a screenshot : Imgur: The magic of the Internet

I want the hierarchy to be setup such that “constructBase” and “rotatingConstructPart” are both children of the root component, and Muzzle is the child of “rotatingConstructPart”. I tried doing this, but no dice :




ABaseOffensiveConstruct::ABaseOffensiveConstruct()
{
constructBase = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Construct Base"));
constructBase->SetupAttachment(GetRootComponent());

rotatingConstructPart = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Rotating Construct Part"));
rotatingConstructPart->SetupAttachment(GetRootComponent());

muzzle = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Muzzle"));
muzzle->SetupAttachment(rotatingConstructPart);

}



Here’s a screenshot of what this code yields : Imgur: The magic of the Internet

Can someone please tell me what I’m doing wrong here. Thanks!

In that constructor you aren’t adding anything as root component before those 3 components…

@Shmoopy1701 Ah, yes! I assumed a root component would exist by default, but apparently not. Did that, and it works fine now. Thanks!

Heh np. :cool: