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"

Add StaticMeshComponent at runtime

Hi,

I want to add a dynamic number of static mesh components to an actor at a specific event during runtime. I tried the following:

     if (System && Actor)
     {
         TArray<AStar*> Stars = System->GetMembersByType<AStar>(AStar::StaticClass());
         for (AStar* Star : Stars)
         {
             UStaticMeshComponent* Component = NewObject<UStaticMeshComponent>(this);
             Component->bAutoRegister = true;
             Component->SetStaticMesh(SphereMesh);
             Component->SetCollisionEnabled(ECollisionEnabled::NoCollision);
             Component->AttachTo(Actor->GetRootComponent());
             Component->RegisterComponent();
             Component->SetWorldScale3D(FVector(0.1f, 0.1f, 0.1f));
             Component->SetRelativeLocation(FVector(0.f, 0.f, 10.f));
         }
     }

I get no error message, but nothing shows up in the world. I also tried to run it without bAutoRegister = true and without RegisterComponent() since I think they are redundant, but it nothing changed.

The variable SphereMesh is set in the Constructor like so:

 static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereModelOb(TEXT("/Game/Shapes/Shape_Sphere"));
 if (SphereModelOb.Succeeded())
 {
     SphereMesh = SphereModelOb.Object;
 }

Can you give me a hint what I am doing wrong?

Thanks in advance

David

Product Version: Not Selected
Tags:
more ▼

asked Oct 23 '14 at 02:30 PM in C++ Programming

avatar image

Oromis
18 5 7 10

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

2 answers: sort voted first

I have some code that looks like this:

 UMyCustomRenderingComponent* AMyActor::CreateSubComponent(...)
 {
     UMyCustomRenderingComponent* newComponent = NewObject<UMyCustomRenderingComponent>(this);
     newComponent->RegisterComponentWithWorld(GetWorld());
     newComponent->AttachTo( RootComponent );
     AddOwnedComponent(newComponent);
     newComponent->SetRelativeLocation( ... position ... );

     return newComponent;
 }

which seems to work great.

more ▼

answered Oct 24 '14 at 03:08 AM

avatar image

ryani
52 8 14 30

avatar image Oromis Oct 24 '14 at 09:23 AM

Thanks for your input, but I'm afraid that this also doesn't work for me. Just out of curiosity, is your CreateSubComponent() method inside an AActor derived class? Mine is not, do you think it makes a difference?

avatar image ryani Nov 25 '14 at 04:34 AM

Yes, it is, but I don't think that's relevant, as long as you make the component owned by a particular Actor at some point. I think you might be missing the RegisterComponentWithWorld() call? I remember that being tricky and hard to find.

avatar image Dragon Slave Jul 02 '18 at 09:01 AM

I think it is relevant. Just try adding a mouse cursor in the player controller of the default top down template. You'll find that it never shows up, but if you implement the same code in the character it works.

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

I have my own custom component which spawns a number of static mesh components and attaches to itself. I do this:

 StaticMeshComponent = ConstructObject<UStaticMeshComponent>(UStaticMeshComponent::StaticClass(), GetOwner(), NAME_None, RF_Transient);
 
 // Add to map of components.
 StaticMeshComponents.Add(StaticMesh, StaticMeshComponent);
 StaticMeshComponent->AttachTo(this, NAME_None, EAttachLocation::KeepRelativeOffset);
 StaticMeshComponent->SetStaticMesh(StaticMesh);
 StaticMeshComponent->SetVisibility(true);
 StaticMeshComponent->RegisterComponent();
 
 // Transform the component by transformation provided by our framework.
 StaticMeshComponent->SetRelativeTransform(FTransform(GOP.TransformMatrix));

Which seems to be very close to what you are doing; out of curiosity, try recreating render state for them? RecreateRenderState_Concurrent etc.

more ▼

answered Nov 25 '14 at 06:03 PM

avatar image

ttvd
333 20 25 34

(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