Understanding Blueprints Inherited from C++ Classes

I’ll attempt to break this down as simple as possible so the issue is clear.

I have AGameCharacter.cpp that inherits from ACharacter.

AGameCharacter has a USpringArmComponent and a UCameraComponent that get created in the Constructor. The camera is parented to the spring arm:

AGameCharacter.h

    /** Side view camera */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent* SideViewCameraComponent;

	/** Camera boom positioning the camera beside the character */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent* CameraBoom;

AGameCharacter.cpp

AGameCharacter::AGameCharacter()
{
	// Create a camera boom attached to the root (capsule)
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(RootComponent);

	// Create a camera and attach to boom
	SideViewCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("SideViewCamera"));
	SideViewCameraComponent->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
}

I create a blueprint CharacterBP that inherits from AGameCharacter.cpp.

When I change AGameCharacter.cpp such that the camera is now parented to the RootComponent, CharacterBP doesn’t reflect this change. The blueprint editor will not let me reparent them to match what the parent class defines. This seems to only be fixable by commenting out CreateDefaultSubobject for the spring arm and the camera, compiling, then uncommenting them out and recompiling again. Its also fixable by reparenting CharacterBP to a different class then parenting it back. Both incur data loss and are really inconvenient.


Is there something I’m not understanding about the relationship between blueprints and the C++ classes they inherit from? Is the Constructor somehow only ever called once for the blueprint and thus the parenting is a one action?

Hey,

So far as i know Constructor is called during editor startup and reflected to the blueprint system ,but not reflected to the blueprint classes created in editor…
That means Blueprint classes can have their own setup, properties, attachments etc. and whatever you change in c++ constructor, reflection system would not override that values. Default values, attachments and everything set only once when you create a new BP Class which is inherited from C++ class…

I can suggest two overrideable functions (OnConstruction, PostEditProperties) in c++ where you can do hardcoded modifications that would be applied to all class instance :slight_smile:

Hello,

If the only thing what you want to do is to change camera to root component (correct me in comment if that’s not what you mean) then you can do that easily in BP constructor. Just attach camera to root component.

if you realy want to use C++ then

SideViewCameraComponent->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);

should be

SideViewCameraComponent->SetupAttachment(RootComponent);

HotReload - which is the functionality that allows you to compile C++ code without opening / closing the editor - does not update everything. If you have done some changes in C++ that seem correct, but the changes are not correctly reflected in a derived Bluperint class, opening / closing the editor OR closing the editor and compiling the project from the IDE (i.e pressing “Build” in Visual Studio with UE4 closed) instead will usually fix those issues.