Download

Edit C++ UProperty component on child BP Class

Im tryng to make a BP class into a C++ class, so I’m making the BP class inherit from the new C++ class.

But when I bring a BP into the world I can’t see or edit its StaticMeshComponent declared in the parent c++ class.
Also I initialize the component in the constructor and gets initialized, but when the begin play executes, the component is referencing NULL.

However when I bring into the world an instace of the parent C++ class I can see and edit it and works as expected.

C++ .h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Engine/DataTable.h"
#include "Ingredient.generated.h"

USTRUCT()
struct BLANC_API FIngredientMeshes : public FTableRowBase
{
	GENERATED_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		UStaticMesh* Ingredient;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		UStaticMesh* Spawner;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		UStaticMesh* Middle;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		UStaticMesh* Mixed;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FName Heat;
};

UCLASS()
class BLANC_API AIngredient : public AActor
{
	GENERATED_BODY()
public:	
	AIngredient(const FObjectInitializer& ObjectInit);

protected:
	virtual void BeginPlay() override;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FName BaseType = TEXT("Tomato");
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		TArray<FName> IngredientList;
	
	UPROPERTY(BlueprintReadOnly)
		UDataTable* DataTable;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
		USceneComponent* SceneComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
		UStaticMeshComponent* StaticMesh;
};

C++ .cpp

#include "Ingredient.h"

AIngredient::AIngredient(const FObjectInitializer& ObjectInit)
: Super(ObjectInit)
{
	PrimaryActorTick.bCanEverTick = false;

	StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComponent"));	
	RootComponent = SceneComponent;
	StaticMesh->SetupAttachment(RootComponent);

	static ConstructorHelpers::FObjectFinder<UDataTable> DT_Ingredients(TEXT("DataTable'/Game/VRCooked/Data/DT_Ingredient.DT_Ingredient'")); 
	if (DT_Ingredients.Succeeded())
	{
		DataTable = DT_Ingredients.Object;
	}
	
	FIngredientMeshes* Ingredientt = DataTable->FindRow<FIngredientMeshes>(BaseType, "");
	StaticMesh->SetStaticMesh(Ingredientt->Ingredient);

	BaseType = TEXT("Lettuce");
}

void AIngredient::BeginPlay()
{
	Super::BeginPlay();
	if (StaticMesh == NULL) {
		UE_LOG(LogTemp, Error, TEXT("IS NULL"));
	}
	else{
		UE_LOG(LogTemp, Error, TEXT("NOT NULL"));
	};
	
	UE_LOG(LogTemp, Error, TEXT("%s"), *BaseType.ToString());
}

BP
image image

So you don’t actually need EditAnywhere for a component. That’s saying that you can change which UStaticMeshComponent StaticMesh is pointing to. What you actually want is VisibleAnywhere. This will make the component visible, and it’s properties will be editable (if you look in UStaticMeshComponent it will have properties that have EditAnywhere flags).

If you’re still getting null for the components after that, perhaps try switching the constructor to
.cpp

AIngredient::AIngredient(const FObjectInitializer& ObjectInit)
: Super(ObjectInit)
{
}

.h

AIngerdient::AIngredient(const FObjectInitializer& ObjectInit);

I would also suggest not getting the DataTable like that, and instead using EditAnywhere, or EditDefaultsOnly (which means you can only set it up in the blueprint class, not in instances of the blueprint)

Thanks for the answer, I learned something, but that didn’t fix the problem for the Static Mesh Component, it’s still not editable or visible from the child BP class and it’s still referencing NULL after construction. The Scene Component is working a expected.