Download

Attach BP child class ActorComponent from code.

I’ve got a setup where my character’s base class has an ActorComponent from a C++ base class attached to it, but I would like to make it possible to change this component to a BP child class if specified in a TSubclassOf<BaseComponentClass> variable, so when I open the blueprint of a child character on the editor, this component shows as an Inherited component of the BP class.

This is my failed attempt at achieving it so you can get the idea:

character.h



    //POINTER TO PARENT CLASS INSTANCE
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly)    UDamageManager                    *    DamageManager ; 
    //SUBCLASS SELECTOR
    UPROPERTY(EditAnywhere, BlueprintReadWrite)       TSubclassOf<UDamageManager>            DamageManagerClass;


character.cpp



ABaseCharacter::ABaseCharacter()
{
        if (DamageManagerClass) {
            DamageManager = NewObject<UDamageManager>(this, TEXT("DamageManager"), RF_NoFlags, DamageManagerClass, false, nullptr);
        }
        else {
            DamageManager = CreateDefaultSubobject<UDamageManager>(TEXT("DamageManager"));
        }
        AddOwnedComponent(DamageManager);
}


My intention is to make it so if i leave this blank, or select the parent class on the character blueprint:

https://forums.unrealengine.com/filedata/fetch?filedataid=131369

The component is a parent class component :

https://forums.unrealengine.com/filedata/fetch?filedataid=131370

However if I select any child BP class:

https://forums.unrealengine.com/filedata/fetch?filedataid=131371

I would like my component to be listed as BPChildComponentType so I can manipulate its settings within the editor.

Any help is very much appreciated.