Can You Add/Remove Blueprint Components in C++ During Editor Runtime? How?

The Situation:
I have a Pawn with the intended functionality of being able to transform into many different objects - however, these objects come in many different shapes and sizes, so I need to be able to change the collision type of my pawn at will.

The Problem:
I can’t seem to find a way to change the collision type in the editor with a drop down variable, let alone change it during runtime, with C++ code. What I’ve tried so far is below…

My Attempts:

  • My first idea was simple polymorphism - use the UShapeComponent as the root so it could point at any type of simple collision shape, but unfortunately, Unreal doesn’t let you do that. The UShapeComponent will not show up in the blueprint as the root, or at all for that matter, and will often set another component (like the mesh) as the root by default, which defeats the purpose.

  • My second idea was to keep a variable of each collision type and switch them out for the root whenever changes are made. However, I need a default collision added in the constructor, but when I do this, I cannot find a way to remove it, so it still exists when I switch the root out for another collision. I also have no idea how to create and keep information about a collision in code, and then add it to the editor at will - I’ve only been able to figure out how to add a component in the constructor with CreateDefaultSubobject() or with CreateObject().

(Note: To make the work easier for designers, I planned on making a drop down to select which collision to use, then I would change the root in the PostEditChangeProperty and remove the old one from display, but I can’t seem to figure out the removal part - and even when I do, how do I add a new component to take it’s place?)

The Question:
So, how would I go about doing this?

  • Can I actually use UShapeComponent as the root, but just have to make some tweaks?
  • If ^that’s not possible, how would I initialize the each collision type without adding them to the Blueprint hierarchy and then how would I swap them out with each other for the root component (only ever showing 1 collision at a time)?
  • If neither of those are possible - What can I do to get the desired result?

Ideally, I would like a nice tool for designers to use with a simple drop down to switch between collision types, and have C++ code handle the information for better workflow.

You could instead create a Pawn per configuration of colliders, designers can create a child BP from which ever Pawn they need and then tweak whatever properties they need to.
Ingame spawn and possess the new pawn.

Regarding creating components at runtime you can use this:

Component = NewObject<USceneCaptureComponent2D>(this, "Component");
Component->RegisterComponent();

We have considered that, but it seems really impractical. First of all, we may allow the user to change their form indefinitely and repeatedly - spawning and possessing a different pawn and deleting old ones would be really expensive compared to just copying collision and mesh information over to the player’s pawn.

It’s more optimal to handle the work within the pawn itself - we are even willing to let every collision just be visible in the Blueprint hierarchy as un-editable and handle swapping the root component during gameplay runtime - as a worst case scenario.

We can let the objects that are used for transformations have their collisions be manually selected in the BP editor (though it would be nice to make it changeable for a cleaner workflow), but we cannot yield this functionality for the main pawn.

Thank you for the input though, I’ve not seen RegisterComponent() before, so I’ll check it out.

To remove a component from the hierarchy, we use *->DestroyComponent().
(* being the component name such as CapsuleComponent->DestroyComponent())

With this, you can simply detach and destroy the old component, and call NewObject<UComponentNameHere>(this);, then set it up as normal.