I have a static mesh component attached to my character, which I would like to change the mesh of dynamically at runtime.
I would like to use StaticMeshcomp->SetStaticMesh(MeshAsset);
But this only works if MeshAsset is set properly, and the only way I have found so far to do this is using ConstructorHelpers::FObjectFinder. This function however only works in a constructor, but I would like this to run in a standalone function that I can call at runtime.
Below is a simplified version of the code I have so far:
void MyCharacter::SetMesh(FString MeshReferenceLocation)
{
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(MeshReferenceLocation); // Cannot run unless in a constructor
if (!MeshAsset.Object)
{
UE_LOG(LogTemp, Error, TEXT("Could not find mesh asset reference."))
return;
}
UStaticMesh* StaticMeshReference = MeshAsset.Object;
MyStaticMeshComponent->SetStaticMesh(StaticMeshReference);
}
Are there any straightforward methods of me loading the StaticMesh asset at runtime using a reference path?
I don’t want to create an object which loads all of my (potentially hundreds) of possible Static Meshes in a constructor when I will only ever need one at a time, as I am worried about the performance issues.