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"

Actor with 2 separate components

Hello, Im new to ue4 and im working on a project. I created an actor with c++ class. I want my actor to have 2 separate components (not one attached to the other). If i declare the 2 components in the header file and then in the .cpp file i type: StaticMesh(the name of the first component) = CreateDefaultSubobject("StaticMeshComponent"); and after that i write the same for the other component(just the name is not the same), it attaches the second one to the first one! Soo my question is how to make them separatley not one attached to the other? Can you give me the code please! Sorry for my bad english!

Product Version: UE 4.22
Tags:
more ▼

asked Jun 03 '19 at 02:52 AM in C++ Programming

avatar image

warper300
1

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

1 answer: sort voted first

I'm assuming you're doing something like this:

 // Sets default values
 ATestActor::ATestActor()
 {
     PrimaryActorTick.bCanEverTick = true;
 
     Mesh1 = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent");
     Mesh2 = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent2");
 }

The problem here is that the first component you create will overwrite the "Default root" component. That's why you see the first mesh become the root component and the second mesh attaches to the first.

If you don't want either mesh to become the root component, you need to specifically create a component to be the root component.

Here is a code snippet:

 // Sets default values
 ATestActor::ATestActor()
 {
     PrimaryActorTick.bCanEverTick = true;
 
     // Scene root
     USceneComponent* sceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("DefaultSceneRoot"));
     RootComponent = sceneRoot;
 
     Mesh1 = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent");
     Mesh2 = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent2");
 }

And here's how the components will look in Blueprints: alt text

testactorbp.png (11.5 kB)
more ▼

answered Jun 04 '19 at 04:24 AM

avatar image

Punitiate
103 6 9 66

(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