Class with a dynamic custom component

Hi, wanted to do the following:

i have this component:

ICustomInt
|
|
|___ACustomComponent
|
|
|_______ABCustomComponent
|
|
|_______ACCustomComponent

Now, i want to create a custom Actor class:

ACustomClass

This class could have any on this ABCustomComponent or ACCustomComponent, is there any way of make this possible? i try with TSubcalssOf but this didn’t work, any one have any advice on how to make this possible? of i will have to create a custom class for each.

269673-class.png

If I understand the questions correctly, you are asking how to make an interface in Unreal Engine C++. This is possible, and you can find the docs on how to do it here.

The concept works the same as it does in normal C++, you just have to add some meta data to your interface class.

Hi BenjaFriend,

OK, thx interfaces clarification, but my questions really is if i can create the component on the actor as the interface???

Example:

ACustomActor.h 

UPROPERTY(EditDefautsOnly, Category = "Components")
ICustomInterface *CustomComp = NULL;

ACustomactor.cpp
 
CustomComp = CreateDefaultSubject<ICustomInterface>(TEXT("CustomComp"))

How On the BP i will specify what type of Component i’m using??

Ah ok, this should work:

// ACustomActor.h
    /** Define this in your blueprint */
    UPROPERTY( EditDefaultsOnly, Category = "CustomComponent")
    TSubclassOf<UInterfaceComponent> Interface_Class;

    /** The actual instance of this interface */
    UInterfaceComponent* CustomComp = nullptr;


// ACustomActor.cpp
// BeginPlay()...
if ( CustomComp != nullptr )
    {
        CustomComp->DestroyComponent();
    }
    CustomComp = NewObject< UInterfaceComponent>( this, *Interface_Class );
    if ( CustomComp )
    {
        CustomComp->RegisterComponent();
    }

Benja, so try this, but i got a question, this “UInterfaceComponent” should be a child class from UActorComponent right??

like this?

class UCustomInterface: public UInterface, public UActorComponent
{
	GENERATED_BODY()
};

/**
 * 
 */
class TESTGAME_API ICustomInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	
	UFUNCTION()
	virtual void A() = 0;
	
};

Or Like this?

class UCustomInterface: public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class TESTGAME_API ICustomInterface:  public UActorComponent
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
	
	UFUNCTION()
	virtual void A() = 0;
	
};

Your interface class itself doesn’t need to inherit from UActorComponent. The Interface should just have your methods that you want to implement in your child classes.

The components that you make to implement that interface should all inherit from UActorComponent and your custom interface.

Well, but the interface itself dont have a metoc called DestroyComponent() or RegisterComponent()

ICustomComponentInterface* CustomComp = nullptr;

When i try to compile this i get this error:

**********************\ICustomComponentInterface.cpp(32): error C2039: 'DestroyComponent': is not a member of 'ICustomComponentInterface'
**************/ICustomComponentInterface.h(21): note: see declaration of 'ICustomComponentInterface'
*************************\ICustomComponentInterface.cpp(37): error C2039: 'RegisterComponent': is not a member of 'ICustomComponentInterface'
*************************/ICustomComponentInterface.h(21): note: see declaration of 'ICustomComponentInterface'