Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

     PrimaryComponentTick.bCanEverTick = true;
     SkeletalMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComponent"));

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

alt text

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

alt text

best regards

Product Version: UE 4.21
wrong.png (18.1 kB)
correct.png (18.6 kB)
more ▼

asked Jan 08 '19 at 08:56 PM in C++ Programming

avatar image

135 6 6 18

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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);
 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;

...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!

more ▼

answered Jan 09 '19 at 10:07 PM

avatar image

1.1k 4 5 8

avatar image ThePathfinder Jan 10 '19 at 08:51 PM

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. :) 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.

avatar image the_batch Jan 10 '19 at 09:05 PM

@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!

avatar image ThePathfinder Jan 10 '19 at 09:44 PM

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

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.

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question