Download

How to use "AddComponent" in an Actor to add a StaticMeshComponent

I have an Actor and I’m trying to dynamically add a static mesh component to it in C++. In blueprint there is the following node:

https://forums.unrealengine.com/filedata/fetch?filedataid=187272&type=thumb&attachmentnodeid=

I’m trying to do the same thing but in C++.

It looks like this function:
**AddComponent(FName TemplateName, bool bManualAttachment, const FTransform & RelativeTransform, const UObject * ComponentTemplateContext) **
is the C++ equivalent but I am having trouble getting it to work. Mainly, I don’t know what to put in for TemplateName…

I have tried this:
AddComponent(UStaticMeshComponent.NamePrivate, true, trans, nullptr);
but it won’t even compile. (I’m assuming that the template name in someway denotes the type of component to add, in my case a StaticMeshComponent)

There is only a small page on this function in the documentation and I can’t find an example of it being used anywhere.

In summary I’m looking for an example of how to use this function to add a StaticMeshComponent to my actor at runtiume or a C++ function that will do the same thing as the above BP node.

Thank you!

PS this is my first post so apologies if I have committed any faux pas!

1 Like