Derived UProperty from base class

Hi,

I want to have a blueprint which always has a CapsuleComponent inherited.
Therefore I have a class , e.g Base which creates the CapsuleComponent in c++ in the constructor.
Now in the Derived class constructor I want to call the constructor of the Base, with Super::Base() to get the CapsuleComponent

Example code

ABasic
UProperty()
	Foo
ABasic::ABasic()
{
	Capsule = CreateComponent // example syntax
}
	
	
ADerived : ABasic
ADerived::ADerived()
{
	Super::ABasic()
}

This crashes my engine because it seems I cannot inherit this UProperty.
I found a wiki about this, but this does refer to networking.

Is there an easy way to manage inherited Uproperty or at least another way to solve my problem?

Thanks in advance

Best

You need to initialize the property first in the base, the following code is an example of how to initialize it in the base first

UCLASS()
class ABase : public AActor
{
     GENERATED_UCLASS_BODY()
public:

     UPROPERTY()
     UCapsuleComponent* CapsuleComponent;

};

UCLASS()
class ADerived : public ABase
{

    GENERATED_UCLASS_BODY()

};


ABase::ABase(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{

	CapsuleComponent = ObjectInitializer.CreateDefaultSubObject<UCapsuleComponent>(TEXT("CapsuleComponent"));
	RootComponent = CapsuleComponent;
	
}

ADerived::ADerived(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
    //CapsuleComponent is usable at this point
}

thanks for the fast answer :slight_smile:

So I’m doing it wrong when calling Super::ABase() ?

It should be ADerived::ADerived : Super() ?

This is the convention, but the main issue is that you didn’t initialize the pointer property first.
Basically unless a property is private you can access it from any derived class.
Now if that property (or any regular variable) is a pointer you cannot access it without setting it to point at something.

In my base class I initialized the CapsuleComponent like you did in constructor and it is a public property. In my base class I was initializing the CapsuleComponent without using ObjectInitializer so I though just calling the base constructor would work.
I’m not understand what’s wrong fully but I’ll investigate and try your solution :slight_smile:

I recommend reading this:

and this:

thank you I will re-read and try to find my case:)