Set Static Mesh by UProperty Ref via Constructor (C++)

I would like to set the Static Mesh of a Static Mesh Component by referencing a UStaticMesh property via the constructor in C++. It works via BeginPlay, Tick, and the constructor via Blueprints, but not the constructor via C++.

The desired effect would be to select a static mesh for the UProperty and have that propagate to several Static Mesh Components on the actor while in the editor (before pressing play).

Use PostEditChangeProperty.

header

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Engine/StaticMesh.h"
#include "PropagateActor.generated.h"

UCLASS()
class YOUR_API APropagateActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	APropagateActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UPROPERTY(BlueprintReadWrite,EditAnywhere)
	UStaticMesh* TargetMesh;

	UPROPERTY(BlueprintReadWrite, EditAnywhere)
	UStaticMeshComponent* Mesh1;

	void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
	
};

cpp

#include "PropagateActor.h"

APropagateActor::APropagateActor()
{

	PrimaryActorTick.bCanEverTick = true;
	TargetMesh = CreateDefaultSubobject<UStaticMesh>(TEXT("StatMesh"));
	Mesh1 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh1"));	
}


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

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


void APropagateActor::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
	Super::PostEditChangeProperty(PropertyChangedEvent);	
	Mesh1->SetStaticMesh(TargetMesh);
	Mesh1->MarkRenderStateDirty();
}
2 Likes

Thank you very much. Just got the same answer on Reddit. Worked perfectly. I greatly appreciate the help <3