load static mesh in c++

I have multiple static mesh files (in fbx format under %USERPROFILE%\Documents\models) imported to Unreal under Contents/MyMeshes. And they are stored with uasset extension. I was trying to load them with StaticLoadObject but always end up with nullptr.

Here is my code,

MyActorBuildings.h:
UCLASS()
class INSTANCEOBJECTS_API AMyActorBuildings : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor’s properties
AMyActorBuildings();

    virtual void OnConstruction(const FTransform& Transform) override;

UFUNCTION(BlueprintCallable, Category = "Buildings")
TArray<FString> load_buildings();

UFUNCTION(BlueprintCallable, Category = "Buildings")
void populate_buildings(TArray<FString>& building_entries);

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

MyActorBuildings.cpp:
void AMyActorBuildings::BeginPlay()
{
Super::BeginPlay();

TArray<FString> building_entries = load_buildings();
populate_buildings(building_entries);

}

void AMyActorBuildings::load_buildings()
{
TArray arr;
arr.Add(TEXT(“tower.uasset”));
arr.Add(TEXT(“powerline.uasset”));

return arr;

}

void AMyActorBuildings::populate_buildings(TArray& building_entries)
{
FString filepath;
FString assetpath = FPaths::ProjectContentDir();
assetpath.Append(“/MyMeshes/”);

for (int inst=0; inst < building_entries.Num(); inst++)
{
    FString modelName = building_entries[inst];
    filepath = assetpath;
    filepath.Append(modelName);

    UInstancedStaticMeshComponent* ism = NewObject<UInstancedStaticMeshComponent>(this, *modelName);
ism->RegisterComponent();
ism->SetFlags(RF_Transactional);
ism->SetMobility(EComponentMobility::Movable);

this->AddInstanceComponent(ism);;

UStaticMesh* meshToUse = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, *filepath, *filepath));
    if (meshToUse != NULL) {
    GEngine->AddOnScreenDebugMessage(-1, 60.f, FColor::Green, TEXT("loaded asset:") + filepath);
    ism->SetStaticMesh(meshToUse);								
} else {
        GEngine->AddOnScreenDebugMessage(-1, 60.f, FColor::Red, TEXT("failed to load asset:") + filepath);
    }
}

}

I have never hardcoded asset paths, I am using datatables to configure rows containing assets for certain situations, stored in the datatable as soft (class/object) pointers. Only the moment I need assets to load I can sync / async load the soft pointers to ensure I don’t load things into memory all at once. This has more benefits, that you don’t hardcode anything in c++. In C++ you could create a spawner (actor class) holding a datatable row handle property that can be configured from the blueprint side. in C++ all you do is read the datatable and its row / rows.

Data Driven Gameplay Elements | Unreal Engine 4.27 Documentation

All about Soft and Weak pointers | Tutorial

It turns out in load_buildings method, I have to change the name of the asset to tower.tower and powerline.powerline, and then the static mesh load properly.

thx,