Spawn a component from a component

I’m making a custom skeletal mesh component and want to spawn other components from it and attach them.

The reason for this is the characters in our game have a lot of customisation options and it would be really cool to have a skeletal mesh component that handles all of this, so that we can re use it in different places and not just in the character BP.

I managed to build all the morph target functions we need into the skeletal mesh component pretty easily and this works really nicely. So the next step is spawning extra meshes and attaching them like hair for example.

I can spawn a mesh and attach it no problem, but it doesn’t show up in the blueprint editor components list and if I set it as VisibleAnywhere all of the properties show up in the same details panel as the main skeletal mesh component. I’m wondering if there’s a way to spawn a component from a component and have it show up correctly in the BP editor? The other way of doing this would be with a child actor but that doesn’t seem like a great way of doing it.

Components spawned by other components needs to be done at runtime using NewObject<>() (and using the owning actor as the outer). You can’t do it from the Components Constructor without causing yourself problems, and therefore they can’t be default objects.

Not giving components a suitable actor as the owner will break most component behaviour, won’t work with replication etc, won’t serialize properties correctly in some cases. Components are very specifically designed to work with actors as their owner.

Not really sure what the end goal is here - but you could always create an Actor Component which manages the attachments, and require that the actor gives it a skeletal mesh to operate on.

2 Likes

Thanks for the info! That’s super helpful :slight_smile:

The end goal is basically a custom skeletal mesh component that handles all aspects of character customisation. By keeping it all contained there I can add it to any actor and the customisation will already be functioning without adding anything else, and I can then make use of it pretty much anywhere really easily as it will need to be used in places other than the character BP. I’ve added functions that can load morph values from a table or an array, I’ll also create the dynamic material instances for editing skin/hair/eye colour and hopefully figure out a way to add extra components like hair and clothing.

I like your idea of the actor giving the component the skeletal mesh, it’s the next best thing so I’ll give that a go and see how it works out. Thanks again :slight_smile:

Alternatively couldn’t you just create a master default character class in C++ with components setup handled in the character constructor based on variables from the custom components? Then you just open the variables to Blueprints and create child classes of the master class to make customised progenerated characters[If that’s the ultimate aim]. (I’d probably split the skeletal mesh from the clothing) [My reasoning is that I would want away for players to switch their clothing later so I think it should be separate from the body customisations so I can later extend a inventory system to the clothing].

Also how do you plan to attach clothing? With bones or with sockets?