How to set Actor Components to other components of a common type?

Well the example from GameDev StackExchange doesn’t work as described. I keep seeing this confusion between what the correct procedures are for creating a Scene Component or an Actor Component all over the forums. The Attach To method only exists in Scene Components. Actor Components need to use Add <Component> that’s only within Actors. This has been the biggest hurdle in my design is the stupid limitations on actor components.

I wrote a static function that takes an Actor reference and an Actor Component UClass reference, and it calls New Object using these two arguments as parameters because I want to take the variable of an already existing Actor Component type on any Actor and I want to set it to a new instance of whatever the UClass reference is.



UActorComponent* UApolloLibrary::SpawnActorComponentFromClass(UClass *actorComponent, class AActor *Owner)
{
   UClass* spawnedComponent = NewObject<UClass>(Owner, actorComponent);
   Owner->AddOwnedComponent((UActorComponent*)spawnedComponent);

   return (UActorComponent*)spawnedComponent;
}


This totally crashes the editor by the way. I don’t yet if its an incorrect use of New Object, or maybe casting from UClass to Actor Component is causing it, or it could be the weird casting I’ forced to do in blueprints because passing a self reference by reference doesn’t make it writable and below blueprint static function (calling the above one) is suppose to write data to the self reference because that’s where the Actor Component variable is I want to set.

The Actor calling Equip Gun Mod to Tank is doing this on key press.

I’m probably making this more complicated than it should be, and I don’t want to. I’m just trying to figure out what retarded workarounds I need to make Actor Components dynamically interchangeable at runtime.