Add component to actor?

Hello,

Working on a plugin, and am wondering if anyone knows how to add a component to an AActor from C++? I saw many ways to do this procedurally and dynamically during runtime, but no examples outside of runtime. Specifically, how would I use FObjectInitializer::CreateDefaultSubobject when
a) Not in runtime (during editor mode) and
b) the class doing the component creation is not the class I want to add the component to (Ive simply got a reference to the world actor where I want to add a reference)

Hello,

Originally, I was going to create an ‘Editor Only’ Skeletal mesh to display some stuff for the game’s developers, and add that mesh to already existing world actors, but now Instead I’m simply creating a new Editor only actor that already has a SkeletalMeshComponent, and instead will spawn this editor only actor when the editor is finished loading. So unfortunately I stopped trying to figure out dynamic addition of components in-editor (though it definitely should be possible)

One thing I will note though, that might be of help to you, is that you can call regular Blueprint functions from C++, not necessarily Blutilities, during editor mode. I attempted with Blutilities and they were very experimental and I found this method to work better. for C++ → Blueprint communication:

FEditorScriptExecutionGuard ScriptGuard;
{
BlueprintInheritedClass->EditorFunction();
}

where BlueprintInheritedClassis a reference to any runtime C++ class, that is inherited by a Blueprint. And EditorFunction() is a BlueprintNativeEvent that is overriden in BP:

UCLASS()
class BlueprintInheritedClass
{
public:
UFUNCTION(BlueprintNativeEvent)
returnType EditorFunction(); //
}

Pretty much what FEditorScriptExecutionGuard lets you do is run Blueprints in editor mode. Not sure if you can even add skeletal meshes dynamically through Blueprints (during runtime), I dont blueprint at all, but if you can, then you should be able to create a BP C+±callable function that adds all of these skeletal and static meshes, add your empty object(s) in your world, loop through all your world objects of this type in C++, get their reference and call their EditorFunction()

Hi Penanito. Did you ever find a way to do this? I have a similar problem and would like to automate adding static and skeletal meshes to an actor in the Editor… NOT at runtime. Here is a link to my question.

Surely there’s a way to do this programatically in the Editor, but so far I haven’t found a way to access the AActor BP to add components to it without creating an instance of it in the game. Yet we can do exactly this in the Editor using the GUI. I just want to automate that process, and it sounds like you might be looking to do something similar for your plugin.

Any leads?

Thanks Penanito! I was unaware of the whole FEditorScriptExecutionGuard concept, so I’ll toy around with this and see if I can make any progress.

I am looking for a way to do this as well–trying to generate the equivalent of “prefabs” by importing a collection of static meshes into an Actor.