I am searching for some hours now. But I find nothing so maybe some of you guys will know that.
Is there any way to add a component in the same manner as we can “Spawn actor from class” by using classes?
I am currently having some blueprints derived from ActorComponent that have each different behaviors (base bloc definition and implementation + all childs with specialized behaviors).
As such. I am having a build tool that is adding new blocs to the “vehicle actor” and also deleting them (I do internal tracking).
But it seems like there is no way to have anything dynamic when it comes to Components like spawning from class or anything like that that would avoid me using something like an enum and then using the “Add Metal Bloc Component” or “Add Thruster Bloc Component” etc etc etc.
I started also digging into C++ code to maybe achieve this using TSubClassOf but I just keep having warnings such as "Static class is not part of TSubClassOf etc. (I could post the exact thing if anybody wants to know).
Some slightly more transparent instructions based on Hayaweh’s method if you are a noob like me. (thanks again so much for your help)
Open your project and create a new C++ Class. when selecting which class it inherits from, choose Blueprint Function Library. Make note of the name of this class and replace ###INSERTCLASSNAMEHERE### below with the class name while leaving the U before it. The proper includes and formatting will be auto generated by unreal, all you have to do is copy and paste the following at the line specified and hit rebuild.
UActorComponent* U###INSERTCLASSNAMEHERE###::AddActorComponent(AActor* Owner, TSubclassOf<UActorComponent> ActorComponentClass)
{
UClass * baseClass = FindObject<UClass>(ANY_PACKAGE, TEXT("ActorComponent"));
if (ActorComponentClass->IsChildOf(baseClass))
{
UActorComponent* NewComp = NewObject<UActorComponent>(Owner, ActorComponentClass);
if (!NewComp)
{
return NULL;
}
//~~~~~~~~~~~~~
NewComp->RegisterComponent(); //You must ConstructObject with a valid Outer that has world, see above
return NewComp;
}
else
return NULL;
}
Ensure that everything compiles properly, save, and exit out of and then reopen Unreal and you will see it in your blueprints. It will not show up in blueprints until you restart unreal.