Download

NEED HELP!!! BP Actor can't inherit Cpp Actor data that Create by Constructor Method

Hello, Every one.

Recently I have a problem, It occurs when BP Actor inherited from My C++ Actor.

I wirte a App Actor, like this:



// .h file
UCLASS()
class COSMOS_API MyCppActor: public AActor
{
	GENERATED_BODY()

	static FName BackName;
public:
	UPROPERTY(Category = TestCG, VisibleAnywhere, BlueprintReadWrite)
	USceneComponent* Root;
	UPROPERTY(Category = TestCG, VisibleAnywhere, BlueprintReadWrite)
	class UStaticMeshComponent* Back;
public:	
	MyCppActor();
	virtual void BeginPlay() override;
	virtual void Tick( float DeltaSeconds ) override;
};


// .cpp file
FName MyCppActor::BackName(TEXT("BackMesh"));

MyCppActor::MyCppActor()
{
	PrimaryActorTick.bCanEverTick = true;
	Root = CreateDefaultSubobject<USceneComponent>(TEXT("MyRoot"));
	UStaticMesh* TmpMesh = nullptr;
	UMaterialInterface* TmpMat = nullptr;
	UMaterialInstanceDynamic* tmpMID = nullptr;

	if (Root)
	{
		Back = CreateDefaultSubobject<UStaticMeshComponent>(MyCppActor::BackName);
		Back->bCastDynamicShadow = false;
		//	load mesh from plugin Meshes dir and set to Back(an UStaticMeshComponent*)
		TmpMesh = DSUtility::LoadMeshFromPath(TEXT("/MyTestPlugin/Meshes/Back"));
		Back->SetStaticMesh(TmpMesh);
		//	load material from plugin Materials dir
		TmpMat = DSUtility::LoadMatFromPath(TEXT("/MyTestPlugin/Materials/Back"));
		
		//	create material instance and set to mesh
		tmpMID = Back->CreateAndSetMaterialInstanceDynamicFromMaterial(0, TmpMat);
		Back->AttachParent = Root;
	}

	RootComponent = Root;
}

void MyCppActor::BeginPlay()
{
	Super::BeginPlay();	
}

void MyCppActor::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
}


In Editor, when I created an Cpp MyActor instance into scene, there is no problem, all right.

But, if I create a new BP Actor(MyBpActor) inherit from CPP Actor(MyCppActor) and create instance from BP Actor(MyBpActor), There will be problems.
I can’t see any material instance on Back(UStaticMeshComponent).

May I ask why? and How to solve this problem?

thank you.

Not sure if this solves it. But try calling super constructor from your BP.

Hi there! My only guess is that the BP inherited instance is calling the constructor with an FObjectInitializer instead of your constructor. This is being generated by the GENERATED_BODY() macro. If you want to implement your own constructor, use GENERATED_UCLASS_BODY() instead of the GENERATED_BODY() macro. Then, implement your constructor that takes a const FObjectInitializer, for example:

MyCppActor(const class FObjectInitializer& ObjectInitializer);

In your implementation, you’ll want to make sure you call the parent’s constructor as well. See this doc for more details: Gameplay Classes | Unreal Engine Documentation

trying using

UCLASS(Blueprintable)