x

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"

Howto add StaticMesh to SceneComponent

USceneComponent apparently has no RootComponent. So I have no idea how to add a StaticMesh to it:

 // In the constructor of my custom USceneComponent:
 SMC = CreateDefaultSubobject<UStaticMeshComponent>("SMC");
 SMC->SetCollisionProfileName(SHIP_WALL_COLLISION_PROFILE);
 SMC->SetMobility(EComponentMobility::Static);
 SMC->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform); // This compiles but crashes the editor when selecting an actor that contains this SceneComponent. Normally "this" is "RootComponent".

Or do I have to use a ChildActorComponent instead of a SceneComponent? I can't really figure out what the difference between the two is. Note that using a ChildActorComponent instead also crashes the editor the same way.

Product Version: UE 4.18
Tags:
more ▼

asked Jan 05 '18 at 08:33 AM in C++ Programming

avatar image

Napoleonite
595 90 104 155

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

My original code apparently was good. I have no idea why it crashed before. Probably something else caused a crash at exactly that line. But the code should be good. It is safe to derive from USceneComponent and to use the this-keyword to attach stuff to it and there is no need to manually add a RootComponent.

more ▼

answered Jan 18 '18 at 06:27 AM

avatar image

Napoleonite
595 90 104 155

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hey there, SceneComponent and ChildActorComponent are 2 completely different components. SceneComponent is just a simple component with a transform and it's used to organize other components into a group, to serve as a pivot, or just to be there as a placeholder. ChildActorComponent allows you to add the functionality of an actor into your blueprint, so it will spawn an instance of that actor and make it part of your blueprint. Regarding your issue, a Root Component can be any SceneComponent (or child classes of it) but you are using "this", "this" is a reference to the actor, not the RootComponent, so you need to do:

 SMC->SetupAttachment(RootComponent); // instead of SMC->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform);
more ▼

answered Jan 05 '18 at 10:34 AM

avatar image

xlar8or
6k 162 129 305

avatar image Napoleonite Jan 05 '18 at 12:12 PM

Hey there, I see that in that case I indeed need a USceneComponent.

However the reason for me to use this instead of RootComponent is because USceneComponent derived classes have no RootComponent...

 SMC->SetupAttachment(RootComponent);

leads to this error: Error: 'RootComponent': undeclared identifier.

My class inherits directly from USceneComponent. What could I have possibly overlooked? Only actors have RootComponents. I Pressed F12 on an actor RootComponent and came out here (in Actor.h):

 // Line 522-523
 UPROPERTY(BlueprintGetter=K2_GetRootComponent, Category="Utilities|Transformation")
 USceneComponent* RootComponent;

So basically USCeneComponents are 'RootComponents'. That is why I used this. But UE4 does not like that. So how can I make it work?

avatar image xlar8or Jan 05 '18 at 12:19 PM

Ah ok i thought you were dong that in the actor. Afaik custom components can't have components inside them, why do you want to do that? Why not create an actor?

avatar image Napoleonite Jan 05 '18 at 01:26 PM

I assumed that SceneComponents are better for performance compared to Actors, plus Actors have so much more functionality that I do not need. I only need a translation, rotation and scale thingy for my custom class so the SceneComponent made sense. But if you can't attach anything to custom SceneComponents... That changes everything. I wish the UE4 documentation would tell me this but the documentation for this engine is pretty bad. Thanks for that info.

Well, in that case it seems that I will have no choice. I must make them all actors (so 7 actors for 1 cube) and attach the 6 actors to the parent using something like this:

 MyActor->AttachRootComponentToActor(this); // In parent actor

 PrimaryActorTick.bCanEverTick = true; // Disable for performance reasons in the child class (the one that used to be my custom SceneComponent).

Oh I see that I also cannot set the location of the new actors in the parent actor yet because the parent actor apparently is still in 0,0,0 while inside the constructor...

Also I believe that there might be an UE4-bug. The names shown in the label are different from it's actual name when using Actors instead of SceneComponents: alt text

naming-problem.png (20.8 kB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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