Context:
Consider the following code:
.h
UCLASS()
... AHexMap : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Mesh")
UStaticMesh* tileMesh; // Exposed to editor
UPROPERTY()
USceneComponent* root;
UPROPERTY()
UInstancedStaticMeshComponent* tile;
...
}
.cpp
...AHexMap::AHexMap(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
root = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("Root"));
RootComponent = root;
tile = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("Tile"));
tile->SetupAttachment(RootComponent,TEXT("Tile"));
// Setting static mesh of component in the constructor
tile->SetStaticMesh(tileMesh);
}
// Instances of tile are created in BeginPlay()
The above code allows a designer to choose which mesh to use for the tile from the editor. The component instances, however, do not render the mesh when written as above. If I instead place the line
tile->SetStaticMesh(tileMesh);
at the very start of BeginPlay(), the mesh renders as expected.
Question:
Why is this happening?
If I had to guess, I would say this is due to either
-
The lifecycle of UPROPERTY variables assigned values through the editor. In this case a mesh assigned through the editor may not be available on construction (for whatever its worth, I tried placing the line in OnConstruction and it still did not render).
-
The lifecycle of components. I think components aren’t registered until after the constructor has been called so maybe the component isn’t at the point in its lifecycle where it can be assigned a mesh (?)
Obviously these are shots in the dark. I am just trying to better understand what’s happening here.