The following code has been bothering me for a bit:
ARollingBall::ARollingBall()
{
UStaticMeshComponent* sphere;
sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ball"));
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Engine/BasicShapes/Sphere"));
sphere->SetupAttachment(RootComponent);
if (SphereVisualAsset.Succeeded()) {
sphere->SetStaticMesh(SphereVisualAsset.Object);
}
}
Namely that I am hard coding a path. What if I decided I wanted to use a different object down the road? Imagine I had multiple pawns all hardcoded to use this asset. In cpp there is no easy way to change all these references. I had the bright idea to expose my mesh choice to blueprints to leverage Unreal's reference handling like so:
UPROPERTY(EditDefaultsOnly, Category = "References")
UStaticMesh * m_staticMesh;
However the following does not create a mesh when I inherit from this class with a blueprint and set the default mesh (m_staticMesh):
ARollingBall::ARollingBall()
{
UStaticMeshComponent* sphere;
sphere = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ball"));
sphere->SetupAttachment(RootComponent);
if (m_staticMesh) {
sphere->SetStaticMesh(m_staticMesh);
}
}
I'm using Unreal 4.21.2 both methods compile, but the latter fails to work.
Any suggestions on how to make the above function properly are appreciated. If you know of a better way to avoid hardcoding paths please let me know.