Attach a SkeletalMeshComponet to SceneComponent at Construction

Hello all,

First of all, I try to attach a USkeletalMeshComponent to a custom USceneComponent at the construction of the custom USceneComponent.
I read a lot of answers some of them say use SetupAttachment(), others say to attach the component in OnComponentCreated(). But none of them works correctly for me. My current Constructor looks like this.

UVisualItemComponent::UVisualItemComponent()
{
	PrimaryComponentTick.bCanEverTick = true;

	SkeletalMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComponent"));
	SkeletalMesh->SetupAttachment(this);
}

But the results are always the same, even if I restart the editor.

264653-wrong.png

Sure I can assign a skeletal mesh to it, but should it not look like this?

264654-correct.png

best regards

What “correct” looks like depends on what you determine works best for your purposes.

But, as far as your constructor goes, I’m pretty sure that calling SetupAttachment(this) doesn’t make any sense, because that’s basically saying “attach this thing to itself.” I doubt that’s what you’re trying to do.

If you look at other actor constructors, you’ll commonly see that the first component is set to be the root component, and subsequent components are frequently attached to that (new) root component — and so forth. Here’s an excerpt from the ACharacter class constructor:

CapsuleComponent = CreateDefaultSubobject<UCapsuleComponent>(ACharacter::CapsuleComponentName);
CapsuleComponent->InitCapsuleSize(34.0f, 88.0f);
CapsuleComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
[...]
RootComponent = CapsuleComponent;

That creates a capsule component (for collision) and makes it the root component. Then, just a bit further down…

Mesh = CreateOptionalDefaultSubobject<USkeletalMeshComponent>(ACharacter::MeshComponentName);
[...]
Mesh->bCastDynamicShadow = true;
Mesh->bAffectDynamicIndirectLighting = true;
Mesh->PrimaryComponentTick.TickGroup = TG_PrePhysics;
Mesh->SetupAttachment(CapsuleComponent);
[...]

…the character’s skeletal mesh component is created, and attached to the CapsuleComponent (by calling SetupAttachment(CapsuleComponent).

How you set up your component hierarchy is up to you, but just remember that the transforms of parent components, by default, affect the position/rotation/scale of child components.

I hope that helps clear things up!

Hello the_batch,

thx you for taking the time to answer. Regarding your answer.

But, as far as your constructor goes, I’m pretty sure that calling SetupAttachment(this) doesn’t make any sense because that’s basically saying “attach this thing to itself.” I doubt that’s what you’re trying to do.

Well, I think using “this” pointer for SetupAttachment() makes sense. Because it is called on the new created SkeletalMesh in the Constructor of the SceneComponent which is the creator of the SkeletalMeshComponent. Or in other words in my case, the “this” pointer is the SceneComponent which creates another SkeletalMeshComponent in its constructor and tries to set itself as the parent of the newly created SkeletalMeshComponent. :slight_smile:
It is similar to the code you posted but with a different thing, in your code example, you set the RootComponent of the Actor to a CapsuleComponent, then create a new MeshComponent and then set the parent of the MeshComponent to the CapsuleComponent (the root component of the Actor). In my case, I have no Actor in the constructor, only the SceneComponent which try to create and attach a new created MeshComponent to itself. Sure the SceneComponent will have an Actor later on but at the construction time it did not know anything about an Actor or known RootNode.

I understand if what I wrote now is TL;DR. Anyway thank you for your feedback, I didn’t try to assign the SkeletalMeshComponent at BeginPlay. Maybe this solves my “issue”. Because in BeginPlay the SceneComponent should already be setup correctly. I keep you updated if it worked.

@ThePathfinder, you’re totally right. I was wrong with that statement, and I now clearly see how you’re creating your USkeletalMeshComponent from within the constructor of another component.

I’m glad I didn’t derail you with that bad advice. Thanks for correcting me for the sake of any future visitors of this page.

I hope to hear that you get everything working, even if I didn’t help very much!

@the_batch All good, I am glad to read another feedback. Every feedback is good feedback. It helps me to rethink things. :slight_smile:

In the end, I leave it as it is. I could use the attached component, but it just looks ugly in the editor. So still thank you for your input, I appreciate it.