Add Component from another component

Is where any way to add static mesh component to actor from another component (like that

128740-screensave.png

)?

As I understend it is imposible in blueprint, but maby it is poseble in c++ code?
I tried to do that with

SMC = NewObject<UStaticMeshComponent>(GetOwner());
SMC->RegisterComponent(); 

and that

SMC = GetOwner()->CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SMC"));

But it crashed every time… Any idea?

P.S. .h

UStaticMeshComponent* SMC;

Why can’t you just connect GetParent with the Target pin in blueprint? Is the parent not valid in the component from where you try to add the other to the parent?

First of all, parrant is valid but function “Add static mesh component” (or any other component) is not.

Secondly, I don’t wont use blueprint here to much, because I would need to add components once at a 0.01 second or less, and blueprint cant do this. Blueprints do function once at least at one frame, and one frame lasts 0.017-0.033 s (60fps-30fps).

Also I tried just copy paste function “Add static mesh component”, but it is not work either.

Fatal error: [File:D:\Build++UE4+Release-4.13+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Obj.cpp] [Line: 84]
No object initializer found during construction.

Here crash log, but I don’t clearly understend how to give him object initializer in code like that:

GetOwner()->CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshMeshMesh"));

For Blueprint: What about calling a custom function on the actor? The custom function can then add the static mesh.

For C++: have you tried starting the editor from within visual studio, the debugger shall then show where the crash happens.

ConstructSubobject can only be called from constructor, from elsewhere you need to call NewObject. You said you already tried this, but where is it chrashing then?

I tried do this, but static mesh not appears.

LastSMC = NewObject<UStaticMeshComponent>(GetOwner());
		LastSMC->StaticMesh = TestSMCReference->StaticMesh;
		LastSMC->SetWorldTransform(TestSMCReference->GetComponentTransform());

But ok, it is not crashed. (I don’t know why it crashed first time)

Ok, I checked all code second time, corrected the most stupid bugs, and it’s work now. But another problem appear. Static Mesh Components invisible, and I don’t know why. I can teleported to this component, but they are invisible…

Trying to add component from component using Blueprints and I can’t :slight_smile: I don’t want to create hacks and call something on Actor just because of that… I’m teaching Blueprints and it’s another thing that is missing, need to do it in C++.

You should set the static mesh in the component like this:

But first you’ll have to add a static mesh component to the Actor:

252605-myactor.png

This is the same as declaring UStaticMesh* pointer in C++.

As in Blueprint, you CAN NOT add member variables in C++ run-time. All class members must be defined during compile-time.

That being said, you can spawn a new actor with a static mesh and attach it it to the parent actor but this is not advisable in large scale.